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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:01:45+00:00 2026-05-16T17:01:45+00:00

This a code that would reverse the data of a document and save it

  • 0

This a code that would reverse the data of a document and save it in the same document itself.
However I am getting a Segmentation Fault.Please Help,I don’t know why it gives a SegFault.

#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>


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

    int fd,n,i,j;

    char* buf;

    if(argc<2)
    printf("USAGE: %s file-to-reverse.\n",argv[0]);

    fd=open(argv[1], O_RDWR);
    if(fd==-1)
    printf("ERROR: Cannot reverse %s,file does not exist.\n",argv[1]);

    i = 0;
    j = n-1;

    while(i < j)
    {   
        read(fd,buf,n);

        char ib = buf[i];
        char jb = buf[j];

        jb = i++;
        ib = j--;

        write(fd,buf,n);
    }

    free(buf);
    close(fd);
}

EDIT1
I tried adding :

#include <sys/stat.h>
struct stat fs;


  fstat(fd, &fs);
  n= fs.st_size;
  buf = malloc(n * sizeof (char));

but now it just duplicates the characters inside the document again and again instead of
reversing them.

  • 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-16T17:01:46+00:00Added an answer on May 16, 2026 at 5:01 pm

    Regarding your second EDIT – your loop is wrong.

    (1) Take the read & write out of the loop – that’s why it keeps writing again & again.

    (2) You need to seek back to the beginning of the file, otherwise you will just be appending the new data to the end of the file.

    (3) You actually have to reverse the chars in the buffer before writing them out.

    read(fd, buf, n);
    
    while (i < j)
    {
    
        char t = buf[i];
    
        buf[i] = buf[j];
    
        buf[j] = t;
    
        i++;
        j--;
    }
    
    lseek(fd, 0, SEEK_SET);
    
    write(fd, buf, n);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was given this Python code that would calculate an MD5 value for any
I have this code that where I would normally use one line: if (tableView
I have trouble finding way to correctly refactor this code so that there would
I have this source code from 2001 that I would like to compile. It
I came across this code on reddit . I would have thought that type
Given this code, tableButton = new JButton(new ImageIcon(80F_FG2015.GIF)); how would I get that String
I would like to reverse the order of this code's list items. Basically it's
In this code that uses the mongodb-native driver I'd like to increase the value
I have this code that I've edited: http://pastebin.com/vrqHek6S I've put in comments where I
I have this code that runs but never stops. class A { public static

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.