title: C++/CLI - URL Download to File
body: <p>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 </p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.AccessViolationException'
    occurred in ParseLinks.exe</p>
  </blockquote>
  
  <pre><code>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-&gt;DownloadFile(_URL, _saveAs);
          webClient-&gt;Dispose();
      }
      catch (System::Exception ^_e)
      {
          // Error
          System::Console::WriteLine("Exception caught in process: {0}", _e);
      }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Relevant code for that:</p>
  
  <pre><code>        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&lt;&lt;"Error: Could not access file\n";
  </code></pre>
  
  <p>Brand New Error! :(</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in ParseLinks.exe</p>
  </blockquote>
  
  <p>The code after the file -> line array loop</p>
  
  <pre><code>myFile.close(); //Close txt file
  
              //Loop through lines
              for (int i = 0; i &lt; count; i++)
              {
                  string temp = parseLinks(lines[i]); //parse links from each line
  </code></pre>
  
  <p>The function for that:</p>
  
  <pre><code>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 &amp;&amp; str[i] != '"')
              link += str[i];
      }
  
      //Discard non-link text
      for (int i = 0; i &lt; 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;
  }
  </code></pre>
  
  <p>The error points to my large compare statement in this function. (Also, I'm clearly terrible at compressing my code)</p>