ifstream infile


I am under windows 7 and I try to read a txt file. here is my code

char data[100];

// open a file in write mode.
ofstream outfile;

cout << "Writing to the file" << endl;
cout << "Enter your name: ";
cin.getline(data, 100);

// write inputted data into the file.
outfile << data << endl;

cout << "Enter your age: ";
cin >> data;

// again write inputted data into the file.
outfile << data << endl;

// close the opened file.

// open a file in read mode.
ifstream infile;

cout << "Reading from the file" << endl;
infile >> data;

// write the data at the screen.
cout << data << endl;

// again read the data from the file and display it.
infile >> data;
cout << data << endl;

// close the opened file.

The file "test.txt" is in the same directory as the application, but I can't read it. If I try to read "afile.dat", everything is perfect.. is there any limitation with windows 7? I think I can read what I write, but I dont have the permission to read another file. Is this a restriction of win 7 ? How can I fix this ?
The current directory isn't always what you think when running in an IDE.

Use a system call GetCurrentDirectory() or _getcwd() to confirm the programs working directory.
Topic archived. No new replies allowed.