格式化IO
方法 描述 fprintf Write formatted data to stream (function ) 向文件格式化写入 fscanf Read formatted data from stream (function ) 向文件格式化读出 sprintf Write formatted data to string (function ) 向C字符串读出 sscanf Read formatted data from string (function ) 向C字符串写入 printf Print formatted data to stdout (function ) 向标准输出给石化读出 scanf Read formatted data from stdin (function ) 向标准输入格式化写入
字符字符串IO
参数 描述 fgetc(函数实现) Get character from stream (function ),getc(宏实现 ) fputc(函数实现) Write character to stream (function ) putc(宏实现) fgets Get string from stream (function ) fputs Write string to stream (function ) getchar Get character from stdin (function ) putchar Write character to stdout (function ) gets Get string from stdin (function ) puts Write string to stdout (function )
直接IO(二进制IO)
二进制读写,在读文件的时候加b
方法 描述 fread Read block of data from stream (function ) fwrite Write block of data to stream (function )
fread
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream )
Read block of data from stream
fread()──从fp所指向文件的当前位置开始,一次读入size个字节,重复count次,并将读入的数据存放到从buffer开始的内存中; 读写的是数据块,既size_t size
参数|描述 –|-- ptr|block of memory with a size of at least (sizecount) bytes,converted to a void size|Size, in bytes, of each element to be read. count|Number of elements, each one with a size of size bytes. stream|Pointer to a FILE object
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
int main ( )
{
FILE * fp = fopen ( "test.txt" , "r" ) ;
char arr[ 5 ] = { 0 , 0 , 0 , 0 , 0 } ;
fread ( arr, 2 , 2 , fp) ;
printf ( "%s\n" , arr) ;
fclose ( fp) ;
system ( "pause" ) ;
return ( 0 ) ;
}
fwrite
和fread对应
int 类型读写
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
int main ( )
{
FILE * pFile = fopen ( "test.txt" , "wb+" ) ;
int wbuffer[ ] = { 1 } ;
fwrite ( wbuffer, sizeof ( int ) , 1 , pFile) ;
rewind ( pFile) ;
int rbuffer[ ] = { 0 } ;
fread ( rbuffer, sizeof ( int ) , 1 , pFile) ;
printf ( "%d\n" , rbuffer[ 0 ] ) ;
system ( "pause" ) ;
fclose ( pFile) ;
return 0 ;
}
结构体读写
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int age;
char name[ 10 ] ;
} person;
int main ( )
{
FILE * pFile = fopen ( "test.txt" , "wb+" ) ;
person wper;
wper. age = 10 ;
strcpy ( wper. name, "wang" ) ;
fwrite ( & wper, sizeof ( person) , 1 , pFile) ;
rewind ( pFile) ;
person rper;
fread ( & rper, sizeof ( person) , 1 , pFile) ;
printf ( "%d %s\n" , rper. age, rper. name) ;
system ( "pause" ) ;
}
参考: http://www.cplusplus.com/reference/cstdio/ http://www.gnu.org/software/libc/manual/html_node/I_002fO-on-Streams.html#I_002fO-on-Streams