C:UNIX-systems programmingnewbie question- copying file using System call

This is a discussion on C:UNIX-systems programmingnewbie question- copying file using System call within the C Programming forums, part of the General Programming Boards category; Code: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<stdlib.h> int main( int argc,char *argv[] ) { char buf; int sourcefile,destfile,n; if(argc!=3) { write(STDOUT_FILENO,"prgm1 ...

  1. #1
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136

    C:UNIX-systems programmingnewbie question- copying file using System call

    Code:
    #include<unistd.h>
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<fcntl.h>
    #include<stdlib.h>
    
    int main( int argc,char *argv[] )
    {
        char buf;
        int sourcefile,destfile,n;
        if(argc!=3)
        {
            write(STDOUT_FILENO,"prgm1 <sourcefile> <destination file>\n",50);   
            exit(1);
        }
        else
        {
             sourcefile=open(argv[1],O_RDONLY);
             if(sourcefile==-1)
             {
                perror("SOURCE FILE ERROR");
                exit(0);
             }
             else
             {
                destfile=open(argv[2],O_WRONLY | O_CREAT , 0641);
                if(destfile==-1)
                {
                    perror("DESTINATION FILE ERROR");
                    exit(0);
                }
                else
                {
                    while((n=read(sourcefile,&buf,1)) != -1)
                    {
                        write( destfile, &buf, 1 );
                    }
                    write(STDOUT_FILENO, "FILES COPIED\n" , 15);    
                    close(sourcefile);
                    close(destfile);
                }
            }
        }
        return 0;
    }
    its says in the textbook that read() will return -1 on error(like not able to read from the sourcefile in this case)
    but the value returned by read() never becomes -1 ? why?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because reaching the end of file is not an error in the respect that "you have passed in a handle that isn't valid" or "you don't really want to do that". It is just "there was nothing more to read", which results in a zero-length. Since zero is not -1, you never get out of your loop.

    It is perfectly valid to read from a file, get zero bytes, then wait for, say, 10 seconds, and read again - if that gave back -1 as an error, how do you know the difference between that, and "someone removed the disk that the data is on" - the latter should probably make the program exit, or at least close the file and try to open in it again, whilst "nothing more to read" should not cause the program to exit, just do whatever it does when there was nothing else to read. A command that does something like this is "tail -f somefile" - if you send some (slow) output from a file (say a loop that prints a number every second), then you can see the output in another window using "tail -f ..."

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136

    [SOLVED] Thanks mats

    Quote Originally Posted by matsp View Post
    Because reaching the end of file is not an error in the respect that "you have passed in a handle that isn't valid" or "you don't really want to do that". It is just "there was nothing more to read", which results in a zero-length. Since zero is not -1, you never get out of your loop.

    It is perfectly valid to read from a file, get zero bytes, then wait for, say, 10 seconds, and read again - if that gave back -1 as an error, how do you know the difference between that, and "someone removed the disk that the data is on" - the latter should probably make the program exit, or at least close the file and try to open in it again, whilst "nothing more to read" should not cause the program to exit, just do whatever it does when there was nothing else to read. A command that does something like this is "tail -f somefile" - if you send some (slow) output from a file (say a loop that prints a number every second), then you can see the output in another window using "tail -f ..."

    --
    Mats

    my textbook says
    if successful, read() returns the number of bytes that it read, otherwise , it returns -1
    i misunderstood this...
    thanks,problem solved

  4. #4
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    ok, i have another doubt on open()
    i change my c-code
    from
    Code:
    sourcefile=open(argv[1],O_RDONLY);
    to
    Code:
    sourcefile=open(argv[1],O_RDONLY,0664);
    shouldn't open() be able to override the default permissions already set?
    i tried and apparently it does not. is there any reason, besides enforcing security(thats the only thing that i could think of)? and is there any way around this? (besides giving the file permissions using the system("chmod "); function?)

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I guess you could use the chmod() system call instead of calling system("chmod ...").

    Setting the mode of the file is non-trivial, as there are a number of things that affect the mode. So you may want to look at the documentation for chmod() to see what it says [I think it explains it] - note, it's "man 3 chmod" or some such, as "man chmod" will give you the command, not the function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    yeah.
    thanks again

    its
    man 3p chmod for fedora....POSIX specs of chmod system call...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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