C++ - File opening

  • C/++/#
  • Thread starter Nusc
  • Start date
  • #1
753
2
Hi. I am required to ask the user to open a file multiple times before exiting the program. How do I do this?

void ReadData(vector <student> & section, int& num_Students,string& filename)
{
ifstream in;
cout << "Please enter file name:" << endl;
cin >> filename;
in.open(filename.c_str());
if(in.fail())
{
// cout << "Input file does not exist. Please enter the correct file name again:" << endl;

// while (i < = 3)
// {



cout << "Input file does not exist. Now exiting program." << endl;
exit(1);
}
else
{
if(!in.eof())
{
in >> num_Students;
cout <<"You have successfully opened "<< filename <<" and there are "<< num_Students <<" students in file."<< endl;
section.resize(num_Students);
for (int i=0; i<section.size();i++)
{
in >> section.at(i).first >> section.at(i).last >> section.at(i).id;
in >> section.at(i).final >> section.at(i).midterm;

section.at(i).lab_scores.resize(5);

for (int j=0;j<5;j++)
in>>section.at(i).lab_scores.at(j);
}

}

}
in.close();

}
 

Answers and Replies

  • #2
753
2
No one knows?
 
  • #3
siddharth
Homework Helper
Gold Member
1,127
0
No one knows?
Put it in a loop?

Code:
char c='y';
while(c!='n')
{

\\ Code here

cout<<"Do you wish to open another file (y/n)?"<<endl;
cin>>c;

}
 
  • #4
753
2
I got it don't worry about it.

Thanks
 

Related Threads on C++ - File opening

  • Last Post
Replies
6
Views
2K
Replies
2
Views
8K
  • Last Post
Replies
2
Views
2K
Replies
8
Views
15K
  • Last Post
Replies
4
Views
3K
Replies
20
Views
33K
  • Last Post
Replies
2
Views
2K
Replies
1
Views
826
Replies
3
Views
3K
Replies
3
Views
2K
Top