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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:28:10+00:00 2026-06-02T21:28:10+00:00

I am trying to read in a text file using fscanf. The input file

  • 0

I am trying to read in a text file using fscanf. The input file looks like this:

1
Test
0
32.1

Everything is read fine except the first integer which gets read as some random value.

My code:

while((fgets(string,64,fp) != NULL)){
    fscanf(fp, "%i\n", &tid);
    fscanf(fp, "%s\n", name);
    fscanf(fp, "%d\n", &stat);
    fscanf(fp, "%f\n", &per);
    printf("%d %s %d %f", tid, name, stat, per);
    }

Output:

11281472 Test 0 32.0999982

Does anyone know what I do wrong?

Entire function for reference:

Task *readData(Task *strt, char *fname){
    #ifdef DEBUG
        fprintf(stderr, "Entered Data import method\n");
    #endif
    char name[30];
    int tid, stat;
    float per;

    FILE *fp;
    fp= fopen(fname, "r");

    if(fgetc(fp) == EOF){
        printf("File is empty");
    }
    else{
    while(!feof(fp)){

        fscanf(fp, "%i\n", &tid);
        fscanf(fp, "%s\n", name);
        fscanf(fp, "%d\n", &stat);
        fscanf(fp, "%f\n", &per);
        printf("%d %s %d %f", tid, name, stat, per);
        strt = AddB(strt, tid, name, stat, per);
        }
    }
    return (strt);
}
  • 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-02T21:28:18+00:00Added an answer on June 2, 2026 at 9:28 pm

    fgets does a reading (consuming) of first string in the input file and then first fscanf tries to parse “Test” as “%i” format and fails.

    You may use feof as condition in while loop, but (thnx to John Bode) it is better to check every fscanf and use a flag or break.

    Every fscanf returns a value. As stated in 1:

    RETURN VALUE

    Upon successful completion, these functions shall return the number of successfully matched and assigned input items; this number can be zero in the event of an early matching failure. If the input ends before the first matching failure or conversion, EOF shall be returned. If a read error occurs, the error indicator for the stream is set, EOF shall be returned, and errno shall be set to indicate the error.

    So, you may want to check the return value of every fscanf to know that something goes (reads) wrong.

    UPDATE:
    You should never use fgets/fgetc to check something (without rewinding back with fseek or undoing with ungetc), because every file read function will CONSUME data from file (current position in file will be incremented). To check errors, just look at return value of usual fscanf/other function you used to parse input.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read a text file using jquery, like this: // LOAD
I'm trying to read a text file that looks like this 0,-16,-4,12,10,4,-14,8,44,8,8,12,-4 1,-16,-4,12,10,4,-14,6,43,10,10,12,-4 2,-16,-6,10,11,2,-13,4,43,10,11,12,-4
I am trying to read a text file using fscanf.I am working in eclipse
I'm trying to read just the integers from a text file structured like this....
I am trying to read values from a text file using the Qt code
I'm trying to read the contents of a small text file using the common
I am trying to read some info from a text file by using windows
I am trying to read in a text input file that contains a list
Hey, where do I place a text file that I'm trying to read using
I'm trying to read from a text file using the read function and store

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.