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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:33:36+00:00 2026-05-11T03:33:36+00:00

As the title says, I need to write a small program to read data

  • 0

As the title says, I need to write a small program to read data from standard input, sort it, and send it to standard output. The program should take 1 argument that tells it how long is one record (in bytes). Here’s how I test it:

printf 'D\x00C\x00\x00B\x00A' | ./binsort 2 | od -c 

The above should output something like:

0000000  \0   A  \0   B   C  \0   D  \0 0000010 

Here’s what I have so far (binsort.c):

#include <stdio.h> #include <stdlib.h> #include <limits.h>  using namespace std;   void print_usage() {         printf('%s\n', 'Usage: '); }   int compare (const void * a, const void * b) // the compare function for qsort... might need some work {   return ( *(int*)a - *(int*)b ); }   int main(int argc, char *argv[]) {         if (argc != 2 || stdin == NULL) // check for argument and piped input         {                 print_usage();                 exit(EXIT_FAILURE);         }          int entry_size = atoi(argv[1]);          if (entry_size <= 0 || entry_size >= INT_MAX) // check for valid range of entry size         {                 print_usage();                 exit(EXIT_FAILURE);         }          char *input = new char[entry_size]; // to hold single record          while (fgets(input, entry_size, stdin) != NULL)         {                 printf('%s', input); // output single record we just read         }          exit(EXIT_SUCCESS); } 

Then compile with g++ binsort.c -o binsort.

The above compiles but doesn’t output the data that printf sent to it. It should output it in 2-byte chunks… like D\0 C\0 \0B \0A… but it doesn’t.

I’m thinking of using qsort to do the sorting on a malloc/realloc-allocated array. However, I never had experience with these, in effect banging my head on this little utility for a few days. Anyone can help?

P.S. People asked if this is a homework assignment… It’s not – developers at my company want to use this to debug output of their project.

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T03:33:36+00:00Added an answer on May 11, 2026 at 3:33 am

    Don’t use scanf() and printf(). Those are supposed to be used with text data. Since you’re dealing with binary data, you instead want to use the lower-level fread() and fwrite() functions. Since you don’t know how much total data there is, you’ll have to use a dynamic data structure (such as a resizeable array) to store the input. You can’t process the data on-line — you have to read it all in, sort it, then write it back out. Your sort function is also wrong — you’re only comparing the first 4 bytes of each record, which is wrong if the record size is anything other than 4 bytes. Use memcmp() instead.

    This should work:

    char *input = NULL; size_t input_size = 0; int num_items = 0; int entry_size;  int compare_func(const void *e1, const void *e2) {   return memcmp(e1, e2, entry_size); }  int main(int argc, char **argv) {    // ...   char *datum = malloc(entry_size);  // check for NULL   input_size = 4096;   input = malloc(input_size);  // check for NULL    while(1)   {     if(fread(datum, 1, entry_size, stdin) < entry_size)       break;     size_t new_size = (num_items + 1) * entry_size;     if(new_size > input_size)     {       input = realloc(input, input_size * 2);  // check for NULL       input_size *= 2;     }     memcpy(input + num_items * entry_size, datum, entry_size);     num_items++;   }    qsort(input, num_items, entry_size, compare_func);    fwrite(input, entry_size, num_items, stdout);    return 0; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think the xsd needs to be in the class… May 12, 2026 at 12:44 am
  • Editorial Team
    Editorial Team added an answer You could associate each PDF file with a guid in… May 12, 2026 at 12:44 am
  • Editorial Team
    Editorial Team added an answer Use a selector that will select all the rows and… May 12, 2026 at 12:44 am

Related Questions

As the title says I need a way to stop or interrupt a thread
I am writing a Wordpress plugin and need to go through a number of
Here is the sample: Dim TestString As String = Hello, & Chr(0) & World
What the question title says. With a query such as SELECT @@IDENTITY AS ins_id

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.