char* pch;
pch = strtok(instruction," "); //gets rid of the instruction and space
pch = strtok(NULL," "); //sets pch to the address part of instruction
int address = (int)strtol(pch,NULL,0);>
my intent is convert "r aeb2d86f0980" into an integer format. When I get to the strtol instruction I'm getting a segmentation fault. Any idea why?
Be cautious when using these functions. If you do use them, note that:
* These functions modify their first argument.
* These functions cannot be used on constant strings.
* The identity of the delimiting byte is lost.
* The strtok() function uses a static buffer while parsing, so it's not thread safe. Use strtok_r() if this matters to you.
If your file contains anything other than lines formatted as "xyz 123abc", even blank lines, then your function will fail.
Your first post appears to be C, but your last post shows that you are using C++. Why are you trying to use C stuff when you have C++ available to you? (It is misunderstanding the C stuff that is causing you to fail.)
void decode_inst( string s )
{
if (s.empty()) return;
istringstream ss( s );
string name;
unsignedlongint value;
ss >> name >> hex >> value;
if (!ss.eof())
{
cout << "Congratulations. The instruction name was " << name << " and the value was " << hex << value << ".\n";
}
else
{
cout << "Alas, the not-empty line was not of the form \"NAME HEX_VALUE\".\n";
}
}