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

The Archive Base Latest Questions

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

I have a text file with data in the form: Lee AUS 2 103

  • 0

I have a text file with data in the form:

Lee AUS 2 103 2 62 TRUE
Check AUS 4 48 0 23 FALSE
Mills AUS 8 236 0 69 FALSE

I need to each line into a struct like, however I’d like to avoid using fixed length arrays (the problem with fgets as far as I can tell):

struct Data
{
    char *sname;
    char *country;
    int *a;
    int *b;
    int *c;
    int *d;
    char *hsisno;
};

I am very new to C. Should I use fscanf, or fgets?

  • 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-17T07:01:34+00:00Added an answer on May 17, 2026 at 7:01 am

    fscanf stands for “file scan formatted” and user data is about as unformatted as you can get.

    You should never use naked "%s" format strings on data where you don’t have absolute control over what can be read.

    The best solution is to use fgets to read a line since this allows you to prevent buffer overflow.

    Then, once you know the size of your line, that’s the maximum size of each string that you will require. Use sscanf to your heart’s content to get the actual fields.

    One final thing. It’s probably a bit wasteful having int* types for the integers, since you know they have a specific maximum size already. I’d use the non-pointer variant, something like:

    struct Data {
        char *sname; char *country;
        int a; int b; int c; int d;
        char *hsisno;
    };
    

    By way of example, here’s some safe code:

    #include <stdio.h>
    #include <string.h>
    
    // Here's all the stuff for a linked list of your nodes.
    
    typedef struct sData {
        char *sname; char *country; char *hsisno;
        int a; int b; int c; int d;
        struct sData *next;
    } Data;
    Data *first = NULL; Data *last = NULL;
    
    #define MAXSZ 100
    int main (void) {
        char line[MAXSZ], sname[MAXSZ], country[MAXSZ], hsisno[MAXSZ];
        int a, b, c, d;
        FILE *fIn;
        Data *node;
    
        // Open the input file.
    
        fIn = fopen ("file.in", "r");
        if (fIn == NULL) {
            printf ("Cannot open file\n");
            return 1;
        }
    
        // Process every line.
    
        while (fgets (line, sizeof(line), fIn) != NULL) {
            // Check line for various problems (too short, too long).
    
            if (line[0] == '\0') {
                printf ("Line too short\n");
                return 1;
            }
    
            if (line[strlen (line)-1] != '\n') {
                printf ("Line starting with '%s' is too long\n", line);
                return 1;
            }
    
            line[strlen (line)-1] = '\0';
    
            // Scan the individual fields.
    
            if (sscanf (line, "%s %s %d %d %d %d %s",
                sname, country, &a, &b, &c, &d, hsisno) != 7)
            {
                printf ("Line '%s' didn't scan properly\n", line);
                return 1;
            }
    
            // Allocate a new node to hold data.
    
            node = malloc (sizeof (Data));
            if (node == NULL) {
                printf ("Ran out of memory\n");
                return 1;
            }
    
            node->sname = strdup (sname);
            node->country = strdup (country);
            node->a = a;
            node->b = b;
            node->c = c;
            node->d = d;
            node->hsisno = strdup (hsisno);
            node->next = NULL;
            if (first != NULL) {
                last->next = node;
                last = node;
            } else {
                first = node;
                last = node;
            }
        }
    
        fclose (fIn);
    
        // Output the list for debugging.
    
        node = first;
        while (node != NULL) {
            printf ("'%s' '%s' %d %d %d %d '%s'\n",
                node->sname, node->country, node->a, node->b,
                node->c, node->d, node->hsisno);
            node = node->next;
        }
    
        return 0;
    }
    

    which reads in your file and stores it in a linked list. It outputs:

    'Lee' 'AUS' 2 103 2 62 'TRUE'
    'Check' 'AUS' 4 48 0 23 'FALSE'
    'Mills' 'AUS' 8 236 0 69 'FALSE'
    

    at the end, as expected.


    I’ve done a whole series of answers on the pitfalls of using *scanf functions on non-controlled data (enter user:14860 fgets into the search box above), some of which (here, here and here, for example) include a perennial favourite function of mine, getLine, for safer user input.

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

Sidebar

Related Questions

I have a simple html form with two controls: input-text and input-file I need
I have a method that saves form controls data to a text file, including
Suppose I have a text file with data separated by whitespace into columns. I
I'm probably doing this all wrong. I have a text file full of data
I have to parse a tab-delimited text file with Ruby to extract some data
If I have a list of data in a text file seperated by a
I have a .rc file which is used to include some text data in
I have a text file with Tag - Value format data. I want to
I am trying to write data from form inputs to a text file using
I have a text file on my local machine that is generated by 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.