C++ C++;fstream is_open()函数始终返回false

C++ C++;fstream is_open()函数始终返回false,c++,fstream,C++,Fstream,我尝试使用open()打开与源文件位于同一目录中的html文件。但是我不知道为什么在我的程序中总是返回false。。。。 这是我的部分源代码 在readHTML.cpp中打开html文件的函数之一 #include "web.h" ... void extractAllAnchorTags(const char* webAddress, char**& anchorTags, int& numOfAnchorTags) { ifstre

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

在readHTML.cpp中打开html文件的函数之一

 #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())
        {......}
    }
main.cpp

#include "web.h"

     int main(){
        .............
        cin >> filename;  // index.html   would be input during running.
        Page = createWeb(filename, anchorText, max_height);
        .............
      }
my main.cpp只需调用createWeb一次 然而,我得到的myfile.is_open()总是返回false,因为它在我的eclipse控制台中打印出0。。。 我不知道为什么我会尝试将参数目录重置为我的工作区
或者我将html文件放在调试文件夹中。。它仍然返回false。

ifstream
无法打开网站。它只打开文件。您需要使用诸如CURL之类的库。

写入未初始化的指针:

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

// ...

WebNode* webNode = new WebNode;
strcpy(webNode->webAddress,webAddress);
指针
webNode->webAddress
当前未指向任何位置。要解决此问题,请将其类型从
char*
更改为
std::string
,并使用字符串赋值来复制字符串内容

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


您还应该重新设计
extractAllAnchorTags
以避免使用指针。

如果我需要读取html源代码中的内容,那么我想读取html文件的源代码怎么办?我试着创建另一个程序,它只是简单地打开html文件,它对我来说是真的。。。我不知道为什么会发生这种情况。我假设变量
webAddress
是一个URL。我说的对吗?webAddress是html名称,例如index.html。如果打开文件或URL,我会将index.html与源代码放在同一个文件夹中。然后我使用webAddress作为变量,将'index.html'存储为字符串数组。然后,我应该打开该文件以获取index.html文件的源代码的一些信息哪个操作系统?我正在使用Windows 7 64位Hanks来激励我。我尝试交换代码的顺序以避免这种内存损坏。谢谢你帮我节省了很多时间
#include "web.h"

     int main(){
        .............
        cin >> filename;  // index.html   would be input during running.
        Page = createWeb(filename, anchorText, max_height);
        .............
      }
struct WebNode
{
    char* webAddress; 
    char* anchorText;
    WebNode** hyperlink;
    int numOfHyperlinks;
};

// ...

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