我不完全熟悉CLI的工作方式,但我有一個大概的想法。我有一個函數需要2個System :: String變量,並使用它們從網頁下載文件。就下載而言,它工作正常,文件顯示在我的目錄中並具有必要的內容。然而,它給我的錯誤C++/CLI - URL下載到文件
類型的未處理的異常「System.AccessViolationException」 發生在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);
}
}
我做了一些挖掘和輸出測試,並且發現了該exe文件在文本文件中的某處發生斷點,因爲整個網頁沒有保存到txt文件中。
對於相關代碼:
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";
品牌新的錯誤! :(
類型 'System.Runtime.InteropServices.SEHException' 的未處理的異常發生在ParseLinks.exe
的代碼的文件後 - >線陣列環
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
功能如下:
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 && 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;
}
錯誤指向我的大型公司在這個函數中重新聲明。 (另外,我在壓縮我的代碼時顯然很糟糕)
'線路'是什麼,它是如何初始化的? –
這是我用於每行文本的數組。我從文件中取出每行文本,將其填充到數組中(用於循環目的),然後將每行寫入一個函數以確定它是否包含鏈接。對於找到的每個鏈接,它會將其拍攝到第二個鏈接的數組中。 (它會忽略重複項和外部網站)對於鏈接數組中的每個鏈接,它會重新執行整個過程,轉到該網站,下載頁面,解析鏈接並將它們添加到數組中。這基本上是一個粗糙的網站爬蟲。 另外,oops:我只將這兩個數組初始化爲100個元素,謝謝! –
沒有保存全部**不**意味着錯誤是保存過程中的一部分。它很可能是,但系統通常緩衝文件寫入和訪問衝突(如果它導致程序終止)可以使「寫入」(但真正只是緩衝)的數據消失在空氣中。 – ssube