0

I'm currently using Clion for a school project that involves creating an assembler for MIPS. I've been trying to test it, but my ifstream.open() is failing to find the text file that is in the same folder as the program. I am using the same exact code to open my file as I did in previous projects, literally copy pasted, and for whatever reason it isn't working. I don't need help with any other issues in the program, I can fix those bugs on my own, but this file business, which I'm assuming has something to do with clion being a pain, makes me wanna pull my hair out.

I have tried declaring my istream as istream and ifstream, as well as inputting the file name, as well as the whole filepath from C: all the way to the file.

#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

should open the file and continue on in the program, but gets stuck in the loop because file is failing to be opened

  • How do you know the file isn't opening? – David G Oct 11 '19 at 00:46
  • the while(!fin) loop continues to go around, which should check that the file has been opened – Clion is the bane of my life Oct 11 '19 at 01:04
  • 1
    (1) check the file has read permissions; (2) check that the _working directory_ of your program is where the file is located, if not using absolute path; (3) try specifying the full absolute path to the file; (4) ensure no other process has the file open in such a way that it prevents read access to other processes. – paddy Oct 11 '19 at 01:06
  • Additionally, since it sounds like you're using windows, it's important that if your filename or path contains _any_ Unicode characters outside of the standard ASCII character set, you _must_ use a `std::wstring` for the file name. File system access in Windows does not support UTF-8 encoding. – paddy Oct 11 '19 at 01:10
  • 2
    Why are you opening the same file twice? Seems kind of weird. – user4581301 Oct 11 '19 at 01:24
  • Why is arraySize 0? – drescherjm Oct 11 '19 at 02:03

1 Answers1

0

There are ways to see why ifstream::open fails. For a longer answer see How to get error message when ifstream open fails.

Here is what you can do:

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

This should give you an error message in the console.

Pibben
  • 1,876
  • 14
  • 28