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

  • Home
  • SEARCH
  • 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 75685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:33:17+00:00 2026-05-10T20:33:17+00:00

I have to read a txt file with lines formated like this: 1: (G,

  • 0

I have to read a txt file with lines formated like this:

 1: (G, 2), (F, 3) 2: (G, 2), (F, 3) 3: (F, 4), (G, 5) 4: (F, 4), (G, 5) 5: (F, 6), (c, w) 6: (p, f), (G, 7) 7: (G, 7), (G, 7) w: (c, w), (c, w) 

Each line will feed a struct with its data (the 5 numbers or letters in it).
What’s the best way to read the line and get the strings I want?
I’m currently using a long sequence of conditions using fgetc but that seems ugly and not very smart.
I can’t use arrays because the lines may vary in size if the numbers have two digits.

  • 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. 2026-05-10T20:33:18+00:00Added an answer on May 10, 2026 at 8:33 pm
    #include <stdio.h>  int main (void) {   char buf[81];       /* Support lines up to 80 characters */   char parts[5][11];  /* Support up to 10 characters in each part */    while (fgets(buf, sizeof(buf), stdin) != NULL)   {     if (sscanf(buf, '%10[^:]: (%10[^,], %10[^)]), (%10[^,], %10[^)])',                parts[0], parts[1], parts[2], parts[3], parts[4]) == 5)     {       printf('parts: %s, %s, %s, %s, %s\n',              parts[0], parts[1], parts[2], parts[3], parts[4]);     }     else     {       printf('Invalid input: %s', buf);     }   }   return 0; } 

    Sample run:

    $ ./test 1: (G, 2), (F, 3) 2: (G, 2), (F, 3) 3: (F, 4), (G, 5) 4: (F, 4), (G, 5) 5: (F, 6), (c, w) 6: (p, f), (G, 7) 7: (G, 7), (G, 7) w: (c, w), (c, w) parts: 1, G, 2, F, 3 parts: 2, G, 2, F, 3 parts: 3, F, 4, G, 5 parts: 4, F, 4, G, 5 parts: 5, F, 6, c, w parts: 6, p, f, G, 7 parts: 7, G, 7, G, 7 parts: w, c, w, c, w 

    If the last value in the input is more than 10 characters it will be truncated with no indication of error, if this is not acceptable you can use the %c conversion specifier as a sixth argument to capture the next character after the last value and make sure it is a closing parenthesis.

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

Sidebar

Ask A Question

Stats

  • Questions 59k
  • Answers 59k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Same as I would for numbers. I'd declare constants of… May 11, 2026 at 8:58 am
  • added an answer There are probably a few minor performance improvements and bug… May 11, 2026 at 8:58 am
  • added an answer Not that I know of, but the short-tag plus the… May 11, 2026 at 8:58 am

Related Questions

I have to read a txt file with lines formated like this: 1: (G,
I have to read a binary file in a legacy format with Java. In
I have been trying to read a picture saved in Access DB as a
I have to read invoice ascii files that are structured in a really convoluted
I'm given a filename and I have to be able to read it from
I have to write a program that read from a file that contains the
I have a small text file that I'd like to read into a scalar
I have a requirement to read and display the owner of a file (for
Imagine I have some code to read from the start to end of a
I have a .NET dll which needs to read it's config settings from it's

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.