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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:16:36+00:00 2026-05-27T17:16:36+00:00

Today I’ve looked over some C code that was parsing data from a text

  • 0

Today I’ve looked over some C code that was parsing data from a text file
and I’ve stumbled upon these lines

fgets(line,MAX,fp);
if(line[strlen(line)-1]=='\n'){
    line[strlen(line)-1]='\0');
}else{
    printf("Error on line length\n");
    exit(1);
}
sscanf((line,"%s",records->bday));

with record being a structure

typedef struct {
    char bday[11];
}record;

So my question here regards the fgets-sscanf combination to create a type/length safe stream reader:

  • Is there any other way to work this out beside having to combine these two readers?

  • What about the \n checking-removing sequence?

  • 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-27T17:16:37+00:00Added an answer on May 27, 2026 at 5:16 pm

    The combination of fgets() with sscanf() is usually good. However, you should probably be using:

    if (fgets(line, sizeof(line), fp) != 0)
    {
        ...
    }
    

    This checks for I/O errors and EOF. It also assumes that the definition of the array is visible (otherwise sizeof gives you the size of a pointer, not of the array). If the array is not in scope, you should probably pass the size of the array to the function containing this code. All that said, there are worse sins than using MAX in place of sizeof(line).

    You have not checked for a zero-length birthday string; you will probably end up doing quite a lot of validation on the string that is entered, though (dates are fickle and hard to process).

    Given that MAX is 60, but sizeof(records->bday) == 11, you need to protect yourself from buffer overflows in the sscanf(). One way to do that is:

    if (sscanf(line, "%10s", records->bday) != 1)
        ...handle error...
    

    Note that the 10 is sizeof(records->bday) - 1, but you can’t provide the length as an argument to sscanf(); it has to appear in the format string literally. Here, you can probably live with the odd sizing, but if you were dealing with more generic code, you’d probably think about:

    sprintf(format, "%%%zus", sizeof(records->bday) - 1);
    

    The first %% maps to %; the %zu formats the size (z is C99 for size_t); the s is for the string conversion when the format is used.

    Or you could consider using strcpy() or memcpy() or memmove() to copy the right subsection of the input string to the structure – but note that %10s skips leading blanks which strcpy() et al will not. You have to know how long the string is before you do the copying, of course, and make sure the string is null terminated.

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

Sidebar

Related Questions

Today I was reading code from some very popular numerical libraries written in FORTRAN
Today while I was writing some code for two methods that shows and hides
Today at work we came across the following code (some of you might recognize
Today I received an email, which announced that a service will be migrated from
today I stumbled upon a very interesting case (at least for me). I am
Today I discovered that my fresh installation of Apache HTTP Server is able to
Today I had a coworker suggest I refactor my code to use a label
Today I stumbled about a Problem which seems to be a bug in the
Today somebody told me that interface implementation in C# is just Can-Do relationship, not
Today I use SQLite and send a database-file with the project. However I want

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.