Reputation: 5
I've been working for quite a while on this, but can't seem to get why the files sample_uc_students.txt and sample_smc_grads.txt are not being read. They're premade documents I put into my documents folder but they're not opening.
Student* readStudentsFromFile(string filename, int num) {
ifstream studentsStream;
studentsStream.open(filename.c_str());
if (!studentsStream.is_open()) {
cerr << "Couldn't open the file " << filename << endl;
return NULL;
}
// create a new array of students with size 'num'
Student* students = new Student[num];
string name, school, sid;
int id;
// read student records from file
for (int i = 0; i < num; i++) {
getline(studentsStream, name, ',');
getline(studentsStream, sid, ',');
getline(studentsStream, school);
istringstream idConv(sid);
idConv >> id;
// create a student object from the record and store it in the array
students[i] = Student(id, name, school);
}
studentsStream.close();
return students;
}
int main() {
const int SIZE = 10;
const int SMC_SIZE = 5;
const int SMC_UC_GRADS_SIZE = 2;
Student* uc = readStudentsFromFile("sample_uc_students.txt", UC_SIZE);
Student* smc = readStudentsFromFile("sample_smc_grads.txt", SMC_SIZE);
\Time it will take
time_t start, end;
time(&start);
Student* common1 = findCommonStudents1(uc, UC_SIZE, smc, SMC_SIZE,
SMC_UC_GRADS_SIZE);
time(&end);
cout << "Using linear search it took " << difftime(end, start) << " seconds."
<< endl;
/*
* library sort function to sort an array: sort(arr, arr+size)
* Note that values must be comparable with the < operator
*/
sort(common1, common1 + SMC_UC_GRADS_SIZE);
writeStudentsToFile(common1, SMC_UC_GRADS_SIZE, "smc_grads_at_uc_1.txt");
time(&start);
Student* common2 = findCommonStudents2(uc, UC_SIZE, smc, SMC_SIZE,
SMC_UC_GRADS_SIZE);
time(&end);
cout << "Using binary search it took " << difftime(end, start)
<< " seconds." << endl;
sort(common2, common2 + SMC_UC_GRADS_SIZE);
writeStudentsToFile(common2, SMC_UC_GRADS_SIZE, "smc_grads_at_uc_2.txt");
delete[] smc;
delete[] uc;
delete[] common1;
delete[] common2;
return 0;
}
Any suggestions on how to get these to open or perhaps I should try to open them through a path?
Upvotes: 0
Views: 7947
Reputation: 206747
When you use:
Student* uc = readStudentsFromFile("sample_uc_students.txt", UC_SIZE);
the program expects the file "sample_uc_students.txt" to be in the same directory where the program is run. It's not going to look for the file in your Documents folder.
Your options:
Upvotes: 2