簡體   English   中英

C ++編譯器找不到文件

[英]C++ compiler can't find a file

我已經為此工作了一段時間,但似乎無法理解為什么未讀取文件sample_uc_students.txt和sample_smc_grads.txt。 它們是我放入“我的文檔”文件夾中的預制文檔,但沒有打開。

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;

}

關於如何打開這些文件的任何建議,或者我應該嘗試通過一條路徑打開它們?

使用時:

Student* uc = readStudentsFromFile("sample_uc_students.txt", UC_SIZE);

該程序希望文件“ sample_uc_students.txt”位於運行該程序的同一目錄中。 不會在“文檔”文件夾中查找文件。

您的選擇:

  1. 將文件復制到運行程序的目錄中。
  2. 使用文件的絕對路徑,而不只是使用文件名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM