changed:problem with c++ strings +a question about wget

Started by
21 comments, last by ranakor 19 years, 4 months ago
edit 2: tiny question about wget wich didn't deserve it's own thread is there a way to specify a filename? say i want to download something from http://hippyarecool.com/leetfile.mpg but i want it stored as thisisaseriousfile.mpg is there a way to do it on command line? (flought over the option list but didn't see it) any idear what could be going wrong there? MainIndexUrl="sometexthere"; MainIndexUrl+=FileCounter; MainIndexUrl+="&"; cout<<MainIndexUrl; assuming filecounter is 22if i remove the last line i have: (edit:filecounter is a STRING containing "22" not a int) sometexthere22 & with the last line i'd expect sometexthere22& but for some reason the & doesn't apear & seems to eat a char & i get that: sometexthere2 any idear? (i'm assuming & is a special char or soemthing?) ps:before someone rushes me for not using code tags i didn't do so because it would be harder to read (no types nor keyword in there so all you'd get is a big textbox with the same text inside beside it's a very tiny snippet) edit:this code throw an exception (last line replaced) MainIndexUrl="view.cgi?id=koh0130&now=3&jd=-1&ino="; MainIndexUrl+=FileCounter; MainIndexUrl+="test"; so seems it's not the & causing trouble? [Edited by - ranakor on December 1, 2004 4:16:48 PM]
Advertisement
Usually "&" is a reference to an address in memory in C++.

my two guesses are to try an escape character "\&" (doubt it)
or use strcat to combine the two strings.
i already tryed "\&" & it didn't work either( & i know & is a keyword but keyword aren't parsed usualy inside of "" (like you can do this i'm fairly sure)

mystring= "for continue break goto return"

so i assumed & had another meaning in "" like the \ does but i'm rather clueless there(
It's treating FileCounter as char instead of int. You need to convert the integer-value to a char-value, with either strstreams or atol().
sorry forgot to add filecounter is a string (& poses no problem) it's created that way

int counter=22
string filecounter=itoa(counter);

& removing the part with the & the mystring cout just fine (including counter at the end)

[Edited by - ranakor on December 1, 2004 1:48:17 PM]
Quote:Original post by ranakor
string filecounter=atoi(counter);


I assume this is a typo? atoi is used to go from array to integer.
'&' has a special meaning within win32 (it underlines the next character for shortcut keys). Since you're using 'cout' to print it to the console, that's not the case here.

Must be something with FileCounter then (itoa?).
yup your right it's a typo (but only in the post) i'll just post the whole code (it doesn't fully make sence as it was going to be 2 prog & i'm actually coding the 2nd inside the 1st so just look for something messing up what i did not for other weirdo stuff p) oh year & i know all those globals are evil=)
[SOURCE]#include <iostream>#include <fstream>#include <string>#include <cstdlib>using namespace std;//globals for easyness's sakestring FilePath="c:\\test save avi\\";string FileName="\\Dr Koh's Guitar Sarang VOD.htm";string StrCurrentFileName;string StrCurrentFileNameOut;string MainIndexUrl;string FileContent[100];string WgetString;string IndexFilePath="c:\\test save avi\\index\\index.htm";string IndexFile;char FileCounter[50]={0};int counter=1;char tmp;struct indexing{	int indexfile;	int indexurl;	int indexurlend;};int stringsize;indexing currentindex;indexing mainindex;int main(){for(;;){	StrCurrentFileName.clear();	StrCurrentFileNameOut.clear();	WgetString.clear();	ifstream IndexParser(IndexFilePath.c_str());	itoa(counter,FileCounter,10);	while(IndexParser.get(tmp))	{		IndexFile+=tmp;	}	MainIndexUrl="view.cgi?id=koh0130&now=3&jd=-1&ino=";	MainIndexUrl+=FileCounter;//this doesnt crash	MainIndexUrl+="test";//this chrashes	mainindex.indexfile=IndexFile.find(MainIndexUrl,0);	mainindex.indexurlend=IndexFile.find("\"",mainindex.indexfile);	WgetString=IndexFile.substr(mainindex.indexfile,mainindex.indexurlend-mainindex.indexfile);	cout<<WgetString;	system(WgetString.c_str());	StrCurrentFileName=FilePath+FileCounter+FileName;	cout<<StrCurrentFileName;	ifstream CurrentFile(StrCurrentFileName.c_str());	StrCurrentFileNameOut=FilePath+"URL.txt";	ofstream CurrentFileOut(StrCurrentFileNameOut.c_str(),ios::app);	if(CurrentFile.fail())	{		CurrentFile.close();		return 1;	}	while(CurrentFile.get(tmp))	{		FileContent[counter]+=tmp;	}	CurrentFile.close();	currentindex.indexfile=FileContent[counter].find("Filename",0);	currentindex.indexurl=FileContent[counter].find("http://",currentindex.indexfile);	currentindex.indexurlend=FileContent[counter].find("\"",currentindex.indexurl);	stringsize=currentindex.indexurlend-currentindex.indexurl;	CurrentFileOut<<counter<<")"<<FileContent[counter].substr(currentindex.indexurl,stringsize)<<endl;		cout<<"\n"<<FileContent[counter].substr(currentindex.indexurl,stringsize);		CurrentFileOut.close;		cin>>tmp;		counter++;	}	return 0;	}[/SOURCE]


[Edited by - ranakor on December 2, 2004 7:17:26 PM]
Why not use ostringstream (include <sstream>) to generate the strings? This way you do not have to handle conversion operations (itoa, which is platform specific code). So the operation becomes:

ostringstream ostr;
ostr << "view.cgi?id=koh0120&now=3&jd=-1&ino=" << counter << "&";
MainIndexUrl = ostr.str();

I tested this code on Linux and it worked find. Ampersand was in place with no missing characters.

A minor nitpick is that FileCounter is not initialized. Bad idea. Change it to be:

char FileCounter[50] = {0}; // Initialize the elements to zero

Hope this helps,
Chris

This topic is closed to new replies.

Advertisement