我试图从fstream读取数据但这段代码不起作用。 它将0置于控制台。 你能救我吗?
// g++ -Wall main.cpp -o main.exe
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::fstream file("main.txt", std::fstream::in | std::fstream::out | std::fstream::app);
file << "45634w6\n";
file << "dtusrjt\n";
while (!file.eof())
{
std::string line;
std::cout << std::getline(file, line) << "\n";
}
int a;
std::cin >> a;
}
答案 0 :(得分:0)
试试这段代码,这段代码会帮助你
#include <fstream>
#include <iostream>
#include <string>
//#include <sstream>
using namespace std;
int main()
{
fstream file("main.txt");
file << "45634w6\n";
file << "dtusrjt\n";
file.close();
file.open("main.txt");
string line;
while (!file.fail())
{
getline(file, line);
cout <<line << "\n";
}
int a;
cin >> a;
}