1

How to read the txt file in the same directory when you run the exe file in the directory set in PATH .

int main(int argc, char* argv[])
{
    cout << "Start" << endl;
    for (int index = 1; index < argc; index++)
        cout << argv[index] << " ";
    cout << endl;

    ifstream readFile("test.txt");
    if (readFile.is_open())
    {
        string str;
        for (; !readFile.eof();)
        {
            readFile >> str;
            cout << str << endl;
        }

        readFile.close();
    }

    else
    {
        cout << "faile open file" << endl;
    }

    cout << "End" << endl;
}

enter image description here

enter image description here

bruno
  • 31,755
  • 7
  • 21
  • 36
Park18
  • 11
  • 1
  • 2
    Other than `for (; !readFile.eof();` ([explanation of why that's a bad idea](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons)), this doesn't look bad. What's the problem you've encountered? – user4581301 Jun 05 '20 at 16:51
  • 2
    When debugging in Visual Studio the default working directory is the folder containing the project file that way both Release and Debug configurations have the same folder as the default. if you click on the file in the file explorer it will default to the folder where the executable is in. You can change this default in the Debugger settings of your project. – drescherjm Jun 05 '20 at 17:05
  • 2
    replace `cout << "faile open file" << endl;` by something like `perror("failed open file");` to see the reason, but probably your program cannot find the file because you are not in the right directory – bruno Jun 05 '20 at 17:05
  • @user4581301 the problem is the file cannot be open (first picture) – bruno Jun 05 '20 at 17:07
  • @drescherjm the second picture indicates the program was started by hand in a terminal no ? – bruno Jun 05 '20 at 17:08
  • I think that was just to show the file location not how it was started because it would have worked if the user clicked on the executable. – drescherjm Jun 05 '20 at 17:08
  • @drescherjm the second picture show the program started with its 2 args then traces. But waiting for OP remarks ;-) – bruno Jun 05 '20 at 17:10
  • Is that powershell? Not exactly sure what the OP is running the program inside there. – drescherjm Jun 05 '20 at 17:11
  • Actually I didn't know that. Can't see the images. – user4581301 Jun 05 '20 at 17:11
  • @user4581301 that is strange, refresh page ( I edited the question to show pictures rather than links 11 mins ago) – bruno Jun 05 '20 at 17:13
  • 1
    One of the more recent answers here may help: https://stackoverflow.com/questions/143174/how-do-i-get-the-directory-that-a-program-is-running-from You will need to get the full path of the currently executable and then extract the directory from that to prepend to your file path, or once you have the directory you want to use change the current working directory to be there and use relative paths. – Retired Ninja Jun 05 '20 at 17:13
  • @bruno I know they're there. I know you edited the question to make them visible. I've just got something blocking them. Have to look into that later. – user4581301 Jun 05 '20 at 17:14
  • I'm making a CLI program. This program receives commands from the user and imports something from the txt file. I execute program with powershell at release directory. No problem.(I move release directory) But after setting release directory at Windows environment variable(PATH), execute this program at powershell can't read file. Why is that? – Park18 Jun 07 '20 at 07:46

0 Answers0