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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:39:16+00:00 2026-05-30T07:39:16+00:00

looking for a way to read arguments from a configuration file I found a

  • 0

looking for a way to read arguments from a configuration file I found a nice way to do it but the thing is that something really weird happens when I try to count the lines.

This is the code:

FILE *file = fopen("config", "r");

char line[100];
int linenum = 0;
//int foo;  Uncomment and it starts to working, doesn't matter if you rename it.

while(fgets(line, sizeof(line), file) != NULL) {
    char option[4];
    char arg[100];

    if (line[linenum] == '#') 
        continue;
    linenum++;

    if (sscanf(line, "%s %s", option, arg) != 2)
        fprintf(stderr, "Syntax error, line %i\n", linenum);

The config file looks like this:

#config file
option1
option2
option3

So the result is:

Syntax error, line 1
Syntax error, line 0
Syntax error, line 0

But if I declare an int variable with any name before while loop it starts to working!

The result is:

Syntax error, line 1
Syntax error, line 2
Syntax error, line 3

What in the world is happening here? my mind is gonna blow, maybe it’s something dumb but I don’t see any reason for this.

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

    Any time you see strange behavior like this, you should start looking for memory overrun errors. Typical symptoms include:

    1. Code that works or stops working if you add or remove unused variables.
    2. Variables that change for no apparent reason.
    3. Code that sometimes runs normally and crashes other times, using the same input.
    4. Code that produces different results between invocations, using the same input.

    In this case, as the other answers point out, you have two potential memory-related errors:

    The line if (line[linenum] == '#') continue;

    should be if (line[0] == '#') continue;

    Otherwise the index will be invalid when linenum > 99.

    And the line if (sscanf(line, "%s %s", option, arg) != 2){

    will overrun the option and arg variables if the input strings are too long. (The error you’re seeing occurs because the text (e.g. option1) is longer than 4 characters and overflows option. You can fix this in several ways:

    • Increase the size of option. You’ll need to know the maximum length of the input strings in order to choose an appropriate size.
    • Limit the number of characters that are copied into option:
      if (sscanf(line, "%3s %99s", option, arg) != 2){
      This may cause unexpected output for long input strings, because arg will contain the remainder of the string in option if it’s truncated.
    • According to http://linux.die.net/man/3/sscanf you can use the a modifier to tell sscanf to allocate memory for the strings as it reads them:
      char **str1, **str2;
      if (sscanf(line, "%as %as", &str1, &str2) != 2){
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a way to read from an external text file map.txt (allocated
I am looking to see a simple way to read from and write to
Looking for a way to read the following config file sample using a multi
I'm trying to read-in a bunch of unsigned integers from a configuration file into
Is there a way to read a file's data but continue reading the data
I am looking for a way to read an input file and print only
I am looking for a way to read the ID3 tags from an MP3
I'm looking for a way if you know the location where to read the
Please note - I am not looking for the right way to open/read a
I'm looking for a faster way to calculate GC content for DNA strings read

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.