C++ Adapting sscanf code to istringstream with error checking - c++

So I've got some code that splits a string into some other variables with sscanf that looks like this.
if(sscanf(input_line.c_str(), "%s %s %lf %lf %lf", &string1, &string2, &point1, &point2, &point3) != 5) {
//does stuff
throw;
}
I use the return value of sscanf to validate the input string.
I would use
istringstream ss(input_line);
ss >> string1 >> string2 >> point1 >> point2 >> point3;
if( ??????? ){
//does stuff
throw;
}
But I don't know how to verify if there were exactly 5 things populated. I'd love to get rid of the legacy C code as I loath char arrays but I don't want to lose the error checking. I'd very much appreciate any advice to adapt this istringstream code or use entirely different c++ style code to get rid of the char arrays but keep the existing functionality.
Thanks in advance,
Max

First of all remember that all input operator functions (>>) returns the input stream, and secondly remember that a stream can be used as a boolean condition.
This means you can do e.g.
std::string string1, string2;
double point1, point2, point3;
if (some_input_stream >> string1 >> string2 >> point1 >> point2 >> point3)
{
// All is okay
}
else
{
// One of the input operations failed
}
To know exactly which input operation failed, you can do them one at a time.

Related

Extract data from c++ string

I wanted to extract the values from the string in C++. I guess this is not the C++ way of doing this, but that one in particular doesn't work. Any ideas?
string line = "{10}{20}Hello World!";
int start;
int end;
string text;
// What to use here? Is the sscanf() a good idea? How to implement it?
cout << start; // 10
cout << end; // 20
cout << text; // Hello World!
Although you can make sscanf work, this solution is more appropriate for C programs. In C++ you should prefer string stream:
string s("{10}{20}Hello World!");
stringstream iss(s);
Now you can use the familiar stream operations for reading input into integers and strings:
string a;
int x, y;
iss.ignore(1, '{');
iss >> x;
iss.ignore(1, '}');
iss.ignore(1, '{');
iss >> y;
iss.ignore(1, '}');
getline(iss, a);
Demo.
You could use the method String.find() to get the positions of '{' and '}' and then extract the data you want through String.substr().
text does not need to have an "&" in front of it inside the sscanf, since string names are already pointers to their starting address.
sscanf(line, "{%d}{%d}%s", &start, &end, text);

Trouble with Out of range in memory using stringsteam

I just started using the stringstream for the first time and I love the concept, but I am having a hard time finding where exactly I am having an out of range in memory with my stringstream function.
What my function does is it takes in a string, for example, "N02550 G3 X16.7379 Y51.7040 R0.0115" This is machine code for a CNC machine at my job. I pass the string to a stringstream in order to find the strings that have a X, Z, Y next to them, these are coordinates. It then gets rid of the character at the beggining in order to save the float number to my struct "Coordinate"(there are 3 doubles, x, y, z).
When I run a text file that has this machine code with 33 lines, my program works. When I run it with machine code of 718 lines, it gets to 718, then crashes with out of range memory. Then another weird part is when I run machine code with 118,000 lines, it goes up to around 22,000 lines then crashes. So I'm having trouble figuring out why it is able to do that and whats causing the problem.
Here is the function:
void getC(string& line, Coordinates& c)//coordinates holds 3 doubles, x, y, z
{
//variables
string holder;
stringstream ss(line);
while(ss)
{
ss >> holder;
if(holder.at(0) == 'X')
{
holder.erase(0,1);//get rid the the character at the beggining
stringstream sss(holder);
sss >> c.x;
sss.clear();
}
if(holder.at(0) == 'Y')
{
holder.erase(0,1);
stringstream sss(holder);
sss >> c.y;
sss.clear();
}
if(holder.at(0) == 'Z')
{
holder.erase(0,1);
stringstream sss(holder);
sss >> c.z;
sss.clear();
}
if(ss.eof()) // to get out of the ss stream
break;
}
ss.clear();
}
If you want to see the whole application(the application is well documented) then ask or if you need the txt files containing the machine code. Thank you!
Try changing:
while(ss)
{
ss >> holder;
...
if(ss.eof()) // to get out of the ss stream
break;
}
To simply this:
while(ss >> holder)
{
...
}
And you can get rid of those calls to clear in each branch (X/Y/Z) as it doesn't really do anything given that sss is a temporary and you're not doing anything more with it (no point setting flags on something you're going to discard right after). I suspect your out of range issue is coming from trying to access holder.at(0) after ss >> holder fails.
You generally want to check for input failure right after reading a token, and a convenient way to both attempt to input and check for failure at once is to simply check if ss >> token evaluates to true. So we can write code like:
if (ss >> token)
{
...
}
else
{
// handle failure if necessary
}
I generally find it's a lot easier to avoid getting in trouble writing code that way than manually checking error flags.
As a simplified version:
void getC(string& line, Coordinates& c)
{
stringstream ss(line);
for (string holder; ss >> holder; )
{
const char ch = holder.at(0);
stringstream sss(holder.substr(1));
if (ch == 'X')
sss >> c.x;
else if (ch == 'Y')
sss >> c.y;
else if (ch == 'Z')
sss >> c.z;
}
}

How to read in a double from a file in c++

How do you read in a double from a file in C++?
For ints I know you can use the getline() and then atoi, but I am not finding an array to double function. What is available for reading in doubles, or converting a char array to a double?
You can use stream extraction:
std::ifstream ifs(...);
double d;
ifs >> d;
This work provided that other then whitespace, the next data in the stream should be a double in textual representation.
After the extraction, you can check the state of the stream to see if there were errors:
ifs >> d;
if (!ifs)
{
// the double extraction failed
}
Do not consider using atof(), or any of the ato.. functions, as they do not allow you to diagnose errors. Take a look at strtod and strtol. Or use the stream extraction operators.
I'm wondering, does one need to be careful about locale settings (e.g. a locale could use comma instead of dot to separate the decimal part) or do stringstreams always default to some standard "C locale" notation?
You can leverage istringstream For example, here are toDouble and toInt:
double toDouble(string s) {
double r = 0;
istringstream ss(s);
ss >> r;
return r;
}
int toInt(string s) {
int r=0;
istringstream ss(s);
ss >> r;
return r;
}

boost lexical_cast throws exception

I'm using boost libs for c++ and the function lexical_cast behaves really weird. If I do lexical_cast("0.07513994") it works fine, but if I use my variable which I need to convert, it throws the bad_lexical_cast exception. Here is the code:
string word;
istringstream iss(line);
do
{
string word;
iss >> word;
double x;
x = lexical_cast<double>(word);
cout << x << endl;
} while (iss);
What am I doing wrong here? I appreciate any help, thanks
Your problem is probably that the loop is processed one more time than you expect.
The last time through the loop, the read to word fails, setting the fail bit in iss, which is what while(iss) is checking. To fix it you need to do something like this.
string word;
istringstream iss(line);
do
{
string word;
iss >> word;
if(iss)
{
double x;
x = lexical_cast<double>(word);
cout << x << endl;
}
} while (iss);
Unlike functions such as atof() which stop parsing as soon as they see an invalid character, lexical_cast requires that every character in the input string be valid. i.e. any leading or trailing spaces will cause it to throw an exception.
You want to see what kind of input it is getting and trim it accordingly. You should also catch bad_lexical_cast just in case it does get input which is completely garbage.
One possible solution is to use boos.regex or boost.xpressive to extract a valid sub-string and pass the result to lexical_cast.
The problem is probably that you are sending an empty string when there is no data left.
You should change the loop you are using.
Use the while {} loop (not the 'do while' loop). This allows you to read from the stream and test it in a single easy to read statement. Note the result of iss >> word is the stream. When used in this boolean context it is tested to see if the state is good and its value converted into something that can be used by the while condition. Thus if the operator >> filed to work correctly then the loop is never entered.
istringstream iss(line);
string word;
while(iss >> word)
{
double x = lexical_cast<double>(word);
cout << x << endl;
}
But really you don't even need the lexical cast in this situation (unless you want to test for non numbers with an exception). The standard stream operator will convert the input into a double.
istringstream iss(line);
double word;
while(iss >> word)
{
cout << word << endl;
}
if (iss.fail())
{ /* Failure to convert input to a double */
}

c++, get phone number from txt file

I'm trying input a phone number in the format: 555-555-5555 into a struct with three int's. I've tried using getline with a delimiter of "-", but I keep getting the error: "cannot convert parameter 1 from 'int' to 'char *'".
I tried creating a temp char* variable to store the number in and then type casting it to int, but that didn't work.
How should I go about doing this?
Thanks
edit:
here's some of the code:
void User::Input(istream& infile) {
char* phone_temp;
...
infile.getline(phone_temp, sizeof(phoneNum.areaCode), "-");
phoneNum.areaCode = (int)phone_temp;
...
}
Since you are posting this as a c++ question, and not a c question, Use istringstream
http://www.cplusplus.com/reference/iostream/istringstream/
From my head it your code would become something like:
std::string sPhoneNum("555-555-5555");
struct
{
int p1;
int p2;
int p3;
} phone;
char dummy;
std::istringstream iss(sPhoneNum);
iss >> phone.p1; // first part
iss >> dummy; // '-' character
iss >> phone.p2; // second part
iss >> dummy; // '-' character
iss >> phone.p2; // last part
EDIT:
now that you have posted example code, I see you already start with an istream, you can just use the >> operator directly, no need to create another istringstream operator. See examples: http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/
Also, stay away from c-style conversion methods with char * and atoi stuff if you don't have to, working with std::string and istreams is the "right" C++ way. It avoids memory leaks and other nasty problems.
Reading a phone number from a stream:
Assuming the number is well formatted:
void User::Input(istream& infile)
{
int part1;
int part2;
int part3;
char dash1;
char dash2;
infile >> part1 >> dash1 >> part2 >> dash2 >> part3;
/*
* !infile will return false if the file is in a bad state.
* This will happen if it fails to read a number from
* the input stream or the stream ran out of data.
*
* Both these conditions constitute an error as not all the values will
* be set correctly. Also check that the dash[12] hold the dash character.
* Otherwise there may be some other formatting problem.
*/
if ((!infile) || (dash1 != '-') || (dash2 != '-'))
{
throw int(5); // convert this to your own exception object.
}
}
if I understand correctly, try atoi() or stringstream to convert from char* to int
See this example on how you can tokenize the line. This question will also help.
Then use atoi to convert string to int.
You can't cast a char* to an int and expect a correct value. A char* is an address in memory, so when you cast it to int, you'll get a memory address in your int. You need to call a function, such as atoi() to algorithmically convert the data char* is pointing to into an integer.
Another viable option, although not quite C++, is:
char a[10],b[10],c[10];
scanf("%d-%d-%d", a, b, c);
It appears you're trying to convert a char to an integer, in which case you'd want to use the atoi function or a string stream.
rather than using infile.getline() use the free standing version with a std::string:
getfile(infile, buffer);
After that, if you'd like you can do an addition getline():
istringstream phonenumber(buiffer);
string areacode = getline(phonenumber, part1. '-');
or you can use the extractor >> (that's what it's for!)
int areacode;
phonenumber >> areacode;
Just a side note: if you're using char* do make sure you allocate space for it, or at least point to allocated space.