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.
#include <iostream>
#include <iomanip>
#include <string>
#include <Windows.h>
#include <fstream>
usingnamespace 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";
}
elseif (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;
}