在C/C++语言开发中,文件读写很常用的操作。文件读写主要涉及文本文件和二进制文件,今天就对其使用进行总结:
1.C/C++标准跨平台读写文件
1)C语言读写文件通过FILE指针操作,文本文件读写用fgetc, fgets, fprintf,fscanf,二进制文件读写用fread,fwrite
2)C++读写文件通过fstream,ifstream,ofstream进行操作,构造函数中指定文件路径时内部会调用open(),如果再次调用open(),
调用将会返回失败,文本文件用<< 和 >> 进行读写,二进制文件用read和write进行读写
2.windows下文件读写
1.系统API
使用VC++读写文件,最直接、最高效的方法就是使用 Windows API,如:使用 CreateFile 打开文件,使用 WriteFile 写文件,使用 ReadFile 读文件……Windows 平台下,所有对文件的读写操作,最终都会调用这些 API 函数。使用 API 的效率最高,对文件读写的控制最强,缺点就是比较复杂,而且代码没有可移植性。
1.2类linux系统IO
VC++的C运行时库实现了一套类似linux的IO函数,头文件是io.h, 如:_wsopen_s,_open,_write,_read ...
3.linux下文件读写
linux系统api,如open,read,write等
4.标准C语言操作文件
1)文本文件
#include
#include void cReadWriteTxtFile() {// r+ r:操作的文件必须存在, 读写都是从文件开头开始// w:写文件,清除原有文件内容 w+:写文件,清除原有文件内容// a:附加方式打开写文件,文件不存在会创建 a +:附加方式读写文件,读从文件开头, 写从文件末尾开始,文件不存在会创建FILE * fp = fopen("c_txt_file.txt", "w");if (!fp){printf("write mode open file error!\n");return;}int i = 1;while (i <= 200) {fprintf(fp, "%d %s\n", i++, "C operate txt file");}fclose(fp);fp = NULL;// r+ 和 r打开文件时候,文件必须存在,否则文件指针为空fp = fopen("c_txt_file.txt", "r+");if (!fp){printf("read mode open file error!\n");return;}char buffer[1024] = {0};int line_num = 0;while (!feof(fp)){memset(buffer, 0, sizeof buffer);//fgets可以从文件中读取一行字符串,fgets()函数自带回车符 fgets(buffer, 1024, fp);//注意这行代码,当fp指向EOF的下一个位置时,feof才会返回trueif (feof(fp)) {break;}printf("line %d: %s", ++line_num, buffer); //输出}fclose(fp);fp = NULL;
}int main()
{cReadWriteTxtFile();return 0;
}
2)二进制文件
#include
#include
#include void cReadWriteBinFile() {FILE * fp = fopen("c_bin_file.dat", "wb");if (!fp){printf("write mode open file error!\n");return;}int value;int unit_size = sizeof(int);for (int i = 0; i < 100; i++) {value = i;fwrite(&value, sizeof(int), 1, fp);}fclose(fp);fp = NULL;// r+ 和 r打开文件时候,文件必须存在,否则文件指针为空fp = fopen("c_bin_file.dat", "rb+");if (!fp){printf("read mode open file error!\n");return;}//计算文件大小fseek(fp, 0, SEEK_END);long file_size = ftell(fp);rewind(fp); // <====> fseek(fp, 0L, SEEK_SET);int e_num = file_size / unit_size;int *parr = (int*)malloc(file_size);for (int i = 0; i < e_num; i++) {fread(&parr[i], unit_size, 1, fp);printf("%d %d\n", i, (int)parr[i]);}free(parr);fclose(fp);fp = NULL;
}int main()
{cReadWriteBinFile();return 0;
}
5.标准C++语言操作文件
1)文本文件
#include
#include
#include
#include using namespace std;//文件打开方式选项:
// ios::in = 0x01, //供读,文件不存在则创建(ifstream默认的打开方式)
// ios::out = 0x02, //供写,文件不存在则创建,若文件已存在则清空原内容(ofstream默认的打开方式)
// ios::ate = 0x04, //文件打开时,指针在文件最后。可改变指针的位置,常和in、out联合使用
// ios::app = 0x08, //供写,文件不存在则创建,若文件已存在则在原文件内容后写入新的内容,指针位置总在最后
// ios::trunc = 0x10, //在读写前先将文件长度截断为0(默认)
// ios::nocreate = 0x20, //文件不存在时产生错误,常和in或app联合使用
// ios::noreplace = 0x40, //文件存在时产生错误,常和out联合使用
// ios::binary = 0x80 //二进制格式文件void cppReadByByte() {fstream fin;fin.open("cpp_txt.txt", ios::in);//按字节读取char ch;while (EOF != (ch = fin.get()))cout << ch;fin.close();
}void cppReadByLine() {fstream fin;fin.open("cpp_txt.txt", ios::in);char line_buf[1024];int read_num;fin.getline(line_buf, sizeof(line_buf));cout << line_buf << "\t" << endl;//tellg() 得到文件指针当前指向的文件位置。//seekg(0, ios::beg); //让文件指针定位到文件开头//seekg(0, ios::end); //让文件指针定位到文件末尾//seekg(10, ios::cur); //让文件指针从当前位置向文件末方向移动10个字节//seekg(-10, ios::cur); //让文件指针从当前位置向文件开始方向移动10个字节//seekg(10, ios::beg); //让文件指针定位到离文件开头10个字节的位置while (!fin.eof()){fin.getline(line_buf, sizeof(line_buf));if (fin.eof()) {break;}//gcount()表示实际读取的字节数read_num = fin.gcount();cout << read_num << ": \t" << line_buf << endl;}fin.close();
}void cppReadByFormat() {fstream fin;fin.open("cpp_txt.txt", ios::in);int first = 1, second = 2, third = 3;string str;while (!fin.eof()){//此处注意,读数值会跳过空格和tab,无论是数字或者字符串fin >> first >> second >> third >> str;if (fin.eof()) {break;}cout << first << '\t' << second << "\t" << third << ends << "\t\t\t\t" << str << std::endl;}fin.close();
}void cppReadWritetxtFile() {fstream fout("cpp_txt.txt", ios::out);if (!fout.is_open()){cout << "open cpp_txt.txt error !" << endl;return;}for (int i = 0; i < 10; i++) {fout << setw(5) << i + 10 << "\t" << setw(10) << i + 1 << "\t" << setw(10) << i + 2 << "hello " << endl;}fout.close();cppReadByFormat();
}int main()
{cppReadWritetxtFile();return 0;
}#include
#include
#include
#include using namespace std;//文件打开方式选项:
// ios::in = 0x01, //供读,文件不存在则创建(ifstream默认的打开方式)
// ios::out = 0x02, //供写,文件不存在则创建,若文件已存在则清空原内容(ofstream默认的打开方式)
// ios::ate = 0x04, //文件打开时,指针在文件最后。可改变指针的位置,常和in、out联合使用
// ios::app = 0x08, //供写,文件不存在则创建,若文件已存在则在原文件内容后写入新的内容,指针位置总在最后
// ios::trunc = 0x10, //在读写前先将文件长度截断为0(默认)
// ios::nocreate = 0x20, //文件不存在时产生错误,常和in或app联合使用
// ios::noreplace = 0x40, //文件存在时产生错误,常和out联合使用
// ios::binary = 0x80 //二进制格式文件void cppReadByByte() {fstream fin;fin.open("cpp_txt.txt", ios::in);//按字节读取char ch;while (EOF != (ch = fin.get()))cout << ch;fin.close();
}void cppReadByLine() {fstream fin;fin.open("cpp_txt.txt", ios::in);char line_buf[1024];int read_num;fin.getline(line_buf, sizeof(line_buf));cout << line_buf << "\t" << endl;//tellg() 得到文件指针当前指向的文件位置。//seekg(0, ios::beg); //让文件指针定位到文件开头//seekg(0, ios::end); //让文件指针定位到文件末尾//seekg(10, ios::cur); //让文件指针从当前位置向文件末方向移动10个字节//seekg(-10, ios::cur); //让文件指针从当前位置向文件开始方向移动10个字节//seekg(10, ios::beg); //让文件指针定位到离文件开头10个字节的位置while (!fin.eof()){fin.getline(line_buf, sizeof(line_buf));if (fin.eof()) {break;}//gcount()表示实际读取的字节数read_num = fin.gcount();cout << read_num << ": \t" << line_buf << endl;}fin.close();
}void cppReadByFormat() {fstream fin;fin.open("cpp_txt.txt", ios::in);int first = 1, second = 2, third = 3;string str;while (!fin.eof()){//此处注意,读数值会跳过空格和tab,无论是数字或者字符串fin >> first >> second >> third >> str;if (fin.eof()) {break;}cout << first << '\t' << second << "\t" << third << ends << "\t\t\t\t" << str << std::endl;}fin.close();
}void cppReadWritetxtFile() {fstream fout("cpp_txt.txt", ios::out);if (!fout.is_open()){cout << "open cpp_txt.txt error !" << endl;return;}for (int i = 0; i < 10; i++) {fout << setw(5) << i + 10 << "\t" << setw(10) << i + 1 << "\t" << setw(10) << i + 2 << "hello " << endl;}fout.close();cppReadByFormat();
}int main()
{cppReadWritetxtFile();return 0;
}
2)二进制文件
#include
#include
#include
#include using namespace std;//说明:C++读写二进制数据需要使用 ifstream和ofstream,读写文本文件,ifstream和ofstream和fstream都可以
void cppReadWriteBinFile() {;ofstream fout;fout.open("cpp_bin.dat", ios::binary);if (!fout){cout << "open cpp_bin.dat error !" << endl;return;}int value = 0;for (int i = 0; i < 10; i++) {value = i;fout.write((char*)&value, sizeof(int));}fout.close();ifstream fin;fin.open("cpp_bin.dat", ios::binary);int num = 0;while (!fin.eof()){fin.read((char*)&num, sizeof(int));if (fin.eof()) {break;}cout << num << std::endl;}fin.close();}int main()
{cppReadWriteBinFile();return 0;
}