0

I'm confused about how to open/access a file using Notepad++ and the cmd with MinGW compiler. I understand the file needs to be in the same scope however, I'm not sure where. I have tried placing the .txt file in my Documents folder which also holds the main.cpp file, and I have tried placing it in the bin folder of the MinGw folder. When I run the code, I get the error message. Is something wrong with my code or is the .txt file in the wrong location?

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int main() {

    ifstream inFS;
    ofstream outFS;
    string fileName;
    double fileNum;

    //fileName = "input_prac.txt";

    //cout << "Enter file name: " << endl;
    //cin >> fileName;

    cout << "Opening file..." << endl;
    inFS.open("input_prac.txt");      // Open file

    if (!inFS.is_open())
    {
        cout << "Could not open file" << endl;
        exit(1);
    }

    // Read file 
    while(!inFS.eof())
    {

        inFS >> fileNum;
        cout << fileNum << endl;
    }

    inFS.close();                // close file


    return 0;
}
Manny
  • 43
  • 6
  • It needs to be in your current working directory. The locations of the source file and the executable don't matter. – molbdnilo Nov 20 '19 at 08:13
  • To fix the next problem you will encounter, read about why [`!inFS.eof()` is wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – molbdnilo Nov 20 '19 at 08:14
  • @molbdnilo Can you elaborate on the current directory? It looks like this on cmd for me: `C:\MinGW\bin>`. So if my .txt file is in the `bin` folder, shouldn't it work? – Manny Nov 20 '19 at 08:27
  • Yes, it should work, although that is a very odd place to be putting your own files in. – molbdnilo Nov 20 '19 at 08:44

0 Answers0