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";