-2

this was my code to read the content of "file.txt" that contains the following text :

hi farah

slay

and this is my c code :

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int main() {

FILE* fp = fopen("files/file.txt","r");

char str[20];

if (fp == NULL) {

printf("error");

exit(1);

}

while(!feof(fp)) {

fgets(str,5,fp);printf("%s",str);

}

fclose(fp);

return 0;

}

but instead of the file content i get this as an output

hi farah

slayslay

0___________
  • 60,014
  • 4
  • 34
  • 74
rayyya
  • 7
  • 1
  • When `fgets` fails to read into `str`, what is the next thing your program does? – Drew Dormann Sep 02 '23 at 21:05
  • 1
    An extreme example of tag spamming. Deleted not relevant ones. I would start from properly formatting your code (think how you treat us if you even do not put any effort into your question). Then read why while(feof(... is always wrong (use search) – 0___________ Sep 02 '23 at 21:05

0 Answers0