c++文件操作
在C++中,文件操作属于技术的应用,不是基本语法(有疑问?)
数据持久化的两种方式:文件和数据库
实际开发中,文件操作无处不在
C++ 将I/O抽象成文件操作
C++ I/O 继承关系(待补充)
写入文本文件
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
//ofstream fout; // 1.创建文件输出流对象
// 2.打开文件,如果文件不存在,则创建它。
//fout.open("test.txt"); // 当前目录生成test.txt文件
// 2.1 文件名一般用全路径,书写的方法如下:
// 1)"E:\WorkSpaces\Data\test.txt" // 错误。
// 2)R"(E:\WorkSpaces\Data\test.txt)" // 原始字面量,C++11标准。
// 3)"E:\\WorkSpaces\\Data\\test.txt" // 转义字符。(慎用?)
// 4)"E:/WorkSpaces/Data/test.txt" // 把斜线反着写。
// 5)/home/user/WorkSpaces/Data/test.txt // Linux系统采用的方法。
//open()函数有重载,可以接受C-style字符串和string类型
string sFileName = "E:/WorkSpaces/Data/test.txt";
//char sFileName[] = "E:\\WorkSpaces\\Data\\test.txt";
//fout.open(sFileName);
//ofstream fout(sFileName); // 构造函数有重载,函数中会打开文件。相当于1和2步合二为一。
// 2.2 打开模式 如果原文件存在,ios::out,ios::trunc,缺省默认都会清空原文件内容
// ios::app 表示在末尾添加
//ofstream fout(sFileName, ios::out);
//ofstream fout(sFileName, ios::trunc);
ofstream fout(sFileName, ios::app);
// 判断打开文件是否成功。
// 失败的原因主要有:1)目录不存在;2)磁盘空间已满;3)没有权限,Linux平台下很常见。
if (fout.is_open() == false)
{
cout << "打开文件" << sFileName <<"失败\n";
return 0;
}
// 3.向文件中写入数据。
fout << "\t望庐山瀑布\n";
fout << "\t作者:李白\n";
fout << "日照香炉生紫烟,遥看瀑布挂前川。\n";
fout << "飞流直下三千尺,疑是银河落九天。\n";
fout.close(); // 4.关闭文件,fout对象失效前会自动调用close()。
cout << "文件操作完成\n";
}
读取文本文件
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string sFileName = "E:/WorkSpaces/Data/test.txt";
ifstream fin(sFileName);
// 判断打开文件是否成功。
// 失败的原因主要有:1)目录不存在;2)磁盘空间已满;3)没有权限,Linux平台下很常见。
if (fin.is_open() == false)
{
cout << "打开文件" << sFileName <<"失败\n";
return 0;
}
// 3.读取文件中内容
// 3.1 方法一
string sBuffer;
while (getline(fin, sBuffer))
{
cout << sBuffer << endl;
}
// 3.2 方法二
/*while (fin >> sBuffer)
{
cout << sBuffer << endl;
}*/
fin.close(); // 4.关闭文件,fout对象失效前会自动调用close()。
cout << "文件操作完成\n";
}
位操作 知识需要一些基础
写入二进制文件
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string sFileName = "E:/WorkSpaces/Data/test.dat";
ofstream fout(sFileName, ios::binary);
if (fout.is_open() == false)
{
cout << "打开文件" << sFileName <<"失败\n";
return 0;
}
struct stPoem
{
char Name[11];
int iAge;
char Poem[256];
};
struct stPoem poem;
poem = { "李白", 24, "飞流直下三千尺,疑是银河落九天。" };
fout.write((char *)&poem, sizeof(poem));
fout.close();
cout << "文件操作完成\n";
}
读取二进制文件
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string sFileName = "E:/WorkSpaces/Data/test.dat";
ifstream fin(sFileName, ios::binary);
if (fin.is_open() == false)
{
cout << "打开文件" << sFileName <<"失败\n";
return 0;
}
struct stPoem
{
char Name[11];
int iAge;
char Poem[256];
};
struct stPoem poem;
while (fin.read((char *)&poem, sizeof(poem)))
{
cout << "姓名:" << poem.Name << " 年龄:" << poem.iAge << " 诗句:" << poem.Poem << endl;
}
fin.close();
cout << "文件操作完成\n";
}
操作文本文件和二进制文件的一些细节
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string sFileName = "E:/WorkSpaces/Data/endl.txt";
//ofstream fout(sFileName, ios::binary); // 文本模式
//if (fout.is_open() == false)
//{
// cout << "打开文件" << sFileName <<"失败\n";
// return 0;
//}
//fout << "abcde"; // 无换行符,大小5字节
// 1)在windows平台下,文本文件的换行标志是"\r\n"。
// 2)在linux平台下,文本文件的换行标志是"\n"。
//fout << "abcde" << endl; // 加入换行符,大小7字节。如果是二进制模式,就是6字节
// 3)在windows平台下,如果以文本方式打开文件,写入数据的时候,系统会将"\n"转换成"\r\n";读取数据的时候,系统会将"\r\n"转换成"\n"。
// 如果以二进制方式打开文件,写和读都不会进行转换。
// 4)在Linux平台下,以文本或二进制方式打开文件,系统不会做任何转换。
// 5)以文本方式读取文件的时候,遇到换行符停止,读入的内容中没有换行符;
// 以二制方式读取文件的时候,遇到换行符不会停止,读入的内容中会包含换行符(换行符被视为数据)。
//fout.close();
ifstream fin(sFileName, ios::binary);
char buffer[15];
memset(buffer, 0, sizeof(buffer));
fin.read(buffer, 15);
for (auto cc : buffer)
cout << (int)cc << endl;
cout << "文件操作完成\n";
}
文件操作-随机存取
fstream类
fstream类既可以读文本/二进制文件,也可以写文本/二进制文件。
fstream类的缺省模式是ios::in | ios::out,如果文件不存在,则创建文件;但是,如果文件存在,不会清空文件原有的内容,而是会覆盖。
普遍的做法是:
1)如果只想写入数据,用ofstream;如果只想读取数据,用ifstream;如果想写和读数据,用fstream,这种情况不多见。不同的类体现不同的语义。
2)在Linux平台下,文件的写和读有严格的权限控制。(需要的权限越少越好)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string sFileName = "E:/WorkSpaces/Data/endl.txt";
fstream fout(sFileName);
if (fout.is_open() == false)
{
cout << "打开文件" << sFileName <<"失败\n";
return 0;
}
fout << "fghijk";
fout.close();
cout << "文件操作完成\n";
}