End of reading file in c

My program reads the last row of data from the intrusion twice. When I run the program, the last line of data is printed twice. Please, help! Here is the code

while ( !feof ( in ) ) {
//fread();
}

I hope this is due to functionality feof.

I do not want to use fgetsor getline. Is there another way? Please guide me.

Thanks to everyone who answered me! I got a solution for this! I did with fgetcand unfgetcin a loop do.

Here is the code:

int ch;
ch=fgetc(fp);
do
{
ungetc(ch,fp);
//fread();

ch=fgetc(fp);
} while( (ch = fgetc(fp)) != EOF && ch != '\n' );
+3
source share
2 answers

You need to use do ... while to use feof()correctly in c.

if (!feof()) // in case the file is zero length.
{
  do
  {
       //whatever....          
  } while(!feof())
}
+1
source

Try it! Use fscanffor input operations.

while(fscanf(stdin, "%s", in) != EOF) {
//your code
}
+1
source

Source: https://habr.com/ru/post/1793114/


All Articles