0

I'm writing a program for my laboratory work using C++. But I have a strange problem that I see first time. I can't open my text file using the ifstream::open method.

This simple code doesn't work too:

int main() {
    ifstream file;
    file.open("numbers.txt");
    if (file.is_open())
        cout << "fstream.open(c:\\numbers.txt): it's ok." << endl;
    else
        cout << "fstream.open(c:\\numbers.txt): error: can't open this file." << endl;
    // output: fstream.open(c:\\numbers.txt): error: can't open this file.
    file.close();
    return 0;
}

It's no compiler error but the program can't open the text file. This file exists, but I can't open it from my program. I tried to move it to "C:/" and to the "<visual studio 2022 project folder>/x64/Debug" but it didn't help.

I tried to compile it with a g++ on Linux and it's all right there. In Windows I can open that file with Notepad, but my program can't open it. What can be wrong?

3
  • 8
    You are using a relative path. Maybe your execution path is not where you think it is. Try printing the current working directory and see if it's the folder you think it is. Alternatively try using an absolute path for testing. Commented Nov 12, 2024 at 6:48
  • 1
    Use std::ofstream to create/write a file with a unique, easily recognized name, something like MyUniqueTestFile.txt. Run the program, and, afterwards, use Windows Explorer to search for the file. The directory where you find it, is the directory where you should put numbers.txt. Commented Nov 12, 2024 at 7:51
  • The windows _getcwd function can be used to get the current working directory.
    – Ian Abbott
    Commented Nov 12, 2024 at 12:26

1 Answer 1

0

As someone in the comments said, you are using relative path. Try checking where executable is running by using this command:

std::filesystem::path cwd = std::filesystem::current_path();
std::cout << "Current path is: " << cwd << "\n";

If your file is not present in this directory, try using actual path of file, for example: "D:/documents/numbers.txt". Also, you can use ifstream's constructor to open a file directly without using ifstream::open() method like this:

std::ifstream file("numbers.txt");

File can read by std::getline() function like this:

std::string line;
while(getline(file, line))
{
    std::cout << line << "\n";
}

getline function reads file's line until there is any left and saves it in a string which is outputted every time loop executes. Note that here, std::getline was called without writing namespace's name because of ADL(argument-dependent lookup).

std::fstream is also a good way of manipulating with files, providing read and write functionality in only one class. Reading file with fstream would look like that:

std::fstream file("numbers.txt", std::ios::in);
std::string line;
while(getline(file, line))
{
    std::cout << line << "\n";
}

std::ios::in indicates that stream will be used for reading, and std::ios::out indicates that it will be used for writing into file.

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.