读取文件 - 无法打开

发布于 2024-12-05 05:30:09 字数 785 浏览 0 评论 0原文

我正在尝试用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄离 2024-12-12 05:30:09

OK IO 101

如果您没有提供完整的文件路径,而仅提供文件名,则当前工作目录将附加到文件名中。

因此,如果您的 .exe 位于 C:\temp 中,并且您从此目录调用您的程序,并且您的文件名为 test.txt,则在这种情况下完整的文件名将为 C:\temp\test.txt

这仅在 . exe 和 test.txt 都位于 C:\temp 下。

在所有其他情况下,它将失败。您可以使用 win API 或 linux 等效工具创建绝对路径 - 我不知道您所在的平台。

现在为了读取成功打开的文件就足够了:

void CreateHistogram(string str_file, vector<HistogramWord> &result) {
string line;
long location;
HistogramWord newWord;

ifstream myfile (str_file.c_str());
if (myfile.is_open()) {
    while (getline(myfile, line)) {
        line = clarifyWord(line);
}
else{
   //throw exception, print error message etc
   throw std::exception(std::string("Couldn't open file : " + str_file).c_str());
}
}

编辑:谢谢@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 :

void CreateHistogram(string str_file, vector<HistogramWord> &result) {
string line;
long location;
HistogramWord newWord;

ifstream myfile (str_file.c_str());
if (myfile.is_open()) {
    while (getline(myfile, line)) {
        line = clarifyWord(line);
}
else{
   //throw exception, print error message etc
   throw std::exception(std::string("Couldn't open file : " + str_file).c_str());
}
}

edit : Thanks @ Shahbaz

风吹雪碎 2024-12-12 05:30:09

我最好的猜测是 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.

北方的巷 2024-12-12 05:30:09

对我来说解决这个问题的是在我的文件路径中使用正斜杠而不是双反斜杠。

例如

inFile.open("path/to/file.txt")

代替

inFile.open("path\\to\\file.txt")

What fixed it for me was using forward slashes instead of double backslashes in my filepath.

e.g.

inFile.open("path/to/file.txt")

instead of

inFile.open("path\\to\\file.txt")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文