Using fstream with txt files and file pathing

If i put in a filepath it is never able to open it please help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    string line;
    string path;
    cout << "put in the filepath of the txt file.";
    getline(cin, path);
    ifstream myfile(path);

    if (myfile.is_open())
    {
        while (getline(myfile, line))
        {
            cout << line << '\n';
        }
        myfile.close();
    }

    else cout << "Unable to open file";

    return 0;
}

Last edited on
Hello NMI21,

What operating system are you using?

What are you entering for line 11? Give example.

Defining the file as an "ifstream" both the path and file name must spelled correctly.

One thing yo could do is: const std::string PATH{ "" };. Put the path in the double quotes. Then you could do:ifstream myfile(PATH + fileNameVar); or use a string in double quotes.

Andy
Thank you for your reply! Since the txt is on my Desktop i write Desktop/txt.txt but if i put it somewhere else it still doeant work
Hello NMI21,

When using a path it can either be a relative path from what the program considers the current working directory or an absolute path.

I have never tried using Desktop/txt.txt, so I do not know if "Desktop" will find the right directory.

I am thing the absolute path would be more along the lines of: C:/Users/your subdirectory/Desktop/

Since you did not say I am guessing that you are using Windows.

If so try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <iomanip>
#include <string>
#include <Windows.h>

#include <fstream>

using namespace std;  // <--- Best not to use.

int main()
{
    string line;
    string path;

    cout << "put in the filepath and the file name of the txt file.";
    getline(cin, path);

    ifstream myfile(path);

    if (!myfile)
    {
        std::cout << "\n File " << std::quoted(path) << " did not open.\n";
        //std::cout << "\n File \"" << inFileName << "\" did not open.\n";

        DWORD errNum = GetLastError();

        if (errNum == 2)
        {
            std::cerr << "\n     File not found!\n\n";
        }
        else if (errNum == 3)
        {
            std::cerr << "\n     Path not found!\n\n";
        }
        else
        {
            std::cerr << "\n     The error number is: " << errNum << '\n';
        }

        return 1;
    }

    //if (myfile.is_open())  // <--- Unnecessary with the above code.
    //{
        while (getline(myfile, line))
        {
            cout << line << '\n';
        }

        //myfile.close();  // <--- Not required as the dtor will close the file when the function looses scope.
    //}

    //else cout << "Unable to open file";

    return 0;
}


Andy
Topic archived. No new replies allowed.