Input character
头文件#include <stdio.h>函数原型int*fp);从文件流中读取下一个字节,并作为字符返回到达文件尾或出现错误时,返回EOFintgetc*fp);与fgetc()功能类似,但可实现成一个宏int getchar(void);相当于getc(stdin)
Output character
header file #include <stdio.h> function prototype int fputc (int c, file *fp); writes a character to the output file stream int PUTC ( int c, FILE *FP); int putchar (int c), equivalent to PUTC (c, stdout " returns the character that was written when the value was successfully returned otherwise-1
头文件#include <stdio.h>函数原型char *fgets(charint size, FILE *stream);最多读取size-1个字符,并保存在s中遇到EOF或者新行时读操作结束‘\0’保存在s的末尾char *gets(char *s);类似fgets(),但从标准输入读取数据并丢弃遇到的换行符号返回值成功返回指向字符串s的指针失败返回NULL头文件#include <stdio.h>函数原型intfputs(constchar *s, FILE *stream);intputs(constchar *s);
Examples of string read and write operations
#include <stdio.h> Main () {FILE *fp; char string [81 ]; if ((Fp=fopen ( "file.txt" , "W" )) ==null) {printf ( "Cann ' t Open file" ); exit (0 ); } while (strlen ( string )) >0 ) {fputs ( String , FP); fputs (, FP); }
fclose(fp); if((fp=fopen("file.txt","r"))==NULL){ printf("cann‘t open file"); exit(0); } while(fgets(string,81,fp)!=NULL) puts(string); fclose(fp);}
Format I/O
获取外部数据函数头文件#include <stdio.h>函数原型int fprintf(FILE constchar *format, …)int fscanf(FILE constchar *format, …)功能按格式对文件进行I/O操作返值成功,返回I/O的个数出错或文件尾,返回EOF
Formatting I/O usage examples
#include <stdio.h> Main () {char s[80 ],c[80 ]; int A, B; FILE *FP; if ((Fp=fopen ( "test" , "W" )) ==null) {puts ( "can ' t open file" ); exit () ; }
scanf ( " %s %d ", s , &a); fprintf (Fp, "%s %d ", s , a); Fclose (FP); if ((Fp=fopen ( "test" , "R" )) ==null) {puts ("can ' t open file"); exit (); } fscanf (Fp, "%s %d ", c,&b); printf ( "%s %d " , c,b); Fclose (FP);}
Stream file input/output
freadsize*fp);sizefwritesize*fp);参数说明buffer: 指向要输入/输出数据块的首地址的指针size: 每个要读/写的数据块的大小(字节数)count: 要读/写的数据块的个数fp: 要读/写的文件指针fread与fwrite 主要用于二进制文件的输入/输出返值成功,返回读/写的块数出错或文件尾,返回0
Casesfloatf[2]; FILE *FP; Fp=fopen ("Aa.dat", "RB"); Fread (F,4,2, FP); for(i=0;i<2; i++) {fread (&f[i],4,1, FP); f[i]+=Ten; } ExamplestructStudent {intNumCharname[ -];CharSexintAgefloatscore[3]; }stud[Ten]; for(i=0;i<Ten; i++) fread (&stud[i],sizeof(structStudent),1, FP);
Input/output sample for stream file
voidDisplay () {FILE *fp;intIif((Fp=fopen ("D:\\fengyi\\exe\\stu_dat","RB")) ==null) {printf("Cannot open file\n");return; } for(i=0; i<size;i++) {fread (&stud[i],sizeof(structStudent_type),1, FP);printf("%-10s%4d%4d%-15s\n", Stud[i].name, STUD[I].NUM,STUD[I].AGE,STUD[I].ADDR); } fclose (FP);}#include <stdio.h>#define SIZE 4structstudent_type{Charname[Ten];intNumintAgeCharaddr[ the];} Stud[size];main () {intI for(i=0; i<size;i++)scanf("%s%d%d%s", Stud[i].name,&stud[i].num, &STUD[I].AGE,STUD[I].ADDR); Save (); Display ();}
File location
Location of files
File position pointer--pointer to the current read and write location
Read/write mode
Sequential read and write: Each time the last read or write the next position as the starting point for this read or write
Random Read and write: position pointer moves to any position as needed
Rewind function
Function prototypes
void Rewind (FILE *fp)
Function
Resets the file location pointer to the beginning of the file
return value
No
File Location Example
#include <stdio.h>main(){ *fp1,*fp2; fp1=fopen("d:\\fengyi\\bkc\\ch12_4.c","r"); fp2=fopen("d:\\fengyi\\bkc\\ch12_41.c","w"); while(!feof(fp1)) putchar(getc(fp1)); rewind(fp1); while(!feof(fp1)) putc(getc(fp1),fp2); fclose(fp1); fclose(fp2);}
fseek function
Function prototype: int fseek (FILE *fp,long offset,int whence)
Function: Change the position of the file position pointer
Return value: succeeded, returned 0; failed, return non 0 value
Ftell function
Function prototypes
Long Ftell (FILE *FP)
Function: Returns the position of the pointer at the current position (represented by the amount of displacement relative to the beginning of the file)
Return value: Successful, returns the current position pointer position; failed, return -1l
Main () {intI FILE*FP;if((Fp=fopen ("Studat","RB")) ==null) {printf("can ' t open file\n");Exit(0); } scanf ("%d", &i); Fseek (fp,i*sizeof(struct Student_type),0); Fread (&stud,sizeof (struct student_type),1, FP);printf('%s%d%d%s\ n ' , STUD.NAME,STUD.NUM,STUD.AGE,STUD.ADDR); Fclose (FP);}
Ferror () function
Ferror () function
Function prototype: int ferror (FILE *fp)
Function: Test file for errors
Return value: no error, 0; error, not 0
Description
Each call to the file input and output function produces a new value for the ferror () function, so it should be tested in time
fopen () When opening a file, the initial value of the ferror () function automatically resets to 0
Clearerr () function
Function prototype: void Clearerr (FILE *fp)
Function: Set the file error flag to 0
return value: None
Description
After an error, the error flag remains until the same file is Clearerr (FP) or rewind or any other input/output function
#include <stdio.h>int main(void){ *stream; stream = fopen("DUMMY.FIL""w"); getc(stream); if (ferror(stream)) { printf("Error reading from DUMMY.FIL\n"); clearerr(stream); } if(!ferror(stream)) printf("Error indicator cleared!"); fclose(stream); return0;}
File Stream Caching operations
缓存分类块缓存行缓存无缓存相关函数头文件#include <stdio.h>函数原型voidchar *buf);intcharint mode, size_t size);_IOFBF:块缓存_IOLBF:行缓存_IONBF:无缓存
Stream and file descriptors
确定流使用的底层文件描述符#include <stdio.h>int fileno(FILE *fp);根据已打开的文件描述符创建一个流#include <stdio.h>FILE *fdopen(intconstchar *mode);
Temporary files
创建临时文件头文件#include <stdio.h>函数原型FILE *tmpfile(void);返回值成功为文件指针出错为NULL为临时文件创建一个文件名头文件#include <stdio.h>函数原型char *tmpnam(char *s);返回值指向唯一路径名的指针
Fcntl () function
function Manipulation file description such as copy, get/Set file descriptor flag, get/Set file status flag, management Recommendation file Lock file #include <unistd.h> #include <fcntl.h> function prototype Span class= "Hljs-keyword" >int fcntl (int FD, int cmd); int fcntl (int fd, int cmd, long arg); int fcntl (int FD, int cmd, struct flock * Lock ); Return value success depends on CMD error return-1 parameter Description fd: File descriptor cmd: action command arg: command using the parameter lock: Ditto
cmd参数说明F_DUPFD:复制文件描述符 FD_CLOEXEC:设置close-on-exec标志F_GETFD:读取文件描述符标志 F_SETFD:设置文件描述符标志 F_GETFL:读取文件状态标志F_SETFL:设置文件状态标志F_GETLK:获取记录锁F_SETLK :释放记录锁F_SETLKW:测试记录锁
Mmap () function
功能mmap将一个文件或者其它对象映射进内存文件被映射到多个页上如果文件的大小不是所有页的大小之和,最后一个页不被使用的空间将会清零头文件#include <sys/mman.h>函数原型void *mmap(voidlengthintintint fildes, off_t off);返回值成功执行时,返回被映射区的指针失败时,mmap()返回MAP_FAILED
参数说明start:映射区的开始地址length:映射区的长度prot:期望的内存保护标志,不能与文件的打开模式冲突,取值类型如下(可通过or运算合理地组合在一起)PROT_EXEC //页内容可以被执行 //页内容可以被读取PROT_WRITE //页可以被写入 //页不可访问
Msync () function
功能把映像的文件写回磁盘,或重新从磁盘中读取文件头文件#include <sys/mman.h>函数原型int msync(voidint flags);返回值成功时,返回0失败时,返回-1
参数说明addr:更新映射区起始地址len:更新映射区的长度flags:控制更新方法MS_ASYNC:执行异步写操作MS_SYNC:执行同步写MS_INVALIDATE:从文件中读回数据
Munmap () function
功能释放内存映射区 头文件#include <sys/mman.h>函数原型int munmap(void *addr, size_t len);返回值成功执行时,返回0失败时,返回-1
Linux-Standard I/O libraries and advanced I/O libraries