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 8421833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:15:34+00:00 2026-06-10T03:15:34+00:00

Using the feof() function, on my output file i have the last value printed

  • 0

Using the feof() function, on my output file i have the last value printed twice. How can i avoid that? My output should be

12
6
15
13

and i have

12
6
15
13
13

Thank you!

while(!feof(pfile1))
{       
    sum=0;
    fscanf(pfile1,"%d%d%d",&choice,&day,&val);
    if(choice==0)
    {
        i=day-1;
        a[i]=a[i]-val;
    }
    else if(choice==1)
    {
        for(i=day-1;i<=val-1;i++)
        {
            sum=sum+a[i];

        }
        fprintf(pfile2,"%d\n",sum);     
    }
}
  • 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-10T03:15:35+00:00Added an answer on June 10, 2026 at 3:15 am

    One option is to check the return value of fscanf:

    On success, the function returns the number of items successfully read. This count can match the expected number of readings or be less -even zero- in the case of a matching failure.
    In the case of an input failure before any data could be successfully read, EOF is returned.

    So, something like:

    if( fscanf(pfile1,"%d%d%d",&choice,&day,&val) <= 0 )
    {
        break;
    }
    

    The other option is to check for feof after executing fscanf. For example:

    if( feof( pfile1 ) )
    {
        return; // or something
    }
    do
    {
        // the same loop body
    }
    while( ! feof( pfile1 ) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this PHP function (using PHP 5.3) that I use to decrypt files,
I have a php function I wrote that will take a text file and
I am using open/read functions, there's feof for fopen/fread to detect end-of-file, what's the
I have a file transfer code written in C using windows sockets which seems
I'm using the fgets function to read a line from a file. I don't
I'm using the following function to open a file: function example() { $foo =
I have a C++ program that will read in data from a binary file
I'm using PHP and I want to upload a text file, with the output
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using Location.getBearing(); I seem to get randomly changing bearings. Aka, I can turn the

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.