atoi and cstring
-
I am reading in CString values from a file and atoi-ing them. Docs say it returns 0 if the string cant be converted to a number (though the example there says atoi("5 cars") returns 5 -- I thought it would return 0 ???) Confused about this. The other thing is that I might have a CString "0". If atoi returns 0 in this case, then its not signalling an error. So would I treat the case of strings like "0" separately? Thanks, ns
-
I am reading in CString values from a file and atoi-ing them. Docs say it returns 0 if the string cant be converted to a number (though the example there says atoi("5 cars") returns 5 -- I thought it would return 0 ???) Confused about this. The other thing is that I might have a CString "0". If atoi returns 0 in this case, then its not signalling an error. So would I treat the case of strings like "0" separately? Thanks, ns
atoi()
would return 0 for "cars 5" since it found no numbers before it found a non-number. In other words, it converts all numbers up to the first non-number encountered. Differentiating between the return values ofatoi("0")
andatoi("T")
sounds like a design issue. What is the format of the incoming data?
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?
-
I am reading in CString values from a file and atoi-ing them. Docs say it returns 0 if the string cant be converted to a number (though the example there says atoi("5 cars") returns 5 -- I thought it would return 0 ???) Confused about this. The other thing is that I might have a CString "0". If atoi returns 0 in this case, then its not signalling an error. So would I treat the case of strings like "0" separately? Thanks, ns
atoi starts translation first by removing seperator characters(like space) from the string, looking for the first character. if it is a digit, it starts translating it into an integer, but if not, it returns zero. you can use it like this: LPSTR ss="4 cars"; int x=atoi(ss); if(x==0 && strcmp(ss,"0")){ // the input is probably not a real zero, // it's an error }
-
atoi starts translation first by removing seperator characters(like space) from the string, looking for the first character. if it is a digit, it starts translating it into an integer, but if not, it returns zero. you can use it like this: LPSTR ss="4 cars"; int x=atoi(ss); if(x==0 && strcmp(ss,"0")){ // the input is probably not a real zero, // it's an error }
Actually it would be better to use strncmp instead of strcmp because then you specify the number of characters to compare so it would return correctly if the string contained "0 cars" also you would want to trim out the spaces in the string first before the compare in case you had a string " 0 cars" so that it would return correctly as well.
-
atoi starts translation first by removing seperator characters(like space) from the string, looking for the first character. if it is a digit, it starts translating it into an integer, but if not, it returns zero. you can use it like this: LPSTR ss="4 cars"; int x=atoi(ss); if(x==0 && strcmp(ss,"0")){ // the input is probably not a real zero, // it's an error }
-
atoi()
would return 0 for "cars 5" since it found no numbers before it found a non-number. In other words, it converts all numbers up to the first non-number encountered. Differentiating between the return values ofatoi("0")
andatoi("T")
sounds like a design issue. What is the format of the incoming data?
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?
The data has been writeString--ed to a file with a %d format. So I read it back in later and want to make sure someone didnt inadvertently change what should be a numerical entry in the input file, into a alphabetical entry.....so I was going to use atoi to check the readstring-ed CString...... Thanks, ns
-
The data has been writeString--ed to a file with a %d format. So I read it back in later and want to make sure someone didnt inadvertently change what should be a numerical entry in the input file, into a alphabetical entry.....so I was going to use atoi to check the readstring-ed CString...... Thanks, ns
So your file should look like: 123 456 004 991 ... and you want to ensure that someone hasn't changed it to: 07b 1c8 004 3df ... Is that an accurate assesment? If so, you'll need to employ
ReadString()
to read each line from the file and parse the data yourself, something like:CString strLine;
while (...)
{
file.ReadString(strLine);
if (! IsAllNumbers(strLine))
AfxMessageBox(...);
}bool IsAllNumbers( LPCSTR lpszData )
{
while ('\0' != *lpszData)
{
if (isdigit(*lpszData))
lpszData++;
else
return false;
}return true;
}
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?
-
So your file should look like: 123 456 004 991 ... and you want to ensure that someone hasn't changed it to: 07b 1c8 004 3df ... Is that an accurate assesment? If so, you'll need to employ
ReadString()
to read each line from the file and parse the data yourself, something like:CString strLine;
while (...)
{
file.ReadString(strLine);
if (! IsAllNumbers(strLine))
AfxMessageBox(...);
}bool IsAllNumbers( LPCSTR lpszData )
{
while ('\0' != *lpszData)
{
if (isdigit(*lpszData))
lpszData++;
else
return false;
}return true;
}
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?
-
I am reading in CString values from a file and atoi-ing them. Docs say it returns 0 if the string cant be converted to a number (though the example there says atoi("5 cars") returns 5 -- I thought it would return 0 ???) Confused about this. The other thing is that I might have a CString "0". If atoi returns 0 in this case, then its not signalling an error. So would I treat the case of strings like "0" separately? Thanks, ns
I usually use isdigit() to test for such things. Calling a function that carries out an operation that you have no intent on using just to see if it returns an error isn't typically considered good design.
Have you answered an MTQ? Check out the stats!