我有以下代码
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <unistd.h>
using namespace std;
int main()
{
ifstream asmfile("asmcode",ifstream::in);
if(!asmfile)
{
cout << "error" << endl;
}
return 0;
}
其中“ asmcode”只是一个文本文件,与主文件存在于同一目录中。它实际上是一个通过bash“> dummy.txt”创建的空文件。
我正在使用g ++ 9.2.1作为编译器的Ubuntu Linux 18.04上运行此程序。
这将在我的机器上打印“错误”。我不知道为什么无法打开该文件。我还注意到,如果我在文件名后附加文件格式,则会出现分段错误,这更加奇怪。
这里怎么了?
您应该添加文件扩展名,否则程序无法在两个具有相同名称的文件之间产生区别。
ifstream asmfile("asmcode.txt", ifstream::in);