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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:52:04+00:00 2026-06-18T17:52:04+00:00

to read strings from a file and print the sorted output using qsort. So

  • 0

to read strings from a file and print the sorted output using qsort. So I write something like this:

int main()
{
  int n=0;
  int size=1;
  File *fp = fopen(args[0],"r");
  int c;
  char* inputFile;
  inputFile = char* malloc(size);
  if(fp==0){
    fprintf(stderr, "Cannot open file!\n");
    return -1;
  else{
    do{
      c = fgetc(fp);
      if(size==1){
        inputFile[n]=c;
      }
      else{
        inputFile = char* realloc(inputFile, size+1);
        inputFile[n]=c;
      }
      n++;
      size++;
  }while(c!=EOF);
  qsort(inputFile, 1, size, compare);//I have implement the compare function correctly
  n=0;
  while(n<size){
    while(input[n]!='\0'){
       printf ("%d ",inputFile[n]);
       n++;
    }
    n++;
  }
  return 0;
}

So, if the input file is ‘\0vaaa\n\0ba\0\nabc’, the program should output print:

abc
ba
vaaa

However, my code isn’t working at all. I have check that the compare method return the correct result. Additionally, I just wonder if I implement the malloc-realloc correctly? Thx

  • 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-18T17:52:05+00:00Added an answer on June 18, 2026 at 5:52 pm

    You want to print strings, so replace:

    printf ("%d ",inputFile[n]);
    

    with

    printf ("%s ",inputFile[n]);
    

    But you may have other problems in your code..

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

Sidebar

Related Questions

How do I read connection strings from custom config file (say abc.config) using WebConfigurationManager
I'm using this code to read the connection string from my app.config file but
I need to read in an Intel Hex file which looks something like this:
The following code should read 5 strings from a .dat file and then print
I am trying to read strings from a file that has each string on
How to read strings from the MyResource.resx file from c#. I am not calling
I'm trying to read a list of strings from a file to an array.
I'm trying to find and read a string from a text file using a
I need to read in an expression from a file using a string stream
I'm trying to write a program that manipulates unicode strings read in from a

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.