C++编译器找不到文件
C++ compiler can't find a file
我已经为此工作了相当长一段时间,但似乎无法理解为什么sample_uc_students.txt和sample_smc_grades .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"位于程序运行的同一目录中。它不会在你的Documents文件夹中查找文件。
你的选择:
- 将文件复制到程序运行的目录。
- 使用文件的绝对路径而不是文件名。
相关文章:
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 找不到QtResource文件中的文件
- "assimp/config.h"找不到,但它在文件夹中
- C++系统找不到指定的文件错误
- 在 Ubuntu 18.04 上,我编译的 cpp 可执行文件找不到 VTK7 依赖项
- Pyinstaller创建的可执行文件找不到Meipass目录中存在的DLL
- 如何修复"QtCharts"库文件找不到/无法包含<QtCharts>?
- 为什么生成文件找不到包含目录
- 我的类在 .h 文件中定义,.cpp文件找不到
- 生成文件找不到 .o 文件
- 部署Qt-dll文件-找不到windows插件,尽管平台/qwindows.dll存在,因此存在libEGL和libGL
- 资源中的文件找不到本地文件
- C++包括 C 文件(找不到 typedef)
- C++生成文件找不到文件
- Xcode 6.0.1版gmp.h头文件找不到,尽管gmp.h和gmpxx.h在local/include中
- 生成文件找不到同一目录中的文件
- 我的生成文件找不到我的头目录
- 包含的文件 <> 找不到 " " 中包含的文件
- 突然间,Ogre头文件找不到(DEVELOPER_DIR随着Xcode更新而改变)
- 尝试在Xcode 5中包含头文件.找不到文件