LaVOZs

The World’s Largest Online Community for Developers

'; c++ - How to open a text file - LavOzs.Com

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.

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)

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.

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.

Related
How do you set, clear, and toggle a single bit?
How do I iterate over the words of a string?
How can I profile C++ code running on Linux?
Why can templates only be implemented in the header file?
C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?
Namespaces and the Pre-Processor
Why use namespace if iostream is imported
ofstream is open, but does not write