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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:04:45+00:00 2026-06-18T22:04:45+00:00

I have a structure called FOO that’s going to be filled with data from

  • 0

I have a structure called FOO that’s going to be filled with data from a file with columnar data:

JohnDoe 30 60 90 120
JaneDoe 20 40 80 160
...

typedef struct FOO {
char *name;
int  *data;
size_t datasize;

} FOO;

…and a function called fillFOO(FILE *fp) that allocates space for and fills this structure with the data from the file. I’ve been trying things like

formatString = myFormatStringBuilder();

and then passing formatString to sscanf in a pretty obtuse way:

fscanf(fi, formatString, pointerToA_FOO->name, pointerToA_FOO->data[0], 
       pointerToA_FOO->data[1], ...); /* Argh! There has to be an easier way...*/

Is there an easier/cleaner way to read from this file?

  • 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-18T22:04:46+00:00Added an answer on June 18, 2026 at 10:04 pm

    easier/cleaner? This line should not work at all:

    fscanf(fi, formatString, pointerToA_FOO->name, pointerToA_FOO->data[0], ...
    

    because the varargs should be l-values.

    Also, if there are always 5 columns then you dont need myFormatStringBuilder() at all:

    FOO* f = malloc(sizeof(FOO));
    f->name = malloc(100); // or whatever
    f->data = malloc(4*sizeof(int));
    fscanf(fi, "%s %u %u %u %u", f->name, &f->data[0], 
             &f->data[1], &f->data[2], &f->data[3]);
    

    EDIT:
    Ok, so the number of columns is not the same for different files. What you can do is this:
    First find out how many columns are in the file by reading the first line (ie getline), and counting the number of spaces, then malloc that instead of 4, then for each line: fscanf(fi, "%s", f->name); and for(int i=0; i<spaces; ++i) { fscanf(fi, "%u", &f->data[i]); }

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

Sidebar

Related Questions

I have a structure called Patch that represents a 2D array of data. newtype
Another likely easy answer, but if I have a data structure called x.y that
I have a structure called log that has 13 chars in it. after doing
I have a data structure like this: struct foo { int id; int route;
In Java there is a data structure called a WeakHashMap that stores weak references
I have a class called Foo which has a member that is a pointer
I have a table called 'survey_product' with the following structure: id int(11) order_id int(11)
I have a table called 'prices with the following structure: 'id', 'price, 'availability_id' and
So I have a django project I just created called 'coolprojectsite' the directory structure
In C#, there is a type called decimal (the System.Decimal structure). I have found

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.