reference 10222290

when reading in a string from an ini file and converting to integers, how do I handle negative integers
jgreavesAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
nietodConnect With a Mentor Commented:
I know its an ini file.  I was wondering how he could do this and not get signed values.  (Storing the result in an unsigned int is one way--a way that I didn't think of!)    That is why I wanted to know what he was doing to the file.

(Note that At the time that I posted my comment, his explanation of the problem was not visible.)
0
 
jkrCommented:
Hmm, what problems do you encounter? The code should be able to handle negative integers...
0
 
jgreavesAuthor Commented:
you're right, my mistake... the variable in the structure is defined as unsigned16 which is why I was getting strange results.  FYI, modifications from the code you sent me earlier in comments.  thanks for all of your help so far

GetPrivateProfileString(szDrive, "Phase3Vals", NULL, acBuf, MAX_SERCOS_STRING_LEN,  m_strFilename);
pc = strtok ( acBuf, pszDelims);

i = 0;
while ( pc)
{
  // skip the '0x' - the docs say that it's not necessary
  if ( !(pcCnvStart = strchr ( pc, 'x')))
  {
    // found no 'x' in the substring...
    n = (int) strtol ( pc, &pcCnvEnd, 10); // <-- added
  }
  else
  {
    // found an 'x' in the substring, skip it...
    pcCnvStart++;
    n = (int) strtol ( pcCnvStart, &pcCnvEnd, 16); // <-- added
  }
  //n = (int) strtol ( pcCnvStart, &pcCnvEnd, 16); // <--removed

  Drive_IDNs3[i].value = n;
  pc = strtok ( NULL, pszDelims);
  i++;
  }
}
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
nietodCommented:
How are you reading the file?  Most methods I can think of should handle this fine.
0
 
jkrCommented:
Todd, it's an .ini file. The suggested method to read a value containing multiple integers was to read it as a string, break it down with 'strtok()' and converting value by value.
0
 
jkrCommented:
What are you going to do with this Q then?
0
 
nietodCommented:
Well, jkr, I guess, that answers your question.  Of Course it raises the question of why?
0
 
jkrCommented:
I also thought of asking this particular  Q ;-)
0
All Courses

From novice to tech pro — start learning today.