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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:15:09+00:00 2026-06-13T17:15:09+00:00

I was experimenting with writing a program that would reverse the contents of a

  • 0

I was experimenting with writing a program that would reverse the contents of a file.
So, giving the inputfile with the content “abc” it should make a file with a content “cba”.

Unfortunately, it doesn’t work and I don’t understand why.

Could you guys please help me?
Thanks

EDIT: i forgot to mention that it was a school assignment – and we have to use functions like lseek and open – Please dont posr me that I should’ve used fgetc anfd other functions 🙂

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

void reverse_file(char * in, char * out)
{
    int infile, outfile;
    infile = open(in, O_RDONLY);
    outfile = open(out, O_WRONLY);

    char buffer;
    char end = EOF;
    write(outfile, &end, sizeof(char));
    do
    {
        // seek to the beginning of a file

        read(infile, &buffer, sizeof(char));


        // printf("the code of a character %d\n", buffer); // returns 10 instead of EOF

        lseek(outfile, 0, SEEK_SET);
        write(outfile, &buffer, sizeof(char));

    } while (buffer != EOF);
    close(outfile);
    close(infile);


}

int main()
{
    reverse_file("tt", "testoutput");

    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-06-13T17:15:11+00:00Added an answer on June 13, 2026 at 5:15 pm

    Your current code (outside the fact that the test for the end of file is wrong), will make a file of one char, because write overwrite the data present in the file at the current position (unless it’s at the end, where it would append).
    Actually, to reverse the file, you should read it starting from the end.

    struct stat instat;
    int pos;
    
    fstat(infile, &instat);
    pos = instat.st_size - 1;
    do
    {
        // seek backward in the input file, starting from the end
        lseek(infile, SEEK_SET, pos);
        read(infile, &buffer, sizeof(char));
        write(outfile, &buffer, sizeof(char));
    
    } while (pos-- > 0);
    

    (Reading char by char is very ineficient with the unix read and write system calls, so as a second step, you should consider using the C primitives (fopen, fread, fwrite), or do some buffered reads and writes with the unix system calls.)

    See:

    • open
    • read
    • write
    • lseek
    • fstat
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been experimenting with writing applications that use a local SQL Database to
I'm experimenting with WeakReference, and I'm writing a code that checks if a weak
I am currently writing a program in Java that will accept strings from PHP
I'm experimenting with writing ActiveX controls and noticed that I can't seem to create
I'm experimenting with the Google Earth Plugin API for an application that I'm writing.
I'm writing code that deals with a file that uses hashes. I need to
Hl Guys, I am busy writing a backend administrative program for a system that
I am writing a program that needs to be able to work with text
http://jsfiddle.net/Codemonkey/ye5qv/2/ I am experimenting with writing large workloads to recur using setInterval so that
I am experimenting with writing a toy compiler in ocaml. Currently, I am trying

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.