C ++ fstream is_open()函数总是返回false [英] C++ fstream is_open() function always returns false

查看:4335
本文介绍了C ++ fstream is_open()函数总是返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用open()在与源文件相同的目录中打开html文件。但是我不知道为什么is_open()总是返回false在我的程序....
这是我的源代码

I have tried to use open() to open html file in the same directory as the source file. However I dont know why is_open() always return false in my program.... Here is part of my source code

的一部分打开html文件in readHTML.cpp

one of the functions to open html file in readHTML.cpp

 #include "web.h"
         ...
void extractAllAnchorTags(const char* webAddress, char**& anchorTags, int& numOfAnchorTags)
    {
        ifstream myfile;
        char line[256];
        char content[2048] = "";

        numOfAnchorTags = 0;
        myfile.open(webAddress);
        cout<< myfile.is_open()<<endl;
        if (myfile.is_open())
        {......}
    }

头文件,web.h

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

struct WebNode
{
    char* webAddress; 
    char* anchorText;
    WebNode** hyperlink;
    int numOfHyperlinks;
};

void extractAllAnchorTags(const char* webAddress, char**& anchorTags, int& numOfAnchorTags);

另一个cpp档案,callF.cpp

another cpp file, callF.cpp

#include "web.h"
........
WebNode* createWeb(const char* webAddress, const char* anchorText, int height)
{
    if(height != 0){
       WebNode* webNode = new WebNode;
       char** anchorTags;
       int numOfAnchorTags;
       strcpy(webNode->webAddress,webAddress);
       strcpy(webNode->anchorText,anchorText);
       extractAllAnchorTags(webAddress,anchorTags,numOfAnchorTags);
        \*.........................*\
 }
 .......

main.cpp

#include "web.h"

     int main(){
        .............
        cin >> filename;  // index.html   would be input during running.
        Page = createWeb(filename, anchorText, max_height);
        .............
      }

我的main.cpp只是调用createWeb一次
但是我得到的myfile.is_open()总是返回false,因为它在eclipse的控制台中输出0 ...
我不知道为什么,尝试将我的参数目录重置为我的工作区
或我把我的html文件在调试文件夹内。它仍然返回false。

my main.cpp just call createWeb once However what i get that myfile.is_open() always returns false since it prints out 0 in my console of eclipse... I have no idea why even I try to reset my argument directory to my workspace or I put my html file inside debug folder .. it still returns false.

推荐答案

您写入未初始化的指针:

You write to uninitialized pointers:

struct WebNode
{
    char* webAddress; 
    char* anchorText;
    WebNode** hyperlink;
    int numOfHyperlinks;
};

// ...

WebNode* webNode = new WebNode;
strcpy(webNode->webAddress,webAddress);

指针 webNode-> webAddress 不是目前指向任何地方。要解决这个问题,请将其类型从 char * 更改为 std :: string ,并使用字符串赋值来复制字符串

The pointer webNode->webAddress does not currently point anywhere. To fix this, change its type from char * to std::string, and use string assignment to copy the string contents.

您复制到未初始化的指针导致未定义的行为,这可能导致内存损坏等,因此您的程序的其余部分似乎失败。

Your copying to uninitialized pointer causes undefined behaviour which may result in memory corruption and so on, so the rest of your program appears to fail.

此外,您还应重新设计 extractAllAnchorTags 以不使用指针。

Also you should redesign extractAllAnchorTags to not use pointers.

这篇关于C ++ fstream is_open()函数总是返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆