Like Tree1Likes
  • 1 Post By grumpy

ssize_t sendfile - transfering data from one file to another

This is a discussion on ssize_t sendfile - transfering data from one file to another within the C Programming forums, part of the General Programming Boards category; Hi , could you plese advise ,I have some problem with the ssize_t sendfile function. ssize_t sendfile(int out_fd , int ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    58

    Question ssize_t sendfile - transfering data from one file to another

    Hi ,
    could you plese advise ,I have some problem with the ssize_t sendfile function.

    ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);

    the error:

    Lesson_4_1A_Home_work.c:84:24: error: expected declaration specifiers or ‘...’ before string constant
    Lesson_4_1A_Home_work.c:84:37: error: expected declaration specifiers or ‘...’ before string constant
    Lesson_4_1A_Home_work.c:84:63: error: expected declaration specifiers or ‘...’ before ‘strlen’


    Code:
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif
    
    #include <sys/sendfile.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <stdlib.h>  //used for exit()
    #include <string.h>
    
    #include <fcntl.h>
    #include <unistd.h>
    #include <assert.h>
    #include <error.h>
    
    
    //prototypes
    
    
    /*******************************************************************************/
    int main()
    {
        #define WorkPATH "//home//ilan/Embedded_linux//Lesson-4_IO_Routines_and_File_descriptors//Home_work//My_solution//a.txt"
        #define NewWorkPATH "//home//ilan/Embedded_linux//Lesson-4_IO_Routines_and_File_descriptors//Home_work//My_solution//NEWa.txt"
        int fd,length=0;
        int fdnew;
        int size;
        char read_buffer[80];
        char write_buffer[]="dddddddddddddddddddddddddddddddddd";
        char write_buffernew[]="Hi new file";
        off_t offset = 0;      /* byte offset used by sendfile */
        
        //open the file, if it doesn't exist create it with the following permission access
        fd = open (WorkPATH,/* O_RDONLY */ O_WRONLY  |O_CREAT,0600);
        if (fd == -1) 
        {
            /* The open failed. Print an error message and exit. */
            printf("error opening file: \n");
            exit(-1);
        }
        
        fdnew = open (NewWorkPATH,/* O_RDONLY */ O_WRONLY  |O_CREAT,0600);
        if (fdnew == -1) 
        {
            /* The open failed. Print an error message and exit. */
            printf("error opening file: \n");
            exit(-1);
        }
        
        /* Read the size of the data in the  file. */
        //Use the systems calls in order to read from $path via read only permissions
        read (fd, read_buffer, sizeof (read_buffer));
        
        //Try to write to it ,with the same read only permissions
         write (fd, write_buffer, strlen (write_buffer));
        
            printf("The file - a.txt - Content is: \n");
                system("cat a.txt\n");    
        // Close the file descriptor, 
        close (fd);
             
          ssize_t sendfile(NewWorkPATH, WorkPATH, off_t *offset,  strlen(write_buffer));
        
        
        // read (fd, read_buffer, sizeof (read_buffer));
        // write (fd, write_buffernew, strlen (write_buffer));
             printf("The file -NEWa.txt - Content is: \n");
                system("cat NEWa.txt\n");    
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Sorry, but given this site's policy about homework (at this link) I'm not inclined to be sympathetic to a transparent attempt to snaffle some code from someone else instead of actually doing your homework, after which you have attempted to make a change by guesswork, with negligible understanding of the programming language, let alone what the code does.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    > #define WorkPATH "//home//ilan/Embedded_linux//Lesson-4_IO_Routines_and_File_descriptors//Home_work//My_solution//a.txt"
    > #define NewWorkPATH "//home//ilan/Embedded_linux//Lesson-4_IO_Routines_and_File_descriptors//Home_work//My_solution//NEWa.txt"
    What's with all the // in the pathnames?
    A single / suffices.

    Perhaps you're thinking of that other OS, where you have to write "\\path\\to\\file.txt"

    > ssize_t sendfile(NewWorkPATH, WorkPATH, off_t *offset, strlen(write_buffer));
    It seems you stopped editing the function prototype half-way through, then forgot what you were doing.
    At a guess
    ssize_t result = sendfile(NewWorkPATH, WorkPATH, &offset, strlen(write_buffer));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Quote Originally Posted by Salem View Post
    At a guess
    ssize_t result = sendfile(NewWorkPATH, WorkPATH, &offset, strlen(write_buffer));
    ... except that the two arguments to sendfile() are of type int, not type char *. However, look at what preceding code does with open() .....
    Salem likes this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c sendfile doesn't works the 2nd time
    By polslinux in forum C Programming
    Replies: 5
    Last Post: 08-08-2012, 05:59 AM
  2. Replies: 2
    Last Post: 05-06-2011, 08:43 AM
  3. Doubt about file transfering program
    By lautarox in forum C Programming
    Replies: 6
    Last Post: 02-25-2010, 12:00 PM
  4. Replies: 21
    Last Post: 11-03-2007, 03:56 PM
  5. transfering contents of 3 arrays to 1
    By myth in forum C Programming
    Replies: 1
    Last Post: 03-08-2002, 08:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21