嵌入式Linux 读写文件以及参数有关问题

嵌入式Linux 读写文件以及参数问题

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#define BUFFER_SIZE 1024

int main(int argc, char **argv) {
    int from_fd, to_fd;
    int bytes_read, bytes_write;
    char buffer[BUFFER_SIZE];
    char *ptr;
    if (argc != 3) {
        fprintf(stderr, "Usage:%s fromfile tofile\n\a", argv[0]);
        exit(1);
    }
    /* 打开源文件 */
    if ((from_fd = open(argv[1], O_RDONLY)) == -1) {
        fprintf(stderr, "Open %s Error:%s\n", argv[1], strerror(errno));
        exit(1);
    }
    /* 创建目的文件 */
    if ((to_fd = open(argv[2], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1) {
        fprintf(stderr, "Open %s Error:%s\n", argv[2], strerror(errno));
        exit(1);
    }
    /* 以下代码是一个经典的拷贝文件的代码 */
    while (bytes_read = read(from_fd, buffer, BUFFER_SIZE)) {
        /* 一个致命的错误发生了 */
        if ((bytes_read == -1) && (errno != EINTR)) break;
        else if (bytes_read > 0) {
            ptr = buffer;
            while (bytes_write = write(to_fd, ptr, bytes_read)) {
                /* 一个致命错误发生了 */
                if ((bytes_write == -1) && (errno != EINTR))break;
                    /* 写完了所有读的字节 */
                else if (bytes_write == bytes_read) break;
                    /* 只写了一部分,继续写 */
                else if (bytes_write > 0) {
                    ptr += bytes_write;
                    bytes_read -= bytes_write;
                }
            }
            /* 写的时候发生的致命错误 */
            if (bytes_write == -1)break;
        }
    }
    close(from_fd);
    close(to_fd);
    exit(0);
}





上面是我找到的一个读写文件的例子。argv[1] 是什么文件地址啊。以及那个经典例子 可以详细解释一下吗。


以前我看到一个例子。也是main(int argc, char **argv)   编译以后 在SecureCRT 当中 输入编译出来的名字 如 Myapp  1 1是不是

就把参数 传给了argc和**argv    .c中只有main以及两个读写函数  。

我想知道 比如我在开发板上面 。我想建立一个文件,对它进行读写。需要先手动创建吗,还是程序可以CREAT ,读写以后 掉

电丢失吗。


------解决思路----------------------
main(int argc, char **argv) 

大概,argc 是参数个数,argv通常是程序名

掉电丢不丢,看你文件存哪里