怎么用c++写一个程序,std::cin用户输入一个txt文件名,程序用ifstream读取并打出来。

如题所述

参考程序【编译环境 Dev C++】

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    string fileName = "";
    cin >> fileName; //获取文件名 
    ifstream file(fileName.c_str()); //打开文件 
    char buffer[16384];
    while(!file.eof())
    {
         file.getline(buffer, 16383); //一行中只会读取16383个字符,过多会忽略掉 
         cout << buffer << endl; //输出 
    }
    cin.get();cin.get(); //只是为了在屏幕上保留输出结果,可以注释掉 
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
相似回答