C/C++文件操做

C语言中的经常使用文件操做函数:ios

fopen(),fread(),fwrite();数组

须要的头文件为stdio.h stdlib.h  memory.h  string.happ

 

套路:ide

1.定义一个文件指针函数

    FILE *file;指针

2.定义缓冲区对象

    char  read_buff[1024];字符串

    char  write_buff[1024] = "hello world";string

2用fopen(文件路径,打开方式)使file指向所须要打开的文件it

    file =  fopen(“D:\\hello.txt”,"r");

   // file = fopen("D:\\hello.txt", "a+");

3 使用fwrite(字符数组,字符串长度,写入个数,文件指针)或fread(接收缓冲数组,数组长度,读取个数,文件指针)对文件进行读写       (若是是读取数据,通常要对缓冲区进行格式化为0)

    memset(read_buff, 0, sizeof(read_buff));

    fread(read_buff,sizeof(read_buff), 1,file);

 

   // fwrite(write_buff,strlen(write_buff),1,file);

 

C++中的文件操做

 

须要的头文件 iostream  fstream

套路:

1. 定义一个ofstream 或者ifstream对象

ofstream fout;

2.打开文件,open(文件路径,打开方式)

fout.open("D:\\hello.txt",ios_base::app);

3.读写操做

fout<<"hello world"; 

相关文章
相关标签/搜索