簡體   English   中英

程序難以打開文件

[英]Program Struggling to Open File

我目前正在將 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';
}

這應該會在控制台中給您一條錯誤消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM