I can't read a txt file. I've tried with different pieces of code which should work and with different text files. The problem isn't that I got the wrong name (the file doesn't lack a txt or have an extra txt). Also, adding a second backwards slash \ or replacing it with forwards slash / doesn't fix it.
Here is the code:
// ConsoleApplication74.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <fstream> #include <string> using namespace std; #include <iostream> #include <iomanip> #include <fstream> using namespace std; int main() { int sum = 0; int x; ifstream inFile; inFile.open("C:\Users\chaim\SkyDrive\Documents\string\text1.txt"); if (!inFile) { cout << "Unable to open file"; exit(1); // terminate with error } while (inFile >> x) { sum = sum + x; } inFile.close(); cout << "Sum = " << sum << endl; return 0; } Thanks!
/in your filenames, because\tis a tab character.stdafxand turn off precompiled headers for small programs. The precompiled header feature does not give you any advantages unless you have a huge program with lots of include files or you are writing a Windows program and accessing their API. (The windows.h file is huge and could benefit from being precompiled.)