What is the specific error? I wrote it myself and tried it. It can be opened. You can copy it and try it. Could it be that I forgot to read from the file, or did I use cin to read?
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream in("D:\zz.txt");
string res;
while (getline(in, res))
{
cout << res << endl;
}
return 0;
}
Let’s try this one below. . . , directly read the characters into the file, and then open it, and see if it can be opened successfully? I can try it, can you try it? . . .
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ofstream ou("D:\zz.txt");
string test("hello world");
ou << test << endl;
ifstream in("D:\zz.txt");
if (in.is_open())
{
cout << "opened!\n";
}
string res;
while (getline(in, res))
{
cout << res << endl;
}
return 0;
}
What is the specific error? I wrote it myself and tried it. It can be opened. You can copy it and try it. Could it be that I forgot to read from the file, or did I use cin to read?
Let’s try this one below. . . , directly read the characters into the file, and then open it, and see if it can be opened successfully? I can try it, can you try it? . . .
Does the file being opened exist?