Binary Files

Why does this code always out put that file could not be opened ? what's the
wrong in this code ?

[code]

#include "stdafx.h"
#include "iostream"
#include "string"
#include "fstream"

using namespace std;

struct student
{
string name;
int id;
string birthdate;
string adress;
int group;
float gpa;
int phone;
} s[50];

int _tmain(int argc, _TCHAR* argv[])
{
fstream myfile;
myfile.open("student.txt", ios::binary | ios::in | ios::out | ios::ate);
if (myfile.is_open())
{
for (int i = 0; i < 50; i++)
{
cout << "Enter Name : " << endl;
getline(cin, s[i].name);
cout << '/t';
cout << "Enter ID : " << endl;
cin >> s[i].id;
cout << '/t';
cout << "Enter Birth Date : " << endl;
cin >> s[i].birthdate;
cout << '/t';
cout << "Enter Adress: " << endl;
cin >> s[i].adress;
cout << '/t';
cout << "Enter Phone Number: " << endl;
cin >> s[i].group;
cout << '/t';
cout << "Enter GPA: " << endl;
cin >> s[i].gpa;
cout << '/t';
cout << "Enter Group: " << endl;
cin >> s[i].phone;
cout << '/t';
}
myfile.close();
}
else
cout << "The file could not be opened " << endl << endl;
return 0;
}
Chances are the problem is caused by the file not being located at the program's working directory. Even if it's in the same folder with the exe, running from your IDE might result in the program being run in a different directory. So first try using absolute path.
thnx a lot :)
Topic archived. No new replies allowed.