使用 ifstream 读取文件
读取文本和数据
使用 ifstream 读取文件 是 CoddyKit 上的免费 C++ Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 C++ Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 C++ Academy 课程共包含 4 节课。
什么是输入文件流
std::ifstream(输入文件流)从文件中读取数据。它的行为类似于 std::cin,但来源是磁盘上的文件。
- 定义于
<fstream>。 - 在构造函数中按名称打开文件。
#include <iostream>
#include <fstream>
int main() {
std::ofstream("demo.txt") << "hello\n";
std::ifstream in("demo.txt");
std::string word;
in >> word;
std::cout << word << '\n';
return 0;
}打开文件
使用文件名构造一个输入文件流,然后使用 is_open() 检查是否找到了文件。
#include <iostream>
#include <fstream>
int main() {
std::ofstream("data.txt") << "42\n";
std::ifstream in("data.txt");
std::cout << (in.is_open() ? "opened" : "failed") << '\n';
return 0;
}读取单词
>> 运算符会读取由空白分隔的词元,就像使用 cin 时一样。
#include <iostream>
#include <fstream>
int main() {
std::ofstream("words.txt") << "alpha beta gamma\n";
std::ifstream in("words.txt");
std::string w;
while (in >> w) std::cout << w << '\n';
return 0;
}读取数字
您可以直接提取数值类型。流提取会将文本解析为相应的类型。
#include <iostream>
#include <fstream>
int main() {
std::ofstream("nums.txt") << "10 20 30\n";
std::ifstream in("nums.txt");
int sum = 0, x;
while (in >> x) sum += x;
std::cout << "sum = " << sum << '\n';
return 0;
}使用逐行读取函数读取行
使用 std::getline() 读取包括空格在内的整行内容,直到换行符为止。
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::ofstream("lines.txt") << "first line\nsecond line\n";
std::ifstream in("lines.txt");
std::string line;
while (std::getline(in, line)) std::cout << "[" << line << "]\n";
return 0;
}读取整个文件
要一次性读取整个文件,请使用流缓冲区迭代器将其读入字符串。
#include <iostream>
#include <fstream>
#include <sstream>
int main() {
std::ofstream("all.txt") << "line1\nline2\n";
std::ifstream in("all.txt");
std::stringstream ss;
ss << in.rdbuf();
std::cout << ss.str();
return 0;
}检测文件结束
读取循环会在提取失败时结束,通常是因为到达了文件末尾。在条件中测试流即可检查这一点。
#include <iostream>
#include <fstream>
int main() {
std::ofstream("eof.txt") << "1 2 3\n";
std::ifstream in("eof.txt");
int x, count = 0;
while (in >> x) ++count;
std::cout << "read " << count << " numbers\n";
std::cout << "eof: " << std::boolalpha << in.eof() << '\n';
return 0;
}处理文件缺失
如果文件不存在,流就会处于失败状态,并且 is_open() 为 false。读取前务必进行检查。
#include <iostream>
#include <fstream>
int main() {
std::ifstream in("does_not_exist_12345.txt");
if (!in) {
std::cout << "could not open file\n";
}
return 0;
}解析结构化行
将 getline 与 istringstream 结合使用,可以从每一行中解析出各个字段。
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
int main() {
std::ofstream("people.txt") << "Alice 30\nBob 25\n";
std::ifstream in("people.txt");
std::string line;
while (std::getline(in, line)) {
std::istringstream ss(line);
std::string name; int age;
ss >> name >> age;
std::cout << name << " is " << age << '\n';
}
return 0;
}显式 open 和 close
您可以稍后使用 open() 打开默认构造的流,并使用 close() 释放它。
#include <iostream>
#include <fstream>
int main() {
std::ofstream("oc.txt") << "data\n";
std::ifstream in;
in.open("oc.txt");
std::string s; in >> s;
in.close();
std::cout << s << '\n';
return 0;
}RAII 会自动关闭
当一个 ifstream 离开作用域时,它的析构函数会自动关闭文件。手动调用 close() 很少有必要。
#include <iostream>
#include <fstream>
int main() {
std::ofstream("raii.txt") << "x\n";
{
std::ifstream in("raii.txt");
std::string s; in >> s;
std::cout << s << '\n';
} // file closed here automatically
std::cout << "closed\n";
return 0;
}快速检查
请检验您对读取行的理解。
回顾
您了解到,std::ifstream:
- 使用
>>和std::getline从文件中读取内容 - 应使用
is_open()或通过测试流来检查状态 - 离开作用域时会通过 RAII 自动关闭
接下来,您将使用 ofstream 写入文件。
常见问题解答
「使用 ifstream 读取文件」课时是免费的吗?
是的 — 「使用 ifstream 读取文件」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 C++ Academy 课程的其余内容,请升级到 CoddyKit PRO。 C++ Academy 课程共包含 4 节课。
「使用 ifstream 读取文件」这节课中我会学到什么?
读取文本和数据 你通过在浏览器中直接运行的动手代码来练习 C++ Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 C++ Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 C++ Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「使用 ifstream 读取文件」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 C++ Academy 课中编写并运行代码吗?
能。每节 C++ Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 使用 ifstream 读取文件
- 使用 ofstream 写入文件
- 二进制文件输入输出
- 错误处理与状态