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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:42:57+00:00 2026-05-21T07:42:57+00:00

I am trying to input txt files into my C program that look something

  • 0

I am trying to input txt files into my C program that look something like this

123 x 182   //this is a comment in the file
1234 c 1923  //this is another comment in the file
12 p 3      //this is another comment in the file

I need to store the int, the single character and the other int on each line and then I want to ignore everything else on the line. Here is what I tried….

while (fscanf(file, "%d %c %d", &one,&two,&three) !=EOF)
                {
                        printf("%d %c %d\n", one,two,three);
                }

Right now I’m just printing out the values to test the process. So, if I test this with a file that does not have any comments or extra stuff after the first 3 things I need, it works perfectly. But if there is extra stuff, I get stuck in an infinite loop where the first line is repeatedly printed.

  • 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-05-21T07:42:58+00:00Added an answer on May 21, 2026 at 7:42 am

    There may be a better way in C, but you could add a loop inside of your current loop to read in the remaining characters until you hit a newline.

    while (fscanf(file, "%d %c %d", &one,&two,&three) !=EOF)
    {
         printf("%d %c %d\n", one,two,three);
         while(fgetc(file) != '\n'){};
    }
    

    This should break out of the nested while loop as soon as the character it gets is a newline, and the next fscanf will begin on the next line.

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

Sidebar

Related Questions

i'm trying to send fake keyboard input to an application that's running in a
I'm trying to get console input in my Clojure program, but when it gives
Currently my application takes in a text file/files, parses them into another file type
I have a java program that I am trying to generate 3 outputs for,
Trying to write a code that searches hash values for specific string's (input by
I am trying to finish a homework program that compares a string with a
I am trying to write a program for class that reads a list of
I'm trying to make my code be able to separate a file into a
I'm trying to load the following ascii file into MATLAB using load() % some
I'm new to powershell and this question will prove that point. I'm trying a

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.