ifstream 说它打开了一个文件,但文件打不开
ifstream says it opened a file but the file doesnt open
我正在使用 visual studio 2017
我是c++的新手,在这里我尝试打开一个txt文件,并确认它已被打开。
#include <iostream>
#include <fstream>
#include <String>
using namespace std;
int main()
{
ifstream infile;
string text;
infile.open("C:\Users\gab_a\source\repos\one\testing.txt");
if (!infile.is_open()) {
cerr << "Specified file could not be found ";
exit(1);
}
else {
cout << "Opened file ";
infile >> text;
cout << text;
}
return 0;
}
它说它打开了它,它甚至读取了文件中的文本,但实际文件没有打开,我什至将文件放在与项目相同的目录中。也没有错误,为什么我的文件打不开?
您正在做的是将文件中的数据读入流中。这与执行打开文件的程序不同。要做到这一点通常是 OS 特定的,但如果您使用 Windows,则可以使用 ShellExecute
或 CreateProcess
。我确实建议您复习一下 C++ - 无意冒犯
我正在使用 visual studio 2017
我是c++的新手,在这里我尝试打开一个txt文件,并确认它已被打开。
#include <iostream>
#include <fstream>
#include <String>
using namespace std;
int main()
{
ifstream infile;
string text;
infile.open("C:\Users\gab_a\source\repos\one\testing.txt");
if (!infile.is_open()) {
cerr << "Specified file could not be found ";
exit(1);
}
else {
cout << "Opened file ";
infile >> text;
cout << text;
}
return 0;
}
它说它打开了它,它甚至读取了文件中的文本,但实际文件没有打开,我什至将文件放在与项目相同的目录中。也没有错误,为什么我的文件打不开?
您正在做的是将文件中的数据读入流中。这与执行打开文件的程序不同。要做到这一点通常是 OS 特定的,但如果您使用 Windows,则可以使用 ShellExecute
或 CreateProcess
。我确实建议您复习一下 C++ - 无意冒犯