C++: Open File Multiple Times Before Exiting

  • C/C++
  • Thread starter Nusc
  • Start date
  • Tags
    C++ File
In summary, the user is asking for advice on how to prompt for a file multiple times before exiting the program. They are considering using a loop and have received help from someone else.
  • #1
Nusc
760
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();

}
 
Technology news on Phys.org
  • #2
No one knows?
 
  • #3
Nusc said:
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
I got it don't worry about it.

Thanks
 

Related to C++: Open File Multiple Times Before Exiting

1. Can we open the same file multiple times in a C++ program?

Yes, it is possible to open the same file multiple times in a C++ program. Each time you open the file, a new file pointer is created which maintains its own position in the file. This allows you to read or write to the file from different parts of the program.

2. How many times can we open a file in a C++ program?

There is no specific limit on the number of times you can open a file in a C++ program. However, it is recommended to avoid opening a large number of files simultaneously as it can lead to performance issues. It is also important to close the file after use to free up system resources.

3. Can we open a file for both reading and writing in a C++ program?

Yes, it is possible to open a file for both reading and writing in a C++ program. This is done by using the "r+" or "w+" mode in the fopen() function. However, it is important to note that using the "w+" mode will truncate the file to zero length if it already exists.

4. Do we need to close a file before opening it again in a C++ program?

Yes, it is important to close a file before opening it again in a C++ program. This ensures that any changes made to the file are saved and the file is released from memory. If a file is not closed before opening it again, it can lead to errors and data corruption.

5. How do we properly close a file in a C++ program?

To properly close a file in a C++ program, you can use the fclose() function. This function takes in the file pointer as a parameter and closes the file. It is good practice to check the return value of the fclose() function to ensure that the file was closed successfully. It is also important to close all open files before exiting the program.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
8K
Replies
10
Views
977
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
2
Replies
66
Views
4K
Back
Top