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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:59:25+00:00 2026-06-14T21:59:25+00:00

I am writing code which very simply reads in a file and prints out

  • 0

I am writing code which very simply reads in a file and prints out what was in the file appropriately.

I have always struggled with getting such a program to terminate upon end of file and think I’ve found the appropriate solution, however each line is printing twice in my output, for a reason beyond me.

Here is my main file:

int main(int argc, char *argv[]) {
    // insure 2 arguments given, one for a.out and one for the test file
    if (argc != 2) {
        // result if request fails
        printf("Requires 2 arguments. Be sure to include test file location\n");
        return 0;
    }

    FILE *fp; //open the file
    fp = fopen(argv[1], "r");

    char option;
    int key;
    int i = 0;
    while (fscanf(fp, "%c %d", &option, &key) != EOF) {
        printf("%d\n", key);
    }
}

The key is printing twice!

Hopefully this is a simple error I’m just overlooking due to overexposure to the problem.

  • 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-14T21:59:26+00:00Added an answer on June 14, 2026 at 9:59 pm

    You probably want:

    fscanf(fp, "%c %d\n", &option, &key);
    

    And you also want to check the return value of fscanf to make sure it equals 2.

    In the first iteration of your loop, the newline is not being consumed.

    In the second iteration, the newline is consumed and put in option, and the %d does not match, and fscanf returns 1. key is unchanged which is why it gets printed again.

    In the third iteration, fscanf finally returns EOF.

    General rule: Always check return values to ensure they are what you expect. (You also violate this rule by failing to check the return from fopen.) At worst it does nothing; at best, it helps you debug problems like this.

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

Sidebar

Related Questions

I am writing a very simple HTML code which is listed below. Written in
I am writing a code which compares file1 (single column of entries) with file
I'm a bit confused. I'm writing a very simple file system that basically: -reads
I'm writing c# code which will interact with the active directory, and my question
I'm writing a code which looks up a MYSQL table, hits an API and
I'm writing some code which could really do with some simple compile time metaprogramming.
I'm writing code in which I use SQL to test several different conditions before
I'm writing some code which involves running a few shell commands from Python and
I am writing a code in which I am trying to open up the
I am writing a code in which the user is asked for username and

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.