opening a file

when i type in the file name i get the error, but i have created this txt file that i am typing in, can you guys tell me what is wrong?

void Initialize(fstream&Gradesfile,fstream&printerfile)
{
char Filename[50];
cout<<"Enter name of file to open: "<<endl;
cin>>Filename;
Gradesfile.open(Filename,ios::in);
if(Gradesfile.fail())
{
cout<<"Error opening file. "<<endl;
}

}//initialize
If the file path is not 50 characters long, you will either have less than the full path (if the path is longer than 50 characters) or you will have uninitialized garbage values (if it is less than 50 characters). Just use std::string ;)
L B wrote:
Just use std::string ;)


This. char arrays are ill advised. std::string is your friend.

Also, if your file name has a space in it, cin >> Filename will only get up to the next space. So for example if the user types "My File.txt" you'll only get "My".

If you want the full line of text, you need to use getline and not >>.
Topic archived. No new replies allowed.