文件I/O-read-write

read


代码习惯


常见错误码

#include <stdio.h>
#include <fcntl.h>      // open
#include <stdlib.h>     // exit
#include <unistd.h>     // close\read
// #include <sys/types.h>
// #include <sys/stat.h>

int main(void) {
    int fd = -1;                    // 文件描述符
    ssize_t size = -1;              // 读取字符数量
    char buf[10];                   // 读取缓冲区
    char fileName[] = "./log.txt";  // 文件名字

    fd = open(fileName, O_RDONLY);  // 打开文件open
    if (fd == -1) {                 
        perror("open error:");
        exit(EXIT_FAILURE);
    } else {
        printf("open %s successfully, fd:%d\n", fileName, fd);
    }

    do {                            
        size = read(fd, buf, 10);               // 读取指定数量字符串
        if (size > 0) {                         // 有效数据,指定字符长度
            printf("read %ld bytes:", size);    // ‘ssize_t’ {aka ‘long int’}
            putchar('\n');
            for (int i = 0; i < size; i++) {    // 循环打印缓冲区单个字符
                putchar(*(buf + i));            // 指针形式打印字符串
            }
            putc('\n', stdout);
        } else {
            printf("reach the end of file end\n");
        }
    } while (size);                             // 文件末尾为0

    return 0;
}

write



#include <stdio.h>
#include <fcntl.h>      // open
#include <stdlib.h>     // exit
#include <unistd.h>     // close\read
#include <string.h>     // strlen
// #include <sys/types.h>
// #include <sys/stat.h>

int main(void) {
    int fdOpen = -1, fdWrite = -1;              // 文件描述符
    ssize_t size = -1;                          // 读取字符数量
    ssize_t wsize = -1;
    char buf[10];                               // 读取缓冲区
    char * openFile = "./log.txt";              // 文件名字
    char * writeFile = "./copy.txt";

    /*打开读取文件*/
    fdOpen = open(openFile, O_RDONLY);          // 打开文件open
    if (fdOpen == -1) {                 
        perror("open error:");
        exit(EXIT_FAILURE);
    } else {
        printf("open %s successfully, fd:%d\n", openFile, fdOpen);
    }

    /*打开写入文件*/
    fdWrite = open(writeFile, O_WRONLY | O_CREAT | O_TRUNC);     // 写入的文件
    if (fdWrite == -1) {                 
        perror("open error:");
        exit(EXIT_FAILURE);
    } else {
        printf("open %s successfully, fd:%d\n", writeFile, fdWrite);
    }

    /*循环写入文件*/
    do {                            
        size = read(fdOpen, buf, 10);           // 读取指定数量字符串
        printf("size:%ld\n", size);
        if (size > 0) {                         // 有效数据,指定字符长度
            printf("read %ld bytes:", size);    // ‘ssize_t’ {aka ‘long int’}
            putchar('\n');
            for (int i = 0; i < size; i++) {    // 循环打印缓冲区单个字符
                putchar(*(buf + i));            // 指针形式打印字符串
            }
            putc('\n', stdout);
            wsize = write(fdWrite, buf, strlen(buf));   // 以设定缓冲区大小情况下怎么打印指定大小
            printf("Write %ld bytes to file %s\n", wsize, writeFile);
        } else {
            printf("reach the end of file end\n");
        }
    } while (size);                             // 文件末尾为0

    close(fdOpen);
    close(fdWrite);
    return 0;
}

疑问

  • 缓冲区设定的技巧,不然会打印失效字符
posted @ 2022-04-29 16:43  steve的miao  阅读(30)  评论(0编辑  收藏  举报