I would like to read data from the text file stored in the following
path C:\folder1\folder2\example.txt
but my code is not working. I am receiving the message "Unable to open file" and yet the text file exists.Any correction will be highly appreciated.
#include <windows.h>
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string filename, line;
SetCurrentDirectoryA( "C:\\folder1\\folder2\\" );
ifstream inFile;
inFile.open("example.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
while (inFile >> line) {
cout << line << endl ;
}
inFile.close();
return 0;
}
#include <iostream>
#include <fstream>
int main()
{
char buf[256];
std::ifstream inFile("C:\\folder1\\folder2\\example.txt");
if (!inFile.is_open()) {
std::cout << "Unable to open file";
exit(1); // terminate with error
}
while (inFile >> buf) {
std::cout << buf << std::endl;
}
inFile.close();
return 0;
}
I've just tried it and it works perfect