Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. atoi and cstring

atoi and cstring

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    ns
    wrote on last edited by
    #1

    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

    D P T 3 Replies Last reply
    0
    • N 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

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      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 of atoi("0") and atoi("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?

      N 1 Reply Last reply
      0
      • N 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

        P Offline
        P Offline
        Peak
        wrote on last edited by
        #3

        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 }

        A N 2 Replies Last reply
        0
        • P Peak

          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 }

          A Offline
          A Offline
          Adam Gritt
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • P Peak

            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 }

            N Offline
            N Offline
            ns
            wrote on last edited by
            #5

            Appreciate the explanation and the nifty way to handle it. Many thanks, ns

            1 Reply Last reply
            0
            • D David Crow

              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 of atoi("0") and atoi("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?

              N Offline
              N Offline
              ns
              wrote on last edited by
              #6

              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

              D 1 Reply Last reply
              0
              • N 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

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                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?

                N 1 Reply Last reply
                0
                • D David Crow

                  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?

                  N Offline
                  N Offline
                  ns
                  wrote on last edited by
                  #8

                  Thank you so much! Its exactly what the scenario is and your resolution works really well! I appreciate having this snippet of code to "import" into my project. :) ns

                  1 Reply Last reply
                  0
                  • N 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

                    T Offline
                    T Offline
                    Terry ONolley
                    wrote on last edited by
                    #9

                    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!


                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups