文件没有打开,即使它存在 [英] File is not opening even though it exists
本文介绍了文件没有打开,即使它存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的程序中,它成功地将数据写入 student.txt
文件,但是当我打开它并逐行打印文件时,它始终显示文件不找到了。
In the following program, it writes data successfully to student.txt
file, but when I open it and print line by line of the file, it always shows file not found.
有人可以帮我吗?
ifstream Myfile;
Myfile.open("student.txt");
if(!Myfile){
cout<<"Sorry file can't be opened" <<endl;
exit(1);
}
else
{
// Use loop and read the names and ids from the file and display them
string line;
while (getline(Myfile, line)){
cout<<line<<endl;
}
// Close the file
Myfile.close();
推荐答案
检查文件放置位置。根据IDE程序的不同,可以在项目的根文件夹或已编译的二进制文件所在的文件夹中查找文件。
Check where your file is placed. Depending on IDE program can look for file in the root folder of the project or in the folder where compiled binaries are situated.
或
只需指定文件的完整路径。
Simply specify the full path to the file.
这篇关于文件没有打开,即使它存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文