【发布时间】:2019-10-11 00:44:56
【问题描述】:
我目前正在将 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
应该打开文件并在程序中继续,但由于文件无法打开而卡在循环中
【问题讨论】:
-
你怎么知道文件没有打开?
-
while(!fin) 循环继续运行,它应该检查文件是否已打开
-
(1)检查文件是否有读权限; (2) 检查你程序的工作目录是否是文件所在的位置,如果不使用绝对路径; (3) 尝试指定文件的完整绝对路径; (4) 确保没有其他进程以阻止对其他进程的读取访问的方式打开文件。
-
此外,因为听起来您使用的是 Windows,所以如果您的文件名或路径包含标准 ASCII 字符集之外的 任何 个 Unicode 字符,那么您的 必须使用
std::wstring作为文件名。 Windows 中的文件系统访问不支持 UTF-8 编码。 -
为什么要打开同一个文件两次?看起来有点奇怪。