fstream - unable to open file stream c++ -
i working xcode , having trouble opening file stream assign variables text file. speculate placing txt file in same directory project allow me open stream without including entire dir. have been messing little while no avail can work properly. believe able read data @ 1 point, think string printed in unicode (not sure). simple program.. think work. think problem has directory example in , how xcode works project files. put example file in project folder , hoped work.
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { string name; ifstream infile; infile.open("example.txt"); if(infile.is_open()) { infile >> name; } else cout << "unable open file"; cout << name; return 0; }
first of all, remember, working directory not same directory program's binary resides.
change:
infile.open("example.txt"); to:
infile.open("/full/path/to/program/directory/example.txt"); where /full/path/to/program/directory/ location of folder, program (and example.txt file) placed. should fix issue.
by way, may want read this question, addresses similar problem.
also, read getcwd() function.
Comments
Post a Comment