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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:09:58+00:00 2026-05-28T04:09:58+00:00

Update : Properly initialising string with char string[sizeof buffer – 1] has solved the

  • 0

Update: Properly initialising string with char string[sizeof buffer - 1] has solved the crashing problem, but I’m still curious as to what having more than one punctuation mark had to do with it!

I am trying to read a string from a file in the form “some text”. Using sscanf with the pattern \"%[^\"]\" has worked perfectly for me so far, but as soon as I started adding punctuation to the string the program has started crashing.

It seems that the error only occurs if more than one punctuation mark is used, regardless of what punctuation mark it is, or the mark’s position. It also occurs regardless of the position of lines with punctuation in the file (ie. even if last line has no punctuation the error still occurs).

Anyway, below is the code I have so far:

char* func(char* f_name);
    FILE* file = get_file(f_name,"r"); // a short function I wrote to get the
                                       // file pointer from the current
                                       // directory. The error is almost
                                       // certainly not here.
    if (file == 0) {
        print("Unable to load file\nExiting...");
        exit(-1);
    }

    char* pattern = "\"%[^\"]\"";
    int read_args = -1;
    char* string; //  string size is unknown until read
    char buffer[1200]; // i expect very long line of data in the file

    while ( fgets( buffer, sizeof(buffer), file ) != NULL ) {
        printf("found line: %s\n",buffer);
        read_args = sscanf(buffer, pattern, string);
        printf("num args: %d\n",read_args);
        printf("read value: %s\n", string);
    }

    fclose(file);
    return string;
}

The following are some data I have tried. Where marked ‘unsuccessful’ the program compiles, runs everything, and the crashes just before exiting.

"test test test" // successful
"test, test test" // successful
"test test; test" // successful

"test, test, test" // unsuccessful
"test; test. test," // unsuccessful

I plan to use a more complex pattern if this problem is resolved, and was reading data with the pattern %d \"%[^\"]\" \"%[^\"]\" successfully before this error occurred. Thanks in advance for any answers.

  • 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-28T04:09:59+00:00Added an answer on May 28, 2026 at 4:09 am

    Your code invokes undefined behavior, since you’re scanning into random memory.

    The string variable is never initialized, so the destination for the string written by sscanf() is undefined.

    You need e.g.

    char string[sizeof buffer - 1];
    

    The fact that it doesn’t always crash is just luck, the code is invoking undefined behavior all the time. It might just have been that with punctuation, based on your examples, the scanned text was longer and thus overwrote more memory, eventually triggering the crash once it hit something important enough.

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

Sidebar

Related Questions

Just curious to know, Test module won't work properly with django 1.2.3 when update
Update: This issue was not properly explored. The real issue lies within render :json
I have a string dependency property (SearchText), when updated, needs to update a collection
Update: Solved, with code I got it working, see my answer below for the
UPDATE I was able to get everything to work properly and I just wanted
UPDATE 1: I haven't figured out what's going on, but this definitely seems to
I update the text of my TextSwitcher in the code of setViewValue. But in
I'm trying to get a nested form view to update properly. This is however
For the last few days I'm trying to properly update my POCO entities. More
Update I have posted my solution to this problem as an answer below. It

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.