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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:09:09+00:00 2026-06-05T07:09:09+00:00

I have a file that I’m trying to read and fill variables with. The

  • 0

I have a file that I’m trying to read and fill variables with. The file consists of this:

0\ttake a nap\n
1\tstudy heap-based priority queue\n
101\treview trees for Midterm 2\n
3\tdo assignment 7\n

This may be hard to read, but you can see that there is an integer to begin with, followed by a tab, a string after that, followed by a newline. I need to take the integer and put that into a variable, detect the tab, and put the string following the tab into a variable, detect the newline, take the two variables and create a node with the information, and then start over again on the next line. After hours of scouring the internet, this is what I’ve come up with:

char activity[SIZE];
 char position[SIZE];
 char line[100];

  FILE *infile;
  char *inname = "todo.txt";

  int i = 0;


  infile = fopen(inname, "r");
  if (!infile) {
    printf("Couldn't open %s for reading\n");
    return 0;
  }
while(i < 100 && fgets(line, sizeof(line), infile) != NULL){
        sscanf(line, "%s\t%s", position, activity);
        printf("%s\n", position);
        printf("%s\n", activity);
        i++;
    }

When running this test code on the txt file above, I get this as a result:

0
take
1
study
101
review
3
do

So, it looks to me like it’s getting the first number alright (as a string) and putting it into the variable, seeing the tab, and grabbing the first sequence after the tab and stopping there after putting it into the other variable. How do I rectify this situation?

  • 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-05T07:09:10+00:00Added an answer on June 5, 2026 at 7:09 am

    You can try changing the sscanf:

    sscanf(line, "%s\t%[^\n]", position, activity);
    

    The %s specifier stops when it encounters blanks. That’s why it only reads study instead of study heap-based priority queue. The %[^\n] tells it: "read until newline". Another issue: you should test the value returned by sscanf to make sure it filled the required number of objects.


    You could also read the first integer as an integer, changing position to int and using %d instead of %s.

    EDIT

    To make myself clear, what I was suggesting was:

    int position;
    sscanf(line, "%d\t%[^\n]", &position, activity);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file that contain lines that looks like this: >AF001546_1 [88 -
I have a file that is similar to this: <many lines of stuff> SUMMARY:
I have a file that contain list of numbers that looks like this: 10^-92
I have a file that I read into a char array. The char array
I have a file that reads like this 2012-021 24X7 Online Tutoring Company Applied
I have a file that looks kind of like this: junk stuff NAME Test
I have a file that require()'s a namespace, as such: <?php require_once('Beer.php'); // This
I have file that consists various Unicode characters. I want to convert all those
i have a file that looks like this: sdfkhsdf,sdfgsg,sdfsdf,sdsdf sdfsdf,43534,sdfsdf,24234,sfddsf efsdf,234234,sdfsdf,234234,sadfsdf i need to
i have xml file that looks like this: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE pointList SYSTEM

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.