siliconwafer said:
#include<stdio.h>
#include<conio.h>
Non-standard header. You don't need this unless you're trying to do a
full-screen editor. Non-standard libraries and headers are not covered here
in comp.lang.c - I'll point those out as I go.
#include<stdlib.h>
#include<string.h>
int main() {
unsigned char*cptr;
unsigned int count = 0;
unsigned char*tptr;
unsigned int size = 2;
unsigned int startpt = 1;
FILE*fptr;
clrscr();
Non-standard function.
fflush's behaviour is *only* defined for streams open for output or update.
This is not the way to "clear the buffer", so to speak.
cptr = (char*)calloc(sizeof(char),10);
cptr = malloc(10); would be perfectly acceptable, provided you remember to
null-terminate your data after reading it. Or you can do:
cptr = calloc(1, 10);
The cast is unnecessary. See
http://www.cpax.org.uk/prg/writings/casting.php
for an explanation of why not to use a cast. Also note that sizeof(char) is
1 by definition. Finally, note that malloc, calloc, and realloc can all
fail. If they do, they return NULL, in which case you should not try to use
the resulting memory because there isn't any.
if((fptr = fopen("c:\\Shekhar\\C\\myTextInEditor.txt","w")) ==
NULL) {
Later, you'll want to modify this to get the name of the file from the user
in some way.
printf("File Cannot be created!!\n");
exit(EXIT_FAILURE);
}
while((*cptr = getchar()) != '\n') {
You need to test the possibility that the user has indicated a desire to end
standard input. In such a case, getchar() returns EOF - which is not a
character (and should not be treated like one) but a message to you, the
programmer, that there will be no more input from that stream.
fprintf(fptr,"%c",*(cptr));
*(cptr): the () are unnecessary; *cptr is fine.
Careful. You'll want to release this memory in due course by passing the
original pointer value to free() - which means you mustn't forget what that
pointer value was. Use a temp to point to the same place (initially), so
that you can modify the temp rather than cptr.
Simpler: if(++count >= 10) {
if((tptr =(char*)calloc(size*10,sizeof(char)))
== NULL) {
if((tptr = calloc(size * 10, 1)) == NULL) would do it, of course. Now, ten
at a time is one way to do it, I suppose - but there are better strategies.
But more of that at some other time. By the way, look up realloc().
printf("Program Terminating!!");
exit(EXIT_FAILURE);
}
memcpy(tptr,cptr,startpt*10);
free(cptr);
Undefined behaviour - cptr doesn't point to where it used to any more.
Non-standard function.
The parentheses are not required. Just do this: return 0;