0

in the week 4 shorts, it was told how we can write cat command from linux in c: i Wrote the following code:

#include <stdio.h>

include

void Print_File(FILE *f) {

char s = fgetc(f);
while (s != EOF)
{
    printf("%c", s);
    s = fgetc(f);
}

}

int main(void) {

FILE *ptr = fopen(info.txt, "a");
if(ptr == NULL)
{
    printf("Invalid Input\n");
    return 1;
}
char *c = malloc(sizeof(char) * 101);
printf("One entry cannot be more than 100 characters long!\n");
printf("Enter your text here - ");
scanf("%[^\n]%*c", c);
fprintf(ptr, "%s\n", c);
Print_File(ptr);
fclose(ptr);
free(c);

}

the file is updated alright, but the file is not printed! Can anyone point out what's wrong with the Print_File() function?

0

After the fprintf, the file pointer is where? At the end of the file. Which implies that s will be EOF on the first char s = fgetc(f);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .