0

I'm trying to make the c++ program that read the alphabet from "abc.txt" and split by space, and then save in array (arrFirst).

My environment is Visual Studio 2019.

(I'm not good at English. I'm sorry if my writing is hard to read)

This is my code, and I have "abc.txt" in same directory with this .cpp file.

This code can't open the "abc.txt" file.

I can't understand why this code can't open the file.

Could you tell me why this code can't open the txt file??

Thank you.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    char arrFirst[30];

    int position = 0;
    int arrFirstSize = sizeof(arrFirst) / sizeof(arrFirst[0]);

    ifstream fin;
    fin.open("abc.txt");
    if (fin.is_open())
    {
        cout << "File Opened Successfully! " << endl;
        while (!fin.eof() && position < arrFirstSize);
        {
            fin.get(arrFirst[position]);
            position++;
        }
        arrFirst[position - 1] = '\0';

        cout << "Displaying Array... " << endl << endl;
        for (int i = 0; arrFirst[i] != '\0'; i++)
        {
            cout << arrFirst[i];
        }
    }
    else
    {
        cout << "File could not be opened." << endl;
    }

    return 0;
}
Jiu Lee
  • 13
  • 1
  • 6
  • What have you done to identify *why* the file won't open? – Scott Hunter Nov 05 '20 at 18:16
  • The `.exe` file is most probably _not_ in the same directory as the `.cpp` (and `.txt`) file – Ted Lyngmo Nov 05 '20 at 18:17
  • If this is Visual Studio the default folder is the one that has the project file. Many other IDEs the default folder is the one containing the executable. A second problem on windows is the OS file explorer by default hides extensions for known types which may let you accidently name the file abc.txt.txt and because of the hiding it may not be obvious. If you are in this situation my advice is to turn off this hiding of the file extension. – drescherjm Nov 05 '20 at 18:18
  • @ScottHunter I debugged it by using F5 and F11. It goes straight from "if(fin.is_open)" to "else" – Jiu Lee Nov 05 '20 at 18:19
  • @JiuLee Is the `.exe` file in the same directory as the `.txt` file? – Ted Lyngmo Nov 05 '20 at 18:23
  • @TedLyngmo Thank you for replying but my .exe file is on same directory – Jiu Lee Nov 05 '20 at 18:23
  • If this is Visual Studio Community / Enterprise / Pro that is the wrong folder. You want to put the text file in the same folder as your project. The default debugging setting sets the working directory to `$(ProjectDir)` which is a Visual Studio variable that points to the folder that contains the project. – drescherjm Nov 05 '20 at 18:24
  • 1
    Unrelated: You loop `while (!fin.eof() ...` is going to cause problems. [Why is `iostream::eof` inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Ted Lyngmo Nov 05 '20 at 18:25
  • @TedLyngmo Do I have to move my txt file at same directory with .exe when I using ifstream?? I didn't know that!! I located it with .cpp!! I'm sorry I'll check it now – Jiu Lee Nov 05 '20 at 18:25
  • ***No, but VS will start the .exe with the current directory to the same directory as the .exe is placed in.*** It won't do that by default. You can have it do that by changing the settings. – drescherjm Nov 05 '20 at 18:27
  • The documentation for this setting is here: [https://learn.microsoft.com/en-us/visualstudio/debugger/project-settings-for-a-cpp-debug-configuration?view=vs-2019](https://learn.microsoft.com/en-us/visualstudio/debugger/project-settings-for-a-cpp-debug-configuration?view=vs-2019) ***Specifies the working directory of the program being debugged, relative to the project directory where your EXE is located. If you leave this blank, the working directory is the project directory. For remote debugging, the project directory is on the remote server.*** – drescherjm Nov 05 '20 at 18:31
  • @drescherjm I'm sure with that!! To check that, I tried to remove .txt on file name, and window gave me warning message. – Jiu Lee Nov 05 '20 at 18:33
  • Read this: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getcwd-wgetcwd?view=msvc-160 – Martin York Nov 05 '20 at 18:35
  • 1
    If you print out the current working directory you will know if your application is running in the expected directory. If it is not then you either need to change directory or move the file to the correct directory. – Martin York Nov 05 '20 at 18:47
  • I do realize this part of the documentation ***relative to the project directory where your EXE is located*** seems to contradict what I said but it's somewhat incorrect. The executable is usually in $(ProjectDir)/$(Configuration) where $(Configuration) is Debug, Release ... The IDE itself has the following help when you click on the Project->Debugging->Working Directory setting ***Working Directory The application's working directory. By default the directory containing the project file.*** – drescherjm Nov 05 '20 at 19:51

0 Answers0