文件的读和写—接口部分的讲解

open read write close ;man 2 open;man 2 write//这一套
fopen fread fwrite fgets getline fclose//这一套
NAME
       read - read from a file descriptor

SYNOPSIS
       #include <unistd.h>

man 2 open
NAME
       open, openat, creat - open and possibly create a file

SYNOPSIS
       #include <sys/types.h>
       #include <sys/stat.h>
       #include <fcntl.h>

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


DESCRIPTION
       The open() system call  opens  the  file  specified  by
       pathname.  If the specified file does not exist, it may
       optionally (if O_CREAT is specified in flags)  be  cre‐
       ated by open().

	   The  return  value  of  open()  is a file descriptor, a
       small, nonnegative integer that is used  in  subsequent
       system  calls  (read(2),  write(2), lseek(2), fcntl(2),
       etc.) to refer to the open file.  The  file  descriptor
       returned  by  a successful call will be the lowest-num‐
       bered  file  descriptor  not  currently  open  for  the
       process.
		
	   //下面这段是说明, 如果打开0, 1, 2,关闭了0, 下次在打开一个,就默认是打开0这个进程
 	   By  default,  the  new file descriptor is set to remain
       open across an execve(2)  (i.e.,  the  FD_CLOEXEC  file
       descriptor flag described in fcntl(2) is initially dis‐
       abled); the O_CLOEXEC flag,  described  below,  can  be
       used to change this default.  The file offset is set to
       the beginning of the file (see lseek(2)).

pathname是,路径名字;
要打开的时候有一些flags,是标志;
mode_t mode 文件的权限;

man 2 read
#include <unistd.h>

ssize_t
     read(int fildes, void *buf, size_t count);
//返回值显示实际读入了文件的大小,是一个整型
//size_t count是一个预计读入文件的大小,如果cout > SSIZE_MAX,就会出现不可预计的错误。

ERRORS
如果返回的值是-1,那就是一个非法的值
errno
man 2 close

#include <unistd.h>
int close(int fd); //返回-1,On errot

DECRIPTION

close() 文件,文件相关的资源都会被删除的,读写;
man fprintf

SYNOPSIS
int fprintf(FILE *stream, const cahr * format, ... );//基于文件流的打开

man fopen

NAME fopen fdopen freopen

SYNOPSIS
	#include <stdio.h>
	FILE *fopen(const char *pathname, const char *mode);
描述
	打开一个文件流,作为一个字符串;mode 有哪些呢?
	r :
	r+: Open for reading and writing
	w : Truncate file to zero length ir create text file for writing
	w+: reading and writing
	a : Open for appending (writing at the end of file)
		如果文件不存在就会创建一个文件
	a+: Open for reading and appending
	The mode string can also include letter 'b'就会展现出一个二进制的文件

RETURN VALUE
	成功了,就返回一个文件指针; return a FILE pointer
	否则出错了,返回一个空指针;NULL is returned and errono is set to indicate the error
man fread

SYNOPSIS
	#include <stdio.h>

	size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
	1. 指针
	2. size读多大
	3. nmemb 一次读多大
	4. 读的那个流

	size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
DESCRIPTION
	fread()的功能是读nmemb items of data, 每一个size bit的长度,从这个流中读出来

RETURN VALUES
	如果fread(), fwrite()成功了,就会返回the numbers of items read or written
	这个数字等于什么呢,等于
	如果失败了,或者读到了文件的末尾,返回值就会变成一个小的item count或者是0

	要区分是读到文件末尾还是读错了,那就需要用到, feof(3), ferror(3)来判断
man 3 fclose
flose 把这个流给他关闭
FCLOSE(3)

SYNOPSIS
	#include <stdio.h>
	int fclose(FILE *fp)

如果用来关闭一个不合法的文件指针,也不知道会发生什么;
我们在实际使用的时候,一般都会认为自己做的close操作是成功的
man fgets

char *fgets(char *s, int size, FILE *stream)
遇到换行会停止,也就是说是每次都读一行;
1. 把内容放在s指针中
2. 我还会返回也是一个指针

DESCRIPTION
		fgets() 读入,如果一个新的行被读入,他的回车也会被读进去;
man get line 
ssize_t getline(char **lineptr,  size_t *n, FILE *stream);

DESCRIPTION
	getline()从stream中读一个整的行,那么这个\n会被读进去
	如果 *lineptr 被设置为 NULL, 并且
	
	如果buffer不是足够的大,那我需要重新改变一下大小,
	在其他的情况下,*lineptr and *n 都会被更新,是反射一个buffer的地址

EXAMPLE提供一个标准的实践
#define
#include <stdio.h>
#include <stdlib.h>
int main (int arc, char *argv[]) { 
	FILE *stream;
	char *line = NULL;
	size_t len = 0;
	ssize_t nread;

	if (argc != 2) {
		fprintf(stdeer, "Usage:%s <file>\n", argv[0]);
		exit(EXIT_FAILURE);
	}
	stream
}

fopen()//file open
使用的时候用到
FILE *fopen(const char *path, const char *mode);
参数:
@path : 指的是要打开的文件名
@mode:打开的模式在这里插入图片描述

比如说我实现一个cat的功能,也就是去读一个文件,这个时候我这样做

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void read_file(char *pathname);
int main(int argc , char **argv) {
    if (argc < 2) {
        fprintf(stderr, "Usage : %s file...\n", argv[0]);
        exit(1);
    }
    for (int i = 1; i <= argc - 1; i++) {
        printf("%s\n", argv[i]);
        read_file(argv[i]);
    }
    return 0;
}
void read_file(char *pathname) {
    FILE *file = NULL;
    char buffer[1024] = {0};
    if ((file = fopen(pathname, "r") )== NULL) {
        perror("fopen");
        exit(1);
    }
    while (fgets(buffer, sizeof(buffer), file) != NULL) {
        printf("%s", buffer);
        bzero(buffer, sizeof(buffer));
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值