Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using URLDownloadToFile for multiple links in a file

Status
Not open for further replies.

thegame8311

Technical User
Jan 6, 2012
133
0
0
US
ok, I'm going to try to explain this the best way possible, so here it goes:

I am trying to run URLDownloadToFile, using links from a file, so It would read the first link, process it, save it to a file, read the next link, process it, save it to the same file.

Problem is that even if i put it in a function, that is called from a loop, that it will process the first link, save all the info that I want, then overwrite the info from the first link with the second link, then go back and process the first link again and over write the second link info:

here is the code:

Code:
string line;
	ifstream flink;
	ifstream fin;
	ifstream fin2;
	ofstream fout;
	ofstream fout2;
	std::string plink;
	string start = "profile\"";
	string end = "</a>";
	string tbod = "<tbody>";
	string etbod = "</tbody>";
	string slink = "<a href=";
	bool tbody = false;
	string Name;
	
	
	flink.open("playerlinks.txt");
	std::string str; LPCSTR lpcstr = str.c_str();
	getline(flink, plink);
	while(flink)
	{
		
		cout << (LPCSTR)plink.c_str() << endl;
	   
		ProcsLink(plink);
            {
            //Some other code for saving info
                fout2 << nPname << " ";
		fout2 << ntname;
		fout2 << endl;
		}
		flink >> plink;
	
	cout << "hi" << endl;
	system("PAUSE");
	//return 0;

	
	}
}

here is the function that is supposed to handle the link:

Code:
void ProcsLink(string plink)
{
	string line;
	
	ifstream fin;
	
	ofstream fout;
		
	string start = "profile\"";
	string end = "</a>";
	string tbod = "<tbody>";
	string etbod = "</tbody>";
	string slink = "<a href=";
	bool tbody = false;
	string Name;
	
		HRESULT hr = URLDownloadToFile(NULL, (LPCSTR)plink.c_str(), _T("test2.txt"), 0, NULL);
	
		fin.open("test2.txt");
		fout.open("test3.txt");
		while(fin.good())
		{
		
			getline(fin, line);

			if(line.find(tbod) != string::npos)
			{
				tbody = true;
			}
			if(line.find(etbod) != string::npos)
			{
				tbody = false;
			}
			if(line.find(start) != string::npos && tbody == true)
			{
				
				
				fout << line;
				
			}
			else if(line.find(slink) != string::npos && tbody == true)
			{
				
				fout << line;
				fout << endl;
				
			}	
		}
}
 
Pass the next file name as a second parameter of ProcsLink. Generate new names or extract names from links in the main loop:
Code:
bool ProcsLink(const string& link, const string& fname)
{
    ...
}
...
for (int fno = 0; getline(flink,link); ++fno) {
    ostringstream ss;
    ss << "test" << fno << ".txt";
    string fname(ss.str());
    ProcsLink(link,fname);
    ...
}
Avoid passing std::string arguments by value (it's ineffective method), use references.

It seems the true problems with your code are error testing and download synchronization.

Are you sure that new thread on the same topic is OK?..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top