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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:56:46+00:00 2026-05-24T21:56:46+00:00

I have to use read a file and write the information to another file

  • 0

I have to use read a file and write the information to another file using system call. I have opened the file for reading as below
filedesc = open(argv[1],O_RDONLY); //which works fine
opened a file to write as below:
fdw=creat(strcat(store,argv[1]),PERMS) // PERMS 0666
Used lseek to reach to the one of file filedesc ( to reverse and print )

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

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

     int filedesc,n,i,fdw,pos;
     char c;
     const char * prefix = "reserve_";
     int needed = strlen( argv[ 1 ] ) + strlen( prefix ) + 1;
     char store[ needed ];
     strcpy( store, prefix );

     if(argc != 2)
   {
       printf(" usage : %s filename ", argv[0]);
     }

         //strcat( store, argv[ 1 ] );
         //printf( "%s\n", store );


      filedesc =  open(argv[1],O_RDONLY);
         if(filedesc <1 )
            {  printf("Unable to open file\n");
          exit(1); }


     if((fdw=creat(strcat(store,argv[1]),PERMS))==-1)
      printf("Unable to create file,please use perror to find details\n");
    else printf("File created\n");



       if((pos=lseek(filedesc,0,SEEK_END))==0)
         {
           printf("Empty file\n");
            exit(-1);
           }
         i=pos-1; 
             //printf("%d",i);

            while(i!=0)
             {
             pos=lseek(filedesc,i-1,SEEK_SET);
              read(filedesc,&c,1);
              if( write(fdw,&c,1)!=1)  //information is not being written to the file
                printf("Error\n");     //pointed by fdw rather getting printed on the
                                       //screen
                i--;                     
            }

        close(filedesc);
          close(fdw);



            }

Problem: If i am running this without first argument, it gives a segmentation fault instead of the usage. Please help

  • 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-24T21:56:47+00:00Added an answer on May 24, 2026 at 9:56 pm

    Your if line is wrong. It will actually store the result of the comparison in fdw instead of storing the result of creat(2).

    if(fdw=creat(strcat(store,argv[1]),PERMS)==-1) /* Wrong. */
    

    Try:

    if((fdw = creat(strcat(store, argv[1]), PERMS)) == -1)
       ^                                          ^
    

    EDIT

    You should also check the result of read:

    if (read(filedesc, &c, 1) <= 0)
        break;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to read/write the window information file (extended file properties) using c#
I'm working on a serialization tool using Moose to read and write a file
I have a file that contains information like the below: 1/1/2010 1-2-3-4 1 1/2/2010
If I have an xmlreader instance how can I use it to read its
i have a char buffer[100] and i'm trying to use gdb to read the
i have to read tags of cmyk *.tiff and *.eps files. i use .net
Have a use case wherein need to maintain a connection open to a database
I am using Nokogiri to read an XML file. I store some of the
Is there a way to read a file's data but continue reading the data
My problem is in regards file copying performance. We have a media management system

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.