My program isn't opening my file?

I'm sure that i have the correct file name. The output I'm getting is "Cannot open file". What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "stdafx.h"
#include <iostream>
#include "Employee.h"
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
	
	int iD, iD2, iD3 = 0;
	string name, position, department;
	string name2, pos2, dep2;
	string name3, pos3, dep3;
	int i;
	const int size = 3; 
	ifstream file;
	file.open("data.txt");
	if (file.good()) {
		file >> name >> iD >> department >> position >> name2
			>> dep2 >> pos2 >> name3 >> dep3 >> pos3;
	}
	else {
		cout << "Cannot open file" << endl;
	}
	Employee line1;
	line1.Setname(name);
	line1.SetNumber(iD);
	line1.SetDepart(department);
	line1.SetPosition(position);



	system("pause");
    return 0;
}
Use perror to get a better error message: http://www.cplusplus.com/reference/cstdio/perror/
For this to work, the file "data.txt" has to be in the working directory of the execuable that's running. I bet it isn't.

Use the full path to the file.

file.open("C:/Some/directory/data.txt");

Note the direction of the / there.
Topic archived. No new replies allowed.