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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:44:02+00:00 2026-05-16T23:44:02+00:00

I am parsing a text (css) file using fscanf. The basic goal is simple;

  • 0

I am parsing a text (css) file using fscanf. The basic goal is simple; I want to pull out anything that matches this pattern:

@import “some/file/somewhere.css”;

So I’m using fscanf, telling it to read and discard everything up to a ‘@’ character and then store everything until it reaches a ‘;’ character. Here’s the function that does this:

char* readDelimitedSectionAsChar(FILE *file)
{
char buffer[4096];

int charsRead;
do
{
    fscanf(file, "%*[^@] %[^;]", buffer, &charsRead);

} while(charsRead == 4095);

char *ptr = buffer;
return ptr;
}

I’ve created a buffer that should be able to hold 4095 characters, as I understand it. However, I’m discovering that this is not the case. If I have a file that contains a matching string that’s long, like this:

@import “some/really/really/really/long/file/path/to/a/file”;

That gets truncated to 31 characters using a buffer of char[4096]. (If I use printf to check the value of buffer, I find that the string is cut short.)

If I increase the buffer size, more of the string is included. I was under the impression that one character takes one byte (though I am aware this is affected by encoding). I am trying to understand what’s going on here.

Ideally, I’d like to be able to set the buffer as large as it needs to be “on the fly” — that is, have fscanf just create a buffer big enough to store the string. Can this be done? (I know of the %as flag for GNU, but this is a Mac application for OS 10.5/10.6 and I’m unsure if that will work on this platform.)

  • 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-16T23:44:02+00:00Added an answer on May 16, 2026 at 11:44 pm

    The main problem you have is that you’re returning a pointer to a local buffer on the stack, which is dangling (and so overwritten by the next call you make). You also have a potential buffer overflow.
    You mention the ‘a’ option, which would help a lot, but its unfortunately a GNU extension which isn’t generally available.

    Second, you have this extra option to scanf, &charsRead which will never be written to as there’s no % for it in the format string. So charsRead will always be random garbage — which means you loop will (probably) just run once, or (rarely) loop forever. Try something like

    char* readDelimitedSectionAsChar(FILE *file)
    {
        char buffer[4096], term[2] = "", *rv = 0;
        int len = 0;
    
        fscanf(file, "%*[^@]");
        while (term[0] != ';' && !feof(file)) {
            if (fscanf(file, "%4095[^;]%1[;]", buffer, term) > 0) {
                int read = strlen(buffer);
                rv = rv ? realloc(rv, len+read+1) : malloc(read+1);
                strcpy(rv+len, buffer);
                len += read;
            }
        }
        return rv;
    }
    

    This is still broken in that it will misbehave if you run out of memory (which can easily happen if you feed it a huge malformed file with an @ in the beginning and no ;),

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

Sidebar

Related Questions

I'm having trouble with PHP text parsing I have a txt file which has
I am trying to use FileHelper library for parsing a text file. Ultimately the
I need help in parsing big Json file: {status:{code:200,text:OK},content:[{proposalId:12536,providerId:1,supervisorName:null,supervisorEmail:lolbroek_13@hotmail.com,hostInstitution:qsd,hostCountry:qsd,hostCity:QSD,hostTypeId:1,title:qsd,status:available,numberStudents:null,description:qsd,categoryId:34,startDate:null,endDate:null,submitDate:null,expectedWorkload:null,purpose:null,workOrganisationId:1,function:null,audienceId:1,preConditions:,certificationGranted:null,deliverables:null,expenses:null,revenue:null,vacancies:null,uploadImg:http:\/\/193.136.60.238\/\/MUTW2012\/core\/img\/internship.png}, {proposalId:12537,providerId:1,supervisorName:null,supervisorEmail:lolbroek_13@hotmail.com,hostInstitution:qsd,hostCountry:qsd,hostCity:QSD,hostTypeId:1,title:qsd,status:available,numberStudents:null,description:qsd,categoryId:34,startDate:null,endDate:null,submitDate:null,expectedWorkload:null,purpose:null,workOrganisationId:1,function:null,audienceId:1,preConditions:,certificationGranted:null,deliverables:null,expenses:null,revenue:null,vacancies:null,uploadImg:http:\/\/193.136.60.238\/\/MUTW2012\/core\/img\/internship.png}, {proposalId:12538,providerId:1,supervisorName:null,supervisorEmail:lolbroek_13@hotmail.com,hostInstitution:qsd,hostCountry:qsd,hostCity:QSD,hostTypeId:1,title:qsd,status:available,numberStudents:null,description:qsd,categoryId:34,startDate:null,endDate:null,submitDate:null,expectedWorkload:null,purpose:null,workOrganisationId:1,function:null,audienceId:1,preConditions:,certificationGranted:null,deliverables:null,expenses:null,revenue:null,vacancies:null,uploadImg:http:\/\/193.136.60.238\/\/MUTW2012\/core\/img\/internship.png}, {proposalId:12539,providerId:1,supervisorName:null,supervisorEmail:lolbroek_13@hotmail.com,hostInstitution:qsd,hostCountry:qsd,hostCity:QSD,hostTypeId:1,title:qsd,status:available,numberStudents:null,description:qsd,categoryId:34,startDate:null,endDate:null,submitDate:null,expectedWorkload:null,purpose:null,workOrganisationId:1,function:null,audienceId:1,preConditions:,certificationGranted:null,deliverables:null,expenses:null,revenue:null,vacancies:null,uploadImg:http:\/\/193.136.60.238\/\/MUTW2012\/core\/img\/internship.png}, ................[etc] I
I want to get distance text from following link. i have used NSXML parsing
I have set up an HttpHandler for *css to do some simple parsing: <handlers>
I have a xml file with data <ul> <li><info>CSS text formatting </info></li> <li><info>Text loaded
I have some simple GWT client code that I would like to test using
I've never really done much with parsing text in PHP (or any language). I've
I am currently parsing large text files with Python 2.7, some of which were
I have quick question about text parsing, for example: INPUT=a b c d e

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.