I have a queation about reading an text file in C++.
Supoose i have a sample.txt file like this:
2
8 5
1 1 1 2 3 4 5 6
4 7
1 2 3 4
the first line of the file means the number of instances(here 2 instances). the second line and third line is the information about the first instence. the third line and fourth line represents the second instances. Below is my main function to read this text file. but when i run it, it just does not work. ie the reading process cannot going on. Anyone can help me out.
int main()
{
int number;
string filePath;
int numberOfInstances;
int Max;
int Rings;
cout << "please input your file path: " ; //ie, D:\\sample.txt
cin >> filePath;
ifstream fin;
fin.open(filePath.c_str());
if(fin.fail()) {
cout << "sorry, fail to open" << endl;
exit(1);
}
fin >> numberOfInstances;
vector<int> vect1;
for(int i = 0; i < numberOfInstances; i++) {
fin >> Rings;
fin >> Max;
for(i = 0; i < Rings; i++) {
fin >> number;
vect1.push_back(number);
}
//
//code to process Vect1, eg, sort it
//
for(i = 0; i < Rings; i++) {
vect1.pop_back();
}
}
fin.close();
return 0;
}