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

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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]

##### Share on other sites
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.

##### Share on other sites
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(

##### Share on other sites
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().

##### Share on other sites
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]

##### Share on other sites
Quote:
 Original post by ranakorstring filecounter=atoi(counter);

I assume this is a typo? atoi is used to go from array to integer.

##### Share on other sites
'&' 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?).

##### Share on other sites
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=)

##### Share on other sites
[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]

##### Share on other sites
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

##### Share on other sites
good point on filecounter init will change it

will try your solution but still interested in being less of an idiot then yesterday so if anyone has a clue about what was wrong lemme know=)

##### Share on other sites
Why is it a bad idea to not initialize a variable with a value before changing the value to what you want and then using it?

##### Share on other sites
Why is it a bad idea to not initialize a variable with a value before changing the value to what you want and then using it?

##### Share on other sites
int index; // random number, depends on your compiler
char str[] = "mystring";
str[index] = 'o'; // oops! access violation!

##### Share on other sites
anon user:

in this case it's a char array to be used as a c string (so it must be null terminated) & simply writing to it means space where you won't have wrote isn't guaranteed to be 0 (thus if you pass that string to a function expecting a null terminated string it may try to read past the bounds)

& i'm still clueless about what's going wrong in my prog (but got a gut feeling it could come from filecounter being to big or something? (the error i get when adding text to it is std::out_of_range

##### Share on other sites
Instead of using double quotes around the & using single. Im sure that when you have a single character you have to use single quotes?

So you could try '&' and '\&'.

Sorry if thats no help:)

##### Share on other sites
well using double quote makes it &\0 stored instead of & as a string but sine a string can take a string as an argument it shouldn't be a problem (beside with osstream it works just fine with the "&")

really'd like to know what wasn't working hehe

##### Share on other sites
bump still no clue about what caused my (fixed) original problem nor about my wget question

##### Share on other sites
Some more problems:
You use char tmp but...
1) never initialized it. In debug mode it is set to zero. Just got lucky on that.
2) Never set it to any value. You have while(IndexParser.get(tmp)) {} but temp is 0x00.

Same thing with IndexFile. It is initialized to the empty string so when you execute the find on it, it does not find the MainIndexURL in it. This returns 0xffffffff, which will then cause the out of range exception being thrown.

So it seems to me that that basic issue is non-initialization of variables and not setting their values before using them, which kinda goes hand in hand with initializing them. Then you do not have a check to see if you really did find the string or not. =)

Fix these problems and your code will probably work a bit better.

Chris

##### Share on other sites
i'm not sure what you mean about tmp it doesn't need to be initialised it's a single char & what's in it before get(tmp) doesn't matter as the point of get is to have a value put in tmp not to pass what's in tmp as for indexfile it is initialised here :

while(IndexParser.get(tmp))
{
IndexFile+=tmp;
}

it is a string set to whatever was in the file

ps: this part works good dunno why you talk about it the problem always was about those 3 lines:

MainIndexUrl="view.cgi?id=koh0130&now=3&jd=-1&ino=";
MainIndexUrl+=FileCounter;//this doesnt crash
MainIndexUrl+="test";//this chrashes

in that this snippet works fine untill the 3rd line (& works fine is mainindexurl is a ostringstream but doesn't if it's a string)

bump

##### Share on other sites
My bad, you are right about tmp. Missed that piece of code somehow...

Are you sure you are opening the file? When I compiled the code I did not have the file, hence the get would fail and tmp was 0x00. This caused the rest of the problems to occur. The test line works for me with the code you supplied hence the references to the other posssible problems I thought I saw.

Chris

##### Share on other sites
as i said the whole program works fine with your stream solution it just didn't work with the same code when MainIndexUrl was a string

MainIndexUrl="sometexthere";
MainIndexUrl+=FileCounter;
MainIndexUrl+="&";

this work when mainindexurl is a ostringstream but not when it's a string (& i'm wondering what's going wrong since the 2nd line works fine but i have a gut feeling that since the 3rd seems normal the 2nd may have screwed up the string for adding more on top of it) filecounter being a char[50]