Hello everyone,
I'm doing to skip the introduction and just get to the code:
Now, my intention with this code is to print out the file's content twice, once in a loop:Code:#include <stdlib.h> #include <stdio.h> #include <string.h> char *output = NULL; int CharInFile = 0; FILE * Read; // main here. calls readfile after opening file. int readfile (FILE * Read) { int c = 0; int x; do { c = 0; c = fgetc (Read); if (c != EOF) { output = (char *) realloc (output, (CharInFile + 1) * sizeof(char)); output[CharInFile] = c; printf("%c", output[CharInFile]); } CharInFile++; } while (c != EOF); printf("%s\n", output); return 0; }
(This one works fine.)Code:printf("%c", output[CharInFile]);
and the other at the end
The second one doesn't work. It prints out just the newline.Code:printf("%s\n", output);
The reason I want to do this is I want to use string.h for functions such as strstr which don't work with single characters (and no, I'd prefer not to write a make-shift function to make it to). It doesn't need to use realloc, so long as the whole file is loaded.
Thanks in advance.



LinkBack URL
About LinkBacks



