读取文件 - 无法打开
我正在尝试用 C++ 打开一个文件,但这似乎给我带来了一些麻烦,这是迄今为止处理打开文件的代码:
void CreateHistogram(string str_file, vector<HistogramWord> &result) {
string line;
long location;
HistogramWord newWord;
const char * filename = str_file.c_str();
//ifstream myfile (str_file.c_str());
ifstream myfile (filename);
//myfile.open(filename);
if (myfile.is_open()) {
while (myfile.good()) {
getline(myfile, line);
line = clarifyWord(line);
好的,只是为了一点解释,HistogramWord 是一个定义的结构在标题中以及我在在线文档中读到的内容中,文件名必须是 const char * 类型,所以这就是我所做的。将 str_file
转换为 const char *
。
现在,我尝试了一些不同的事情,这就是为什么一些代码被注释掉的原因。当到达 if (myfile.is_open())
行时,它的计算结果始终为 false。似乎有人知道为什么吗?
谢谢, 布兰登
I am trying to open a file in C++ but it seems to be giving me a bit of hassle, here is the code that deals with opening the file so far:
void CreateHistogram(string str_file, vector<HistogramWord> &result) {
string line;
long location;
HistogramWord newWord;
const char * filename = str_file.c_str();
//ifstream myfile (str_file.c_str());
ifstream myfile (filename);
//myfile.open(filename);
if (myfile.is_open()) {
while (myfile.good()) {
getline(myfile, line);
line = clarifyWord(line);
Okay, just for a bit of explanation, HistogramWord is a struct that is defined in the header and from what I have read in the online documentation, the filename has to be of type const char *
, so that is what I have done. Converted str_file
to be a const char *
.
Now, I have tried a few different things which is why some of the code is commented out. When it gets to the line if (myfile.is_open())
, it always evaluates to false. Anyone seem to know why?
Thanks,
Brandon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
OK IO 101
如果您没有提供完整的文件路径,而仅提供文件名,则当前工作目录将附加到文件名中。
因此,如果您的 .exe 位于 C:\temp 中,并且您从此目录调用您的程序,并且您的文件名为 test.txt,则在这种情况下完整的文件名将为 C:\temp\test.txt
这仅在 . exe 和 test.txt 都位于 C:\temp 下。
在所有其他情况下,它将失败。您可以使用 win API 或 linux 等效工具创建绝对路径 - 我不知道您所在的平台。
现在为了读取成功打开的文件就足够了:
编辑:谢谢@Shahbaz
OK IO 101
If you don't give the complete filepath but only the filename then the current working directory will be appended to the filename.
So if your .exe is in C:\temp and you call your program from this directory and your filename is test.txt then the complete filename in this case will be C:\temp\test.txt
This will only work if the .exe and the test.txt are both under C:\temp.
In all other cases it will fail. You could create the absolute path by using win API or the linux equivalent - I don't know what platform you are on.
Now in order to read a succsfully opened file this will suffice :
edit : Thanks @ Shahbaz
我最好的猜测是 Windows 正在“隐藏已知文件类型的扩展名”,因此文件名实际上与您在 Windows 中输入的名称不同。例如,如果它是一个 .txt 文件,并且您将其命名为 test.txt,则实际名称将是 test.txt.txt,这是 Windows 所做的一件非常愚蠢的事情。
要更改此设置,请转至我的电脑 ->工具->文件夹选项->并取消选中“隐藏已知文件类型的扩展名”框。这是针对 XP 的。如果你有另一个窗口,它应该或多或少是相同的路径。如果您没有看到工具栏,请尝试按 ALT+t(工具)或 ALT+f(文件)来显示工具栏。
这个问题给我们很多人带来了大学第一学期的困扰。
My best guess is that Windows is "hiding extensions for known file types" so the name of the file is actually different than what you have put in windows. For example if it's a .txt file, and you name it test.txt, the actual name would be test.txt.txt which is quite a stupid thing windows does.
To change this, go to My Computer -> Toold -> Folder Options -> And uncheck the box that says "Hide extensions for Known File Types". This is for XP. If you have another windows it should be more or less the same path. If you don't see the toolbar, try ALT+t (tools) or ALT+f (file) to make it appear.
This problem give quite many of us a trouble in the first semester of college.
对我来说解决这个问题的是在我的文件路径中使用正斜杠而不是双反斜杠。
例如
代替
What fixed it for me was using forward slashes instead of double backslashes in my filepath.
e.g.
instead of