How to write a file with C in Linux?

后端 未结 4 1488
忘了有多久
忘了有多久 2021-02-04 13:36

I want to rewrite the \"cp\" command of Linux. So this program will work like #./a.out originalfile copiedfile. I can open the file, create new file but can\'t writ

相关标签:
4条回答
  • 2021-02-04 13:47

    You need to write() the read() data into the new file:

    ssize_t nrd;
    int fd;
    int fd1;
    
    fd = open(aa[1], O_RDONLY);
    fd1 = open(aa[2], O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
    while (nrd = read(fd,buffer,50)) {
        write(fd1,buffer,nrd);
    }
    
    close(fd);
    close(fd1);
    

    Update: added the proper opens...

    Btw, the O_CREAT can be OR'd (O_CREAT | O_WRONLY). You are actually opening too many file handles. Just do the open once.

    0 讨论(0)
  • 2021-02-04 13:57

    First of all, the code you wrote isn't portable, even if you get it to work. Why use OS-specific functions when there is a perfectly platform-independent way of doing it? Here's a version that uses just a single header file and is portable to any platform that implements the C standard library.

    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        FILE* sourceFile;
        FILE* destFile;
        char buf[50];
        int numBytes;
    
        if(argc!=3)
        {
            printf("Usage: fcopy source destination\n");
            return 1;
        }
    
        sourceFile = fopen(argv[1], "rb");
        destFile = fopen(argv[2], "wb");
    
        if(sourceFile==NULL)
        {
            printf("Could not open source file\n");
            return 2;
        }
        if(destFile==NULL)
        {
            printf("Could not open destination file\n");
            return 3;
        }
    
        while(numBytes=fread(buf, 1, 50, sourceFile))
        {
            fwrite(buf, 1, numBytes, destFile);
        }
    
        fclose(sourceFile);
        fclose(destFile);
    
        return 0;
    }
    

    EDIT: The glibc reference has this to say:

    In general, you should stick with using streams rather than file descriptors, unless there is some specific operation you want to do that can only be done on a file descriptor. If you are a beginning programmer and aren't sure what functions to use, we suggest that you concentrate on the formatted input functions (see Formatted Input) and formatted output functions (see Formatted Output).

    If you are concerned about portability of your programs to systems other than GNU, you should also be aware that file descriptors are not as portable as streams. You can expect any system running ISO C to support streams, but non-GNU systems may not support file descriptors at all, or may only implement a subset of the GNU functions that operate on file descriptors. Most of the file descriptor functions in the GNU library are included in the POSIX.1 standard, however.

    0 讨论(0)
  • 2021-02-04 13:59

    You have to allocate the buffer with mallock, and give the read write the pointer to it.

    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    int main(){
        ssize_t nrd;
        int fd; 
        int fd1;
    
        char* buffer = malloc(100*sizeof(char));
        fd = open("bli.txt", O_RDONLY);
        fd1 = open("bla.txt", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
        while (nrd = read(fd,buffer,sizeof(buffer))) {
            write(fd1,buffer,nrd);
        }   
    
        close(fd);
        close(fd1);
        free(buffer);
        return 0;
    }
    

    Make sure that the rad file exists and contains something. It's not perfect but it works.

    0 讨论(0)
  • 2021-02-04 14:08

    You have to do write in the same loop as read.

    0 讨论(0)
提交回复
热议问题