the following code i wrote is supposed to open a file given as an input, write to it and the read.
- fopen() works properly and i have access to the file.
- fprintf() also works as expected.
but as to fgets -if i use the if command as shown the condition is true, and if i dont i get that input[0] is the '\n' character while input[1] is 'h', and the loop runs without stopping since fgets() keep reading the first char again and again.
also, it seems like fgets() does not advance and has read all of the file into input - i can print input[3] and get 'l' as expected, although fgets() is ordered to read only 2 chars.
int main(int argc, char *argv[])
{
FILE* read = NULL;
read = fopen(name, "a+");
char* input = "";
fprintf(read, "hello world\n");
fprintf(read, "hello world\n");
assert(ferror(read) == 0);
while(!feof(read))
{
if(fgets(input, 2, read)==NULL)
return 0;
printf("%c\n", input[1]);
}
return 0;
}