I
I
Ilya2014-12-14 23:36:47
C++ / C#
Ilya, 2014-12-14 23:36:47

Why can't c++ programs see the file when run outside of xcode?

Good evening.
I have a program that needs to read values ​​from a .txt file.
Checked in the program:

fromFile.open("words.txt");
    if (!fromFile.is_open()) 
    {
        cout << "Could not open the file "<<endl;
        cout << "Program terminating.\n";
        exit(EXIT_FAILURE);
    }

When I run the program separately from xcode (I just open the compiled console file), I see that the test condition is met and, accordingly, the program does not see the file.
When run from xcode (cmd + R) everything is fine, the program works.
I did not change the location of the executable file, it lies in the same place where xcode created it.
words.txt is in the same folder as the executable file.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Armenian Radio, 2014-12-14
@Quirel

The program tries to look for a file in the current folder . In order for the program to look for a file in the same folder where its executable file is located, you need to take argv[0], strip the program name from it, put the file name in this place and use the resulting value.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question