开发者

C++/CLI - URL Download to File

开发者 https://www.devze.com 2023-04-07 01:26 出处:网络
I\'m not entirely familiar with how CLI works, but I have a general idea. I have a function that takes 2 System::String variables, and uses those to download a file from a webpage. As far as the downl

I'm not entirely familiar with how CLI works, but I have a general idea. I have a function that takes 2 System::String variables, and uses those to download a file from a webpage. As far as the download goes, it works fine, and the file shows up in my directory with the necessary content. However, it gives me the error

An unhandled exception of type 'System.AccessViolationException' occurred in ParseLinks.exe

void downloadFile(System::String ^_URL, System::String ^_saveAs)
{
    try
    {
        System::Net::WebClient ^webClient = gcnew System::Net::WebClient();
        // Downloads the resource with the specified URI to a local file.
        webClient->DownloadFile(_URL, _saveAs);
        webClient->Dispose();
    }
    catch (System::Exception ^_e)
    {
        // Error
        System::Console::WriteLine("Exception caught in process: {0}", _e);
    }
}

I did some digging and output testing, and found out that the exe is hitting a break point somewhere in the text file, as the entire webpage did not save to the txt file.

Relevant code for that:

        if (myFile.is_open()) //if file open
        {
            while (!myFile.eof()) //before end of file
            {
                getline(myFile, ln);
                lines[count] = ln;
                count++; //count total lines to set loop length for later parsing
                //Error occurs somewhere in here
            }
            myFile.close();
        }
        else
            cout<<"Error: Could not access file\n";

Brand New Error! :(

An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in ParseLinks.exe

The code after the file -> line array loop

myFile.close(); //Close txt file

            //Loop through lines
            for (int i = 0; i < count; i++)
            {
                string temp = parseLinks(lines[i]); //parse links from each line

The function for that:

string parseLinks(string str)
{
    const int len = str.length();
    string link;
    bool quotes = false, islink = false;
    string compare[5] = {".htm",".html",".php",".asp",".pdf"};

    //Parse all quoted text
    for (int i = 0; i != len; i++)
    {
        //Change bool if quote found
        if (str[i] == '"')
        {
            if (quotes == false)
                quotes = true;
            else
                quotes = false;
        }

        //If bool true, and char is not a quote, add to link string
        if (quotes == true &&am开发者_运维百科p; str[i] != '"')
            link += str[i];
    }

    //Discard non-link text
    for (int i = 0; i < 5; i++)
    {
        //Link check for links given array of path filetypes
        if (link.compare((link.length() - compare[i].length()),compare[i].length(),compare[i]) == 0)
            islink = true;
    }
    //Link check for links with no path filetype (.html, .php, etc.)
    if (link.compare(0,7,"http://") == 0)   
        islink = true;

    //If not a link, return empty string
    if (islink == false)
        link = "";

    return link;
}

The error points to my large compare statement in this function. (Also, I'm clearly terrible at compressing my code)


You're using getline wrong, and possibly that's causing your error. The correct idiom is this:

std::string line;
while (std::getline(myFile, line))
{
  // process `line`
}

There's no need to check myFile for openness separately.

0

精彩评论

暂无评论...
验证码 换一张
取 消