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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:27:04+00:00 2026-06-17T20:27:04+00:00

strtol wont convert my string i got from reading a text file. int getNumFiles(int

  • 0

strtol wont convert my string i got from reading a text file.

int getNumFiles(int archive){
off_t cur=lseek(archive,20,SEEK_SET);
if(cur==-1){
    printf("lseek ERROR\n");
    exit(-1);
}
bool b=true;
char headerSizeBuffer[4];
char *end;

while(b){
    int numRead=read(archive,headerSizeBuffer,3);
    if(numRead != 3){
        printf("read ERROR\n");
        exit(-1);
    }
    headerSizeBuffer[3]='\0';
    printf("headerSizeBuffer=%s  with length=%ld\n",headerSizeBuffer,strlen(headerSizeBuffer));
    long headerSize=strtol(headerSizeBuffer,&end,10);//atol(headerSizeBuffer);
    printf("headerSize=%ld\n",headerSize);

    if (!*end)
        printf("Converted successfully\n");
    else
        printf("Conversion error, non-convertible part: %s\n", end);
    b=false;
}

return 1;

}

the console give me this when i run the compiled code

headerSizeBuffer=031l_archive  with length=12
headerSize=31
Conversion error, non-convertible part: l_archive

all i want to do is convert 031 into a long or int with the value 31.

  • 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-06-17T20:27:06+00:00Added an answer on June 17, 2026 at 8:27 pm

    Odds are that you didn’t allocate space, or enough space, for headerSizeBuffer and it is getting overwritten some time between (the start of) the call to printf and (the end of) the call to strtol.

    Edit: From the comment above: “char* headerSizeBuffer”

    Yup, I was right. Since you haven’t allocated any space you have undefined behavior … which means that any behavior might occur, including what you’re seeing. Be sure that you allocate enough space to headerSizeBuffer, either by setting it to point to enough memory (that could be from malloc, or it could be an array, or various other less common means) or, more appropriate to your case because the size needed is known at compile time, declaring it as an array rather than a pointer:

    char headerSizeBuffer[4];
    

    Be sure to set headerSizeBuffer[3] = ‘\0` to terminate the string before passing it to strtol.

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

Sidebar

Related Questions

I found the code to convert a hexadecimal string into a signed int using
I want to parse an int from a string ( char* ) in C.
I'm trying to convert this string 09195462965 to an int but I'm running into
I try to read a number from a string with strtol() and it returns
I read that atoi() is deprecated and that it is equivalent to: (int)strtol(token_start, (char
I want to convert number from char * format to 32 bit integer int32_t,
Here's the contents of a text file: SQUARE 2 SQUARE RECTANGLE 4 5 I'm
I was making some proves with strtol() from stdlib library because i had a
I have developed a mini-shell that accepts text from stdin. It works so far,
I need to convert String^ type to hexadecimal string representation. I'm coding on MSVC++'05.

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.