c++ - 为什么打开带有格式的文件时fstream会导致分段错误?
问题描述
我有以下代码
#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" 创建的空文件。
我在 Ubuntu Linux 18.04 上运行它,使用 g++ 9.2.1 作为编译器。
这会在我的机器上打印“错误”。我不知道为什么文件没有被打开。我还注意到,如果我在文件名后面附加文件格式,我会得到一个更奇怪的分段错误。
这里有什么问题?
解决方案
您应该添加文件扩展名,否则程序无法区分两个同名文件。
ifstream asmfile("asmcode.txt", ifstream::in);
推荐阅读
- vba - 通过 FSO VBA - 文件没有移动,请通过我的代码,我不明白为什么文件没有移动。我正在尝试执行它,但消息框
- flutter - 成员“值”只能在 Flutter GetX 子类的实例成员中使用
- python - 如何使用 Python 找出图像中的所有数字及其位置?
- node.js - 赛普拉斯 - 错误:无法解析“async_hooks”
- javascript - Openpgp 的 PGP 密钥解密错误 - TypeError:无法在 SecretKey.decrypt 处读取 null 的属性“类型”
- download - 如何使用 rsync 从窗口服务器提高下载到 ubuntu 本地 PC 的速度?
- delphi - 如何循环组件 nethttpclient Generics.Collections
- python - 使用 Python 将 PDF 文本提取到文本文件中 - 提取错误
- c# - 带有用户输入的倒数计时器以设置时间c#
- javascript - 频道成员没有得到更新