标准IO
IO:Input & Output
IO是一切操作的基础,IO分为
- 标准IO (stdio)
- 系统调用IO (sysio) 文件IO
两者都可以使用的情况下优先使用标准IO,标准IO依赖于系统调用IO
sdio 标准IO函数
fopen:(打开文件流)
函数原型:
FILE* fopen(const char *path, const char *mode);
path : 指定打开的文件路径
mode:打开文件的方式
the argument mode points to a string beginning with one of the following sequences.
- r : Open text file for reading.The stream is positioned at the beginning of the file.
- r+ : Open for reading and writing.The stream is position at the beginning of the file.
- w : Truncate file to zero length or create text file for writing.The stream is positioned at the beginning of the file.
- w+ : Open for reading and writing.The file is created if it does not exist,ohterwise it is truncated.The stream is positioned at the beginning of the file.
- a : Open for appending (writing at end of file).The file is created if it does not exist.The stream is positioned at the end of the file.
- a+ : Open for reading and appending (writing at end of file).The file is created if it does not exits.The initial file position for reading is at the beginning of the file,but output is always appended to the end of the file.
返回值说明:
successful return a FILE pointer. Otherwirse, NULL is returned and errno is set to indicate the error.
fclose:(关闭文件流)
函数原型:
int fclose(FILE * fp);
返回值说明:
Upon successful completion 0 is returned.Otherwirse EOF is returned and errno is set to indicate the error .
Linux 一个进程中最大打开文件个数为1024
fgetc:(从流中获取单个字符)
函数原型:
int fgetc(FILE *stream);
函数说明:
reads the next character from stream and returns it as an unsigned char cast to an int , or EOF on end of file or error.
返回值说明:
return the character read as an unsigned char cast to an int or EOF on end of file or error.
fputc:(将字符c写入文件流中)
函数原型:
int fputc(int c,FILE* stream);
函数说明:
writers the character c, cast to an unsigend char, to stream.
返回值说明:
return the character written as an unsigned char cast to an int or EOF on error.
实现mycpy,将srcfile内容拷贝到destfile中
#include<stdio.h>
int main(int argc,char** argv){
if(argc < 3){
fprintf(stderr,"Usage : %s <source_file> <dest_file>",argv[0]);
return -1;
}
FILE *fps = fopen(argv[1],"r");
if(fps == NULL){
perror("fopen() fps");
return -1;
}
FILE *fpd = fopen(argv[2],"w");
if(fpd == NULL){
fclose(fps);
perror("fopen() fpd");
return -1;
}
while(1){
char ch = fgetc(fps);
if(ch == EOF) break;
fputc(ch,fpd);
}
fclose(fps);
fclose(fpd);
return 0;
}
fgets:(从指定流中获取size个字符)
函数原型:
char* fgets(char *s,int size,FILE *stream);
函数说明:
reads in at most on less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline.If a newline is read,it is storedinto the buffer.A terminating null byte(‘\0’) is stored after the last character in the buffer.
返回值说明:
return s on success,and NULL on error or when end of file occurs while no characters have been read.
结束情况:
- 读取到了size - 1个字节(最后一个字节用于添加 ‘\0’ )
- 读取到了 ‘\n’
fputs:(将指定串输入到指定流)
函数原型:
int fputs(const char *s,FILE *stream);
函数说明:
writes the string s to stream,without its terminating null byte(‘\0’).
返回值说明:
return a nonnegative number on success,or EOF on error.
fread:(在指定流中读取nmemb个size大小的数据存储到ptr中)
函数原型:
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
函数说明:
reads nmemb elements of data,each size bytes long,from the stream poninted to by stream,storing them at the location given by ptr.
返回值说明:
return the number of items read or wirtten.If an error occurs, or the end of the file is reached,the return value is a short item count (or zero).
fwrite:(向指定流中写入nmemb个size大小的数据)
函数原型:
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *steam);
函数说明:
writes nmemb elements of data,each size bytes long,to the stream pointed to by stream, obtaining them from the location given by ptr.
返回值说明:
return the number of items read or wirtten.If an error occurs,or the end of the file is reached,the return value is a short item count (or zero).
printf (族)
函数原型:
int printf(const char* format, ..... );
int fprintf(FILE* stream, const char* format, .....);
int sprintf(char* str, const char* format, ..... );
int snprintf(char* str, size_t size, const char* format, .... );
scanf(族)
函数原型:
int scanf(const char* format, ....);
int fscanf(FILE* stream, const char* format, ....);
int sscanf(const char *str, const char* format, ....);
fseek(定位文件文件位置指针)
函数原型:
int fseek(FILE* stream, long offset, int whence);
函数描述:
sets the file position indicator for the stream pointed to by stream.The new position, measured in bytes,is obtained by adding offset bytes to the position specifiled by whence. If whence is set SEEK_SET,SEEK_CUR, or SEEK_END, the offset is realtive to the start of the file, the current position indicator,or end-of-file,respectively.
返回值说明:
successful return 0.Otherwise, -1 is returned and errno is set to indicate the error.
ftell(返回当前文件位置指针的位置)
函数原型:
long ftell(FILE* stream);
函数描述:
obtain the current value of the file position indicator for the stream pointed to by steam.
返回值说明:
return the current offset.Otherwise, -1 is returned and errno is set to indicate the error.
rewind(文件位置指针返回文件开头位置)
函数原型:
void rewind(FILE* stream);
函数描述:
sets the file position indicator for the stream pointed to by stream to the beginning of the file.Is is equivalent to:
void fseek(stream,0L,SEEK_SET);
fflush(刷新流)
函数原型:
int fflush(FILE* stream);
返回值说明:
Upon successful completion 0 is returned. Otherwise, EOF is returned and errno is set to indicate the error
缓冲区
作用:合并系统调用
- 行缓冲:换行时刷新、满了的时候刷新、强制刷新,如stdio,因为是终端设备
- 全缓冲:满了的时候刷新、强制刷新(默认情况,除了终端设备)
- 无缓冲:需要立刻输出的内容,如stderr
setvbuf函数用于设置文件缓冲类型
实现getline函数功能
ssize_t myGetline(char **lineptr,size_t *n,FILE* stream){
char* buf = *lineptr;
char c = ' ';
int index = 0;
if(buf == NULL || *n == 0){
*lineptr = malloc(sizeof(char) * 10);
if(*lineptr == NULL) return -1;
buf = *lineptr;
*n = 10;
}
while((c = fgetc(stream)) != '\n'){
if(c == EOF){
return -1;
}
if(index < *n - 2){
buf[index++] = c;
}else{
*n += 10;
buf = realloc(buf,*n);
buf[index++] = c;
}
}
buf[index++] = '\n';
buf[index] = '\0';
*lineptr = buf;
return index;
}
临时文件:
- 如何创建临时文件
- 如何创建临时文件不冲突
- 如何销毁临时文件
tmpnam
函数原型:
char* tmpnam(char* s);
返回值说明:
The function returns a pointer to a unique temporary filenaem, or NULL if a unique name cannot by generated.
tmpfile
函数原型:
FILE* tmpfile(void);
函数说明:
The function opens a unique temporary file in binary read/write (w+b) mode. The file will be automatically deleted when it is closed and the program terminates.
返回值说明:
The function returns a stream descriptor, or NULL if a unique filename cannot by generated or the unique file cannot be opened.In the latter case, errno is set to indicate the error.