c++ - 程序难以打开文件
问题描述
我目前正在将 Clion 用于涉及为 MIPS 创建汇编程序的学校项目。我一直在尝试对其进行测试,但是我的 ifstream.open() 无法找到与程序位于同一文件夹中的文本文件。我正在使用与以前项目中相同的代码来打开我的文件,从字面上复制粘贴,并且无论出于何种原因它都无法正常工作。我不需要帮助解决程序中的任何其他问题,我可以自己修复这些错误,但是我认为这个文件业务与 clion 的痛苦有关,让我想拔掉我的头发.
我尝试将我的 istream 声明为 istream 和 ifstream,以及输入文件名,以及从 C: 一直到文件的整个文件路径。
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <fstream>
#include <array>
using namespace std;
//void GrowArrays(int &size, int *intArray, string *stringArray);
bool RegisterCheck(string &binary, string hold);
int main() {
string fileName;
string binaryOut;
string outFileName;
string tempString;
string holdString;
int holdInt;
int binaryHold[16];
int arraySize = 0;
string labels[arraySize] = {};
int labelAddress[arraySize] = {};
string stringTemp[arraySize];
int tempInt[arraySize];
int binaryCounter;
int instructionCounter = 0;
ifstream fin;
ifstream fint;
cout << "Please input the name of the file you wish to open";
cin >> fileName;
cout << "please input the name of the file you wish to write to.";
fin.clear();
fint.clear();
fin.open(fileName);
fint.open(fileName);
while(!fin) {
cout << "File not opened, try again.";
cin >> fileName;
fin.open(fileName);
fint.open(fileName);
}
` `//not making it to rest of program after this
应该打开文件并在程序中继续,但由于文件无法打开而陷入循环
解决方案
有很多方法可以查看 ifstream::open 失败的原因。有关更长的答案,请参阅ifstream open failed 时如何获取错误消息。
这是您可以执行的操作:
fin.open(fileName);
if(!fin) {
std::cerr << "Error: " << strerror(errno) << '\n';
}
这应该会在控制台中给您一条错误消息。
推荐阅读
- roku - 有没有办法将键盘与标签相关联?
- python - 将请求响应添加到 Pandas Dataframe 中的单独行
- python - Eror exucting script with python 3.5 but working fine with 3.8
- python - “BertTokenizerFast”对象没有属性“get_vocab_size”
- python - “球”对象没有属性“身体”
- python - 具有复合主键的 Django 模型未更新
- php - 我想检查两个表是否所有内容都相似
- azure - 如何在 Azure ML 工作室设计器管道中使用模块?
- nuxt.js - 我如何从生产中阻止页面但在 nuxt 的开发中显示?
- sql - 无法识别日期“% {syslog_timestamp}”