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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:06:21+00:00 2026-06-07T23:06:21+00:00

I need to write a program which reverses the order of the bytes in

  • 0

I need to write a program which reverses the order of the bytes in a given binary file. It accepts the file name in the command line. In addition it can use file positioning functions such as fseek no more than a fixed number of times.

Here is a code which I wrote which does not use it a fixed number of times:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char** argv) {

    if (argc>2) {
        printf("Please enter a valid file name");
        return 1;
    } else {
        FILE* file;
        file=fopen(argv[1], "r");
        if (file==NULL) {
            printf("Please enter a valid file name");
            return 1;
        } else {
            FILE* fileBackUp;
            fileBackUp=fopen("c:\backupFile.txt", "w+");
            fseek(file, 0, SEEK_END);
            fseek(file, -1, SEEK_CUR);
            while (ftell(file)>=0) {
                int c= fgetc(file);
                fputc(c, fileBackUp);
                fseek(file, -2, SEEK_CUR);
            }

            fseek(fileBackUp, 0, SEEK_SET);

            int c;
            while (!feof(fileBackUp)) {
                c=fgetc(fileBackUp)
                fputc(c,file);
            }

            fclose(fileBackUp);
            fclose(file);
        }
    }
    return 1;
}

It uses an extra file for it. I surely believe that there’s a shorter elegant way to do that with a fewer steps as requested. Any suggestions?

Another thing: It seems that the first condition is always being filled, how come?

  • 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-07T23:06:22+00:00Added an answer on June 7, 2026 at 11:06 pm
    #include <stdio.h>
    
    long max(long v1, long v2) { return v1 >= v2 ? v1 : v2; }
    long min(long v1, long v2) { return v1 >= v2 ? v2 : v1; }
    void invert_bits(char *arr, size_t size);
    
    int main(int argc, char *argv[]) {
      size_t  read_sz;
      FILE * infile = fopen(argv[1], "r");
      fseek(infile, 0, SEEK_END);
      long file_sz = ftell(infile);
      long offset  = file_sz;
      long total_read = 0;
      long num_seeks  = 10;
      size_t  buffer_sz = (file_sz + num_seeks - 1) / num_seeks;
      char    buffer[buffer_sz];
      while (file_sz > total_read) {
        if ((offset + num_seeks - 1) / num_seeks < buffer_sz) {
          buffer_sz = offset / num_seeks;
        }
        offset = max(0, offset - buffer_sz);
        fseek(infile, offset, SEEK_SET);
        read_sz = fread(buffer, 1,
            min(buffer_sz, file_sz - total_read), infile);
        total_read += read_sz;
        invert_bits(buffer, read_sz); 
        num_seeks--;
      }
      fclose(infile);
    }
    
    void invert_bits(char *arr, size_t size) {
      size_t i;
      int j;
      for (i = size; i > 0; i--) {
        char v = arr[i - 1];
        char o = 0;
        for (j = 0; j < 8; j++) {
          o |= v & 1;
          v >>= 1;
          o <<= 1;
        }
        printf("%c", o);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a java program which when executed pushes a command into
I need to write a program in BSD sockets which behaves like a file
i need to write a program in which main() would read a file(containing some
I have been given a project where i need to write a program which
I need to write a small program which to test whether a line (position
I need to write one java program which monitors a folder containing excel sheets
I need to write an I2C program which can write data to 32 bit
I need to write a program that takes input line at a time and
I need to write a program which will write many characters in a output
I need to write a program which does 2 tasks at the same time

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.