C++ 使用 fstream 读取文件

C++ Using fstream to read from a file

本文关键字:文件 读取 fstream 使用 C++      更新时间:2023-10-16

我使用Putty进行编码,所以我的问题虽然范围一般,但从该程序的角度提出。

您如何从命令行获取文件并将其逐行读取到主程序中,以便可以在函数中对其进行打包和操作?我知道您使用 fstream 类,但我不确定实际从文件中读取行的正确过程

这是一个简单的示例:

ifstream in("file.txt");
if (!in.is_open())
{
    cout << "Error - cannot open the file." << endl;
    return 0;
}
string line;
while (getline(in, line))
{
    cout << line << endl;
    ...
}