Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8657495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:32:04+00:00 2026-06-12T15:32:04+00:00

I am using fscanf in C++ as follows: Here is my text file: abcd

  • 0

I am using fscanf in C++ as follows:

Here is my text file:

abcd
efgh

There is no space or new line after “efgh”.
Now here is my loop:

FILE* fp;
char eof = 0;
do
{
    char str[20];
    fscanf(fp, "%s", str);
    std::cout<<str<<std::endl;
}
while((eof = fgetc(fp)) != eof)

The output i expect is:

abcd
efgh

But the actual output i get is:

abcd
efgh
efgh

I debugged a found that after reading “efgh” the value read into eof is ‘\n’ and not EOF.
The environment is linux mint.I want to know why always the last string is read 2 times.Please advice

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:32:05+00:00Added an answer on June 12, 2026 at 3:32 pm

    The last string is not being read twice. The problem is the loop’s test for continuing:

    (eof = fgetc(fp)) != eof
    

    This assigns fgetc()‘s return value to eof and checks that it is not equal to eof. Something which is hard to do logically. However, when fgetc() is called when the file is at EOF, it returns -1. That is cast to a char for the assignment, but the sub-expression in parenthesis retains the value -1 (due to type promotion rules). Comparing -1 to 255 or -127 (depending on whether char is signed or unsigned) finally terminates the loop.

    The third time through the loop, fscanf() fails and does not update str: that’s why the same value seems to have been read twice.

    To fix it, the most straightforward technique would be:

    do {
     ...
    } while (!feof (fp));
    

    However, on many operating systems, feof() doesn’t work very well with fscanf() because the end of file indication isn’t reliably set until fscanf() fails. A more reliable, O/S-resistent technique is to use

    do {
        int result = fscanf (fp, ...whatever...);
        if (result < 0)   // end of file or i/o error?
             break;
    } while (!feof (fp));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to parse a text (CSS) file using fscanf and pull out
I've already got some code to read a text file using fscanf() , and
I am trying to read a text file using fscanf.I am working in eclipse
im using the fscanf function to handle input. now in the input each line
I am parsing a text (css) file using fscanf. The basic goal is simple;
I am trying to read in a text file using fscanf. The input file
why when using fscanf to acquire data from a file, is used 2 times,
I am trying to get double values from file using fscanf function. I am
I'm used to using fscanf for simple file input, because it makes it simple.
I have a text file which has first line as below: j0W82LBrSdUbw Basically it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.