C++ 程序正在努力打开文件

C++ 程序正在努力打开文件,c++,iostream,clion,C++,Iostream,Clion,我目前正在为一个学校项目使用Clion,该项目涉及为MIPS创建汇编程序。我一直在尝试测试它,但是我的ifstream.open()找不到与程序位于同一文件夹中的文本文件。我使用与以前项目中相同的代码打开我的文件,从字面上看是复制粘贴的,无论出于何种原因,它都不起作用。我在程序中不需要任何其他问题的帮助,我可以自己修复这些bug,但是这个文件业务,我假设它与clion的痛苦有关,让我想把我的头发拔出来 我尝试将我的istream声明为istream和ifstream,并输入文件名,以及从C:到文

我目前正在为一个学校项目使用Clion,该项目涉及为MIPS创建汇编程序。我一直在尝试测试它,但是我的ifstream.open()找不到与程序位于同一文件夹中的文本文件。我使用与以前项目中相同的代码打开我的文件,从字面上看是复制粘贴的,无论出于何种原因,它都不起作用。我在程序中不需要任何其他问题的帮助,我可以自己修复这些bug,但是这个文件业务,我假设它与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
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//空数组(int&size、int*intArray、string*stringArray);
布尔寄存器检查(字符串和二进制,字符串保持);
int main(){
字符串文件名;
字符串二进制输出;
字符串输出名;
字符串tempString;
字符串保持字符串;
int-holdInt;
int-binaryHold[16];
int-arraySize=0;
字符串标签[arraySize]={};
int labelAddress[arraySize]={};
字符串stringTemp[arraySize];
int tempInt[arraySize];
int二进制计数器;
int指令计数器=0;
流鳍;
ifstreamfint;
cout>文件名;
cout文件名;
打开(文件名);
fint.open(文件名);
}     
``//在此之后无法进入程序的其余部分

应该打开文件并在程序中继续,但由于无法打开文件而陷入循环中

有多种方法可以查看ifstream::open失败的原因。有关详细答案,请参阅

以下是您可以做的:

fin.open(fileName);
if(!fin) {
  std::cerr << "Error: " << strerror(errno) << '\n';
}
fin.open(文件名);
如果(!fin){

std::cerr如何知道文件未打开?while(!fin)循环将继续循环,这将检查文件是否已打开(1)检查文件是否具有读取权限;(2)检查程序的工作目录是否位于文件所在的位置,如果不使用绝对路径;(3)尝试指定文件的完整绝对路径;(4)确保没有其他进程以阻止对其他进程进行读取访问的方式打开该文件。此外,由于听起来像是在使用windows,因此如果文件名或路径包含标准ASCII字符集之外的任何Unicode字符,则必须使用
std::wstring
作为文件名。fileWindows中的系统访问不支持UTF-8编码。为什么要打开同一个文件两次?看起来有点奇怪。