LInux文件基础知识和文件目录操作(二)文件I/O操作

1、文件I/O操作分为两部分来讲解:

     第一部分是非缓冲文件操作,这种操作适合于比较小规模文件的读写和对实时性要求很高的设备的数据通信,这类操作是系统调用提供的;

     第二部分是缓冲文件操作,所面向的则是大规模非实时性数据处理问题,这类操作是标准输入输出库提供的。

2、非缓冲文件操作:

     非缓冲文件操作是针对于小规模文件的读写,或者是实时设备。执行非缓冲文件操作后,应用程序将立即获取或传送数据。非缓冲文件操作的函数只有两个,分别是read()函数和write()函数,这些函数通过文件标识符找到文件。在介绍这些函数前,首先介绍三个操作系统预先分配的文件标识符:
     0,标准输入,即通过终端输入。
     1,标准输出,即通过终端输出。
     2,标准错误,系统中存放错误信息的堆栈。

     注:非缓冲文件操作函数涉及:open/close/read/write/lseek

3、缓冲文件操作:

    基于缓冲区的文件I/O操作减少了对设备的物理数据接口访问次数,从而使大量数据的I/O操作的系统开支减少,并将实际读写速度提升。标准输入输出库定义了文件流结构指针FILE*作为文件的标识,同时提供了一系列缓冲文件操作函数。有三个文件流是系统预定义的,分别是:
     stdin,标准输入。
     stdout,标准输出。
     stderr,标准错误。

     注:缓冲文件操作函数涉及:fopen/fclose/fread/fwrite/fscanf/fprintf/fseek/fflush/fgetpos/fsetpos……

4、code:

 (1) FileHandler.c:  non-buffer  reading file

#include <stdio.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(){
    char c[8000];
    int fd, i, j=0;
    fd = open("/usr/include/gnu-versions.h", O_RDONLY);  //get file description
    if(fd != -1){
        i = read(fd, c, 8000);                     //read information from file
        for(; i>0; i--){
            putchar(c[j++]);                   //print char into standard output
        }
    }else{
        puts("open file failed.");
    }
    return 0;
}

(2) FileHandler2.c: non-buffer write

#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define NUMBER 2000

int main(){
    char c[NUMBER];
    int fd, i;
    i = read(0, c, NUMBER);             //standard input
    if(i>0){
        fd = open("test", O_RDWR);
        if(fd != -1){
            if(write(fd, c, i) != i){
                perror("write test error.");
            }
            puts("write test successfully.");
            close(fd);
        }else{
            perror("open test failed");
        }

    }else{
        perror("read standard input error.");
    }
    return 0;
}
(3)FileHandler3.c : lseek function : change file pointer.

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(){
    int fd = open("test", O_RDWR | O_CREAT, 0664);
    if(fd != -1){
        if(write(fd, "12345", 5) != 5){
            perror("write error!");
        }
        lseek(fd, 2, SEEK_SET);           //change file pointer position.
        if(write(fd, "abcdefg", 7) != 7){
            perror("write error set");
        }
        lseek(fd, -3, SEEK_CUR);
        if(write(fd, "asdfghjkl", 9) != 9){
            perror("write error cur");
        }
        lseek(fd, -2, SEEK_END);
        if(write(fd, "zxcvbnm", 7) != 7){
            perror("write error end");
        }
        close(fd);
    }
 return 0;
}
(4) FileHandler4.c: buffer read and write.

#include <stdio.h>
#define SIZE 65536
#define LENGTH 1024

int main(){
    FILE *fp;
    char c[SIZE];
    int i;
    fp = fopen("/usr/include/gnu-versions.h", "r");
    if(fp != NULL){
        puts("open file success");
    }else{
        perror("open failed");
    }

    i = fread(c, LENGTH, SIZE/LENGTH, fp);
    fclose(fp);
    if(i > 0){
        fp = fopen("test", "rw+");
        if(fp != NULL){
            i = fwrite(c, LENGTH, SIZE/LENGTH, fp);
            if(i > 0){
                fflush(fp);
            }
            fclose(fp);
        }else{
            perror("open test error");
        }
    }

    if(fclose(fp) != EOF){
        puts("close file success");
    }else{
        perror("close failed");
    }
    return 0;

}

(5) FileHandler5.c: buffer fscanf and fprintf

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

struct buddy{
    char name[50];
    int tel;
    char address[100];
};
int main(){
    if(creat("buddy", 0664) == -1){
        perror("create buddy failed");
        return 1;
    }
    FILE * fp= fopen("buddy", "rw+");
    if(fp != NULL){
        fprintf(fp, "<name>%s <tel>%d <address>%s", "wangle",
                1234567890,"China");
        fclose(fp);
        struct buddy bud;
        fp = fopen("buddy", "rw+");
        fscanf(fp, "<name>%s <tel>%d <address>%s</address>", bud.name,
                &bud.tel,bud.address);
        fclose(fp);
        printf("<name>%s <tel>%d <address>%s</address>", bud.name,
                bud.tel,bud.address);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/wangle1001986/p/3189414.html