Linux我用 LinuxLinux学习之路

Linux文件编程

2017-07-07  本文已影响118人  故事狗

文件概述

文件:存储在外部介质上的数据的集合,是操作系统数据管理的单位

在Linux下一切皆文件。

int整数10000不同存放的形式

文件的处理方法

typedef struct _iobuf{
    char*      _ptr;        //文件输入的下一个位置
    int       _cnt;         //当前缓存区的相对位置
    char*    _base;        //指基础位置
    int      _flag;        //文件标志
    int      _file;        //文件的有效性验证
    int      _charbuf;    //检查缓冲区的状况,如果无缓冲区则不读取
    int      _bufsiz;     //文件的大小
    char*    _tmpfname;   //临时文件名
} FILE;

对底层文件的信息进行了封装。

//例如:只读方式打开
#include<stdio.h>
//.......
FILE *fp1;
 fp1 = fopen("test.dat". "r");
char* filename = "test.dat";
fp1 = fopen(filename, "r");
//例如:读写方式打开,打开错误程序退出
FILE* fp2;
fp2 = fopen(filename, "rw");
if(fp2 == NULL)
{
    printf("File open error!\n");
    exit(0);
}
#define  putc(ch, fp)  fputc(ch, fp)
//putc实际是fputc的宏定义

#define  getc(fp)      fgetc(fp)
//getc实际是fgetc的宏定义

#define  putchar(c)    fputc(c, stdout)  
//常用的putchar其实是fputc在stdout的宏定义

#define  getchar()     fgetc(stdin)  
//常用的getchar实际是fgetc在stdin的宏定义

例如,读取文件并显示

#include<stdio.h>
#include<stdlib.h>

int main()
{
    FILE* fp;
    char ch, *filename = "out.txt";
    if((fp = fopen(filename, "r")) == NULL){
        printf("cannot open file\n");
        exit(0);
    }

    while((ch = fgetc(fp)) != EOF)
        putchar(ch);

    fclose(fp);

    return 0;
}
//将屏幕输入写入文件中,如果为$则结束输入
#include<stdio.h>
#include<stdlib.h>

int main()
{
        FILE *pf;
        pf = fopen("./test.dat", "w");
        if(pf == NULL)
        {
                printf("error\n");
                exit(0);
        }

        int x = 0;
        while((x = getchar()) != '$')
        {
                putchar(x);
                fputc(x, pf);
                x = 0;  
        }

        fclose(pf);
        return 0;
}
执行以上的输入到文件中的程序
由执行结果可以看出,如果未输入$那么程序不会跳出while循环,则不会执行到fclose,那么文件中则不会有内容。因此fclose具备刷新缓冲区到硬盘的作用。
#include<stdio.h>
#include<stdlib.h>

int main()
{
        FILE *pf;
        pf = fopen("./test.dat", "w");
        if(pf == NULL)
        {
                printf("error\n");
                exit(0);
        }

        int x = 0;
        while((x = getchar()) != '$')
        {
                putchar(x);
                fputc(x, pf);
                x = 0;
                fflush(pf);     //手动的刷新缓存到硬盘  
        }

        fclose(pf);
        return 0;
}
手动刷新缓存,即使不调用`fclose`也可以将内容写到文件中
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct t
{
        int num;
        int count;
} test;

int main()
{
        FILE* fp;
        if((fp = fopen("test.dat", "w")) == NULL)
        {
                printf("error\n");
                exit(0);
        }
        int i, j;
        test ts[5];
        for(i =0; i < 5; i++){
                ts[i].num = i;
                ts[i].count = i * 100;
                printf("num = %d; count =  %d \n", ts[i].num, ts[i].count);
        }

      // 采用逐个存放的方法存放,也可以一次性把数组直接存入,那么代码是  fwrite(ts, 1, sizeof(ts), fp);

        for(j = 0; j < 5; j++){
                fwrite(&ts[j], 1, sizeof(test), fp);
        }

        fclose(fp);

        FILE* pf;
        if((pf = fopen("test.dat", "r")) == NULL){
                printf("error\n");
                exit(0);
        }

        int k;
        test ts2[5];
        //之前是逐个放入的,这里一次性取出,也可以逐个取出
        fread(ts2, 5, sizeof(test), pf);
        for(k = 0; k < 5; k++){
                printf("num = %d; count =  %d \n", ts2[k].num, ts2[k].count);
   
        }
        fclose(pf);

        return 0;
}
                                                                        
执行结果

这两个函数的使用和scanf和printf基本一致,无非第一个参数为文件指针。同样如果fscanf的第一个参数为stdin,那么就相当于scanf;如果fprintf的第一个参数为stdout,那么就相当于printf

fprintf(fp, "%d, %d6.2f", i, t);//将i、t按照%d,%6.2f的格式输出到fp文件中
fscanf(fp, "%d, %f", &i, &t);//若文件中有3,4.5,则3送入i,4.5送入t
#include<stdio.h>
int main()
{
  char s[80], c[80];
  int a, b;
  FILE *fp;
  if((fp = fopen("test", "w")) == NULL){
    puts("can't open file\n");
    exit(0);
  }
  
    fscanf(stdin, "%s%d", s, &a);  //从键盘读取
    fprintf(fp, "%s %d", s, a); //写到文件
    fclose(fp);

    if((fp = fopen("test", "r")) == NULL){
    puts("can't open file\n");
    exit(0);
  }
  fscanf(fp, "%s%d", c, &b);  //从文件读取
  fprintf(stdout, "%s %d", c, b);  //打印到屏幕
  return 0;
}

//显示,复制文件中的内容

include<stdio.h>

int main(){
FILE *fp1, *fp2;
fp1 = fopen("test.dat", "r");
fp2 = fopen("target.dat", "w");
while(!eof(fp1)) putchar(getc(fp1));
//fp1的指针已指向文件末尾,如果不rewind,则从末尾开始执行后续代码,写入不了内容。
rewind(fp1);
while(!feof(fp1)) put(getc(fp1, fp2);
fclose(fp1);
fclose(fp2);
return 0;
}


  - fseek函数
    - 函数原型`int fseek(FILE *fp, long offset, int whence)`
        - fp:文件指针
        - offset:偏移量(以起始点为基点,移动的字节数),>0向后移动, <0向前移动
        - whence:起始点
            - SEEK_SET  0:文件开始
            - SEEK_CUR  1:文件的当前位置
            - SEEK_END   2:文件末尾
    - 功能:改变文件位置指针的位置
    - 返回值:成功,返回0,失败,返回非0值。

    - ftell函数
      - 函数原型:`long  ftell(FILE *fp)`
      - 功能:返回位置指针当前的位置(用相对文件开头的位移量表示)
      - 返回值:成功:返回当前位置指针位置;失败,返回-1L

# I/O文件编程
- Linux系统调用
所谓系统调用是指操作系统提供给用户程序的一组“特殊的”接口,用户程序可以通过这个组“特殊”接口来获得操作系统内核提供的特殊服务。
在linux中用户程序不能直接访问内核提供的服务。为了更好的保护内核空间,将陈股的运行空间分为内核空间和用户空间,他们运行在不同的级别上,在逻辑上是相互隔离的。
有两种操作内核的方式:系统命令、用户程序接口(API)

- 用户编程接口(API)
在linux中用户编程接口(API)遵循了UNIX中最流行的应用编程界面标准——POSIX标准(可移植操作系统接口标准)。这些系统调用编程接口主要通过C库(libc)实现的。

- 文件I/O介绍
- 可用的文件I/O函数——打开文件、读文件、写文件等等。大多数linux文件I/O只需要5个函数:open、read、write、lseek、close
不带缓存指的是每个read和write都是调用内核中的一个系统调用。这些不带缓存的I/O函数不是ANSI C的组成部分,但是POSIX的组成部分。

- Linux文件描述符
对内核而言所有打开文件都有文件描述符引用。文件描述符是一个非负整数。当打开一个现存文件或创建一个新文件时,内核向进程返回一个文件描述符。当读、写一个文件时,用open或create返回的文件描述符标识该文件,将其作为参数传送给read或write。
在POSIX.1应用程序中,整数0、1、2应被代换成符号常数STDIN_FILENO、STDOUT_FILENO和STDERR_FILENO。这些常数都定义在头文件<unistd.h>中。
文件描述符的范围是0~OPEN_MAX。早期UNIX版本采用的上限是19(允许每个进程打开20个文件),现在很多程序则将其增加至63。

- POSIX文件编程
- open  

include<sys/types.h>

include<sys/stat.h>

include<fcntl.h>

int open(const char *pathname, int oflag, mode_t mode);

- 返回:若成功返回文件描述符;若出错返回-1
- 参数
  - pathname:需要打开的文件的路径和名称
  - oflag:打开的标记,可用来说明此函数的多个选择项,参数如下(常数定义在<fcntl.h>)
    - O_RDONLY:只读打开
    - O_WRONLY:只写打开
    - O_RDWR:读、写打开
    - O_APPEND:每次写时,都是加到文件的尾端
    - O_CREAT:若此文件不存在则创建它。使用此选项时,需要同时说明第三个参数mode,用其说明该新文件的存取许可权位
    - O_EXCL:如果同时指定了O_CREAT,而文件已经存在,则出错。这可测试一个文件是否存在,如果不存在则创建此文件成为一个原子操作。
    - O_TRUNC:如果此文件存在,而且为只读或只写成功打开,则将其长度截短为0(清空文件)
    - O_NOCTTY:如果pathname指的是终端设备,则不将此设备分配作为此进程的控制终端
    - O_NONBLOCK:如果pathname指的是一个FIFO、一个块特殊文件或一个字符特殊文件,则此选项为此文件打开操作和后续的I/O操作设置非阻塞方式。
    - O_SYNC:是每次write都等到物理I/O操作完成。
  - mode:打开的模式,对open函数而言,仅当创建新文件时才使用第三个参数。(文件权限chmod 777 filename)
mode标志用来表示文件的访问权限。
Linux总共用五个数字表示文件的权限(用户id、设置组id、自己权限、组权限、其他人权限)如果有O_CREAT参数时:`open("test", O_RDWR | O_CREAT, 10750);`,没有O_CREAT参数时:`open("test", O_RDWR); `

- creat
可用creat函数创建一个新文件

include<sys/stat.h>

include<sys/types.h>

include<fcntl.h>

int creat(const char* pathname, mode_t mode);

  - 返回值:若成功返回只写打开的文件描述符;出错返回-1。
    注意:此函数等效于:
`open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);`
  - creat的一个不足之处是他以只写方式打开所创建的文件。

- close

include<unistd.h>

int close(int filedes);

  - 返回值:若成功返回0;出错返回-1
  - 当一个程序终止时,他所有的打开文件都由内核自动关闭。很多程序都是用这一功能而不显示地用close关闭打开的文件。


/open.c/

include<unistd.h>

include<sys/types.h>

include<sys/stat.h>

include<fcntl.h>

include<stdlib.h>

include<stdio.h>

int main()
{
int fd = -1;
fd = open("hello.c", O_CREAT |O_TRUNC | O_WRONLY, 00600);
if(fd < 0)
{
perror("open:");
exit(1);
}
else
printf("open file:1hello.c %d\n", fd);

if(close(fd) < 0){
perror("close");
exit(2);
}
else
printf("Close hello.c);
return 0;
}


- read
用read函数从打开文件中读取数据

include<unistd.h>

ssize_t read(int feledes, void* buff, size_t nbytes);

- 返回值:如果read成功,返回读到的字节数;若已到文件尾为0;出错返回-1;

有很多种情况可使实际读到的字节数少于要求读的字节数
  1. 读普通文件时,在读到要求字节数之前已经到达了文件的尾端。例如,若在到达文件尾端之前还有30个字节,而要求读100个字节,则read返回30,下一次再调用read时,将会返回0(文件尾端)
  - 当从终端设备读时,通常一次最多读一行。
  - 当从网络读取时,网络中的缓冲机构可能造成返回值小于所要求读的字节数
  - 某些面向记录的设备,例如磁带,一次做多返回一个记录
  - 读操作从文件的当前位移量处开始,在成功返回之前,该位移量增加实际读取的字节数。

- write函数
用write函数向打开文件些数据

include<unistd.h>

ssize_t write(int filedes, const void* buff, size_t nbytes);

  - 返回值:若成功为已写的字节数,若出错返回-1
    - 其返回值通常与参数nbytes的值不同,则表示出错。write出错的一个常见原因是:磁盘已写满,或者超过了对一个给定进程的文件长度限制。
  - 对于普通文件,写操作从稳健的当前位移量处开始。如果在打开该文件时,指定了O_APPEND选项,则在每次写操作之前,将文件位移量设置在文件的当前结尾处。再一次成功写之后,该文件位移量增加实际写的字节数。

/open.c/

include<unistd.h>

include<sys/types.h>

include<sys/stat.h>

include<fcntl.h>

include<stdlib.h>

include<stdio.h>

include<string.h>

int main()
{
int fd = -1;
int ret;
char buf[32];
fd = open("hello.c", O_CREAT | O_RDWD, 00600);
if(fd < 0)
{
perror("open:");
exit(1);
}
else
printf("open file:1hello.c %d\n", fd);

ret = read(fd, buf, 12);
if(ret < 0) printf("read error....\n");
else{
buf[ret] = '\0'; //手动添加字符串结束符
printf("str = %s\n", buf);
}

ret = write(fd, buf, strlen(buf));
if(ret == strlen(buf)) printf("write successly");

if(close(fd) < 0){
perror("close");
exit(2);
}
else
printf("Close hello.c);
return 0;
}



- lseek

include<sys/types.h>

include<unistd.h>

off_t lseek(int filesdes, off_t offset, int whence);

  - 返回值:若成功为新的文件位移,若出错,返回-1。
  - 对参数offset的解释与参数whence的值
    - 若whence是SEEK_SET,则将该文件的位移量设置为局文件开始处offset个字节。
    - 若whence是SEEK_CUR,则将该文件的偏移设置为其当前偏移量+offset,offset可以为正或负。
    - 若whence是SEEK_END,则将该文件的位移量设置为文件长度+offset,offset可以为正或负。

若lseek成功执行,则返回新的文件位移量,为此可以用一下方式确定一个代开文件的昂前位移量:

off_t curr_pos;
curr_pos = lseek(fd, 0, SEEK_CUR);


include<sys/stat.h>

include<sys/types.h>

include<fcntl.h>

include<unistd.h>

include<string>

include<stdlib.h>

include<stdio.h>

int main()
{
int fd, size, len;
char *buf = "Hello! I'm writing to this file!";
char buf_r[10];
off_t curr;

len = strlen(buf);
if((fd = open("hello.c", O_CREAT | O_TRUNC | O_RDWR, 0666)) < 0)
{
perror("open:");
exit(1);
}
else printf("open file: hello.c %d\n", fd);

curr = lseek(fd, 0, SEEK_CUR);
printf("curr = %d\n", curr);

if((size = write(fd, buf, len)) < 0){
perror("write");
exit(2);
}
else printf("write: %s\n", buf);

curr = lseek(fd, 0, SEEK_CUR);
printf("curr = %d\n", curr);
lseek(fd, 0, SEEK_SET); //指针回到开头
if((size = read(fd, buf_r, 9)) < 0){ //从头读取9个字符
perro("read");
exit(3);
}
else{
buf_r[size] = '\0';
printf("read from file: %s, len = %d\n", buf_r, size);
}
if(close(fd) < 0){
perror("close");
exit(4);
}
else{
printf("close hello.c");
}
return 0;
}


操作多个终端的程序

include<stdlib.h>

include<stdio.h>

define TTY0 "/dev/pts/0" //终端设备名

define TTY1 "/dev/pts/1"

define TTY2 "/dev/pts/2"

define TTY3 "/dev/pts/3"

void tty_write(char* tty, char* buf)
{
int fd = -1;
fd = open(tty, O_RDWR);
if(fd < 0){
fprintf(stdin, "open tty err\n");
exit(0);
}
write(fd, buf, strlen(buf));
close(fd);
}

int main()
{
char buf[] = "test tty\n";
char tty[32];
strcpy(tty, TTY1);
tty_write(tty, buf);
strcpy(tty, TTY2);
tty_write(tty, buf);

    return 0;

}



![消息发送的情况](http:https://img.haomeiwen.com/i5688965/dadfbe7d59408523.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![TTY2收到的消息](http:https://img.haomeiwen.com/i5688965/69047eecbce94d73.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

- 总结:
  1. 打开文件
`FILE *fopen(char *name, char *mode)`
  - 文件关闭
`int fclose(FILE *fp);`
  - 字符I/O:
`int fputc(int c, FILE* fp)`
`int fgetc(FILE* fp)`
  - 数据块I/O:
 `size_t fread(void* buffer, size_t size, size_t count, FILE *fp)`
 `size_t fwrite(void* buffer, size_t size, size_t count, FILE *fp)`
  - 格式化I/O:
`int fprintf(FILE *fp, const char* format[, argument,...])`
`int fscanf(FILE *fp, const char* format[, address...])`
  - 字符串I/O:
    `char* fgets(char *s, int n, FILE *fp)`
    `int fput(char *s, FILE *fp)`
  -  rewind函数
       `void  rewind(FILE *fp)`
  - fseek函数
   `int fseek(FILE *fp, long offset, int whence)`
  - open  

include<sys/types.h>

include<sys/stat.h>

include<fcntl.h>

int open(const char *pathname, int oflag, mode_t mode);

 - creat

include<sys/stat.h>

include<sys/types.h>

include<fcntl.h>

int creat(const char* pathname, mode_t mode);

  - close

include<unistd.h>

int close(int filedes);

  - read

include<unistd.h>

ssize_t read(int feledes, void* buff, size_t nbytes);

  - write

include<unistd.h>

ssize_t write(int filedes, const void* buff, size_t nbytes);

  - lseek

include<sys/types.h>

include<unistd.h>

off_t lseek(int filesdes, off_t offset, int whence);

上一篇下一篇

猜你喜欢

热点阅读