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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:47:51+00:00 2026-05-29T14:47:51+00:00

The program that I am working on takes a file and parses it line

  • 0

The program that I am working on takes a file and parses it line by line then changes each line into a uint32_t and adds it into an array. From there I am supposed to sort the array with qsort().
I wrote my program how I thought it should be but when I test the code out it is saying that all the new uint32_ts are the same value. Is this because of a mistake when I change the string into the uint32_t? Would it be better to use strtoul?
One last question, is the implementation of my qsort correct? (it compiles and says that it has sorted things but I’m not sure since my conversion to uint_32 is obviously not correct.
Anyways here is the code:

int main(int argc, char* argv[]){
  char const* const fileName = argv[1];
  FILE* file = fopen(fileName, "r"); // should check the result
  char line[256];
  uint32_t parArray[256];
  int compar(const void *a, const void *b){
     const unsigned long long *x = a, *y = b;
     if(*x > *y)
       return 1;
     else
       return(*x < *y) ? -1: 0;
   }
  int lineCounter = 0; // starts at 0 for the array
  while(fgets(line, sizeof(line), file)){
     // parse all info here
     uint32_t t = (uint32_t) line;
     // build the array
     parArray[lineCounter]=t;
     lineCounter++;
     printf("Original: %s, Unsigned Int: %u\n", line,t); 
  }
  qsort(&parArray[0],lineCounter+1,sizeof(uint32_t*),compar);
  int i;
  for(i=0;i<lineCounter;i++){
    printf("%u\n",parArray[i]);
  }
  return 0;
}
  • 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-29T14:47:54+00:00Added an answer on May 29, 2026 at 2:47 pm

    You’re not parsing the lines as you read them. Casting line to a uint32_t simply takes the address of that array in memory. That explains why every line of the output is the same. You probably want to call strtoul(line, NULL, 10) or similar instead.

    Also, your second parameter to qsort is off by one. lineCounter has the correct value at the time the loop terminates: the number of lines in the file. By adding one, you’re introducing undefined behavior by reading past the populated values in the array.

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

Sidebar

Related Questions

Currently I'm working on a program that parses through a file and takes characters
I'm working on a program that takes in a Wavefront .obj file and eventually
I'm working on a program for class that takes in a number from 0
I'm working on a program that takes a redirected file as input. For example,
I'm working on a program that needs to be able to load object-properties from
I'm trying to make a program that takes in one argument, a file, and
So I am working on a C# program that takes in a set of
I've written a working program in Python that basically parses a batch of binary
I'm working on a program that modifies a file, and I'm wondering if the
I'm working on a program that deals with a file that uses hashes. The

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.