0

I am trying to open a text file, and the code below is my attempt:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    std::ifstream file;
    file.open("InputFile.txt");

    std::string fileOutput;

    if (file.is_open())
    {
        while (!file.eof())
        {
            file >> fileOutput;
            std::cout << fileOutput << std::endl;

        }
    }
    else
    {
        std::cout << "File failed to open" << std::endl;
    }
    file.close();

    return 0;
    }

The text file is located on my desktop, and it only contain two integers.

enter image description here

Whenever I run the code above, it will show me the "file failed to open" message. I am completely new to c++, so I really don’t have any idea why my code is not working. So any comments would be appreciated.

Beans
  • 139
  • 5
  • 19
  • 1
    You need to give the program more than the filename - where on the hard disk is the desktop located? Alternatively put the text file in the smae folder as the program and then it will be able to find it. The OS always looks in the current folder and then in the "path" - but it never looks everywhere. – Jerry Jeremiah Jan 15 '20 at 02:08
  • 4
    Not the cause of your actual problem, but worth pointing out: [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/q/5431941/364696). The correct loop would be something like `while (file >> fileOutput) { std::cout << fileOutput << std::endl; }` – ShadowRanger Jan 15 '20 at 02:11

3 Answers3

4

The text file is located on my desktop

So where is your C++ source file, is it located in my desktop as well?

Note this code file.open("InputFile.txt"); tries to open the InputFile.txt in the current folder, that means it only works if both C++ source file and your text file are in the same folder. That seems to be your problem.

artm
  • 17,291
  • 6
  • 38
  • 54
2

Unless the file you are opening and your executable are in the same directory, the same message will be printed since it will search for the file in the current working directory. You can specify the absolute path to the file on your desktop using %USERPROFILE%\\Desktop\\InputFile.txt or any other environmental variable that maps the absolute path of a disk, from which your file can be found.

  • 1
    It will look in the *working* directory, *not* the directory containing the executable (these might be the same by coincidence, but frequently they differ). For example, if you're in `C:\Users\SomeUser` and run `C:\Program Files\MyProgram\myprogram.exe` (by absolute or relative path, or by running `myprogram.exe` unqualified and finding it because `C:\Program Files\MyProgram` is in your `PATH`), the working directory remains `C:\Users\SomeUser`, it's not the location where `myprogram.exe` is found. – ShadowRanger Jan 15 '20 at 02:18
2

Like @ShadowRanger's this comment, the existing answers are both inaccurate. The argument for file.open() needs to either 1. reflect the relative location of the text file in relation to the current working directory (where you are calling the executable from), or 2. give the absolute location of the text file on the disc.

I suggest the following solution:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char** argv) {
    if (argc != 2) {
        std::cout << "incorrect number of inputs" << "\n";
        std::cout << "correct usage: this_executable.exe file_location" << "\n";
        return -1;
    }

    std::ifstream file;
    file.open(argv[1]);

    std::string fileOutput;

    if (file.is_open())
    {
        while (file >> fileOutput)
        {
            std::cout << fileOutput << std::endl;

        }
    }
    else
    {
        std::cout << "File "<< argv[1] <<" failed to open" << std::endl;
    }
    file.close();

    return 0;
}

This solution takes the file's address info out of the code. With this solution, when you call your executable, the file's address(directory path + file name) is given to the executable at run-time rather than compile-time. Now, you'd run the executable like:

C:\path_to_your_exe>my_executable.exe C:\path_of_your_txt_file\InputFile.txt

The benefits of this approach are:

  1. You can change the file's name / path without having to recompile the code;
  2. On the commandline, it is easier to check that the target file's address is correct by tab completion

Also note:

If you are wondering what argv[1] means, see this guide for more information on commandline arguments for C++. You also want to make sure to catch situations when the user did not specify an input (meaning argv[1] is invalid, thus the argc != 2)

B.Gao
  • 146
  • 1
  • 8
  • 1
    A minor amendment to the Geeks For Geeks reference: `argv[0]` doesn't always contain the name of the program. It usually contains the command executed and this may or may not be the name of the program, but it is not formally specified exactly what will be in argv[0]. Some systems, usually embedded devices, leave it blank. – user4581301 Jan 15 '20 at 03:06
  • @user4581301 thanks for pointing these out, I updated the code accordingly. – B.Gao Jan 15 '20 at 03:11