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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. strtol problem

strtol problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
3 Posts 2 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
    nss
    wrote on last edited by
    #1

    I am using strtol to catch bad entries: looping on i { fgets(line, MAX_SIZE -1, fp); pc = line; for ( unsigned int j = 0; j < cols ; ++j, ++col) { num = strtol( pc, &pc, 10); cout << i << " " << j << " " <<num<< " pc " << (*pc==' '?'.':*pc) << endl; line comes from a file that looks like: 9 a5 3 9 1 3 9 5 5 7 10 9 4 6 10 10 8 5 1 7 10 12 1 3 10 10 10 11 12 3 /// an 'a' snuck in...strtol should arrest it, but it never does. (however with a9 5 3 9 3 9 3 8 5 3, I see the letter 'a' in the cout.....) Here is the output for the first case (when a doesnt show...) 0 0 9 pc . 0 1 0 pc .//pc should have been 'a', num is 0 is okay since it didnt get read 0 2 0 pc .//'num' should have been 5 0 3 0 pc . 0 4 0 pc . 0 5 0 pc . 0 6 0 pc . 0 7 0 pc . 0 8 0 pc . 0 9 0 pc . Any ideas why strtol isnt performing as I understand it should? thanks, ns

    D 1 Reply Last reply
    0
    • N nss

      I am using strtol to catch bad entries: looping on i { fgets(line, MAX_SIZE -1, fp); pc = line; for ( unsigned int j = 0; j < cols ; ++j, ++col) { num = strtol( pc, &pc, 10); cout << i << " " << j << " " <<num<< " pc " << (*pc==' '?'.':*pc) << endl; line comes from a file that looks like: 9 a5 3 9 1 3 9 5 5 7 10 9 4 6 10 10 8 5 1 7 10 12 1 3 10 10 10 11 12 3 /// an 'a' snuck in...strtol should arrest it, but it never does. (however with a9 5 3 9 3 9 3 8 5 3, I see the letter 'a' in the cout.....) Here is the output for the first case (when a doesnt show...) 0 0 9 pc . 0 1 0 pc .//pc should have been 'a', num is 0 is okay since it didnt get read 0 2 0 pc .//'num' should have been 5 0 3 0 pc . 0 4 0 pc . 0 5 0 pc . 0 6 0 pc . 0 7 0 pc . 0 8 0 pc . 0 9 0 pc . Any ideas why strtol isnt performing as I understand it should? thanks, ns

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

      nss wrote: Any ideas why strtol isnt performing as I understand it should? What are you expecting it to do? When calling strtol("9 a5 3 9 1 3 9 5 5 7", ...), processing stops at the space following the first 9 because that is the first character that it did not recognize as part of the number. The same is true for calling strtol("a9 5 3 9 3 9 3 8 5 3", ...), in that processing stops at the 'a' because that is the first character that it did not recognize as part of the number. Had you used base 16 instead, processing would have stopped at the space following 'a9'. Make sense?


      "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

      N 1 Reply Last reply
      0
      • D David Crow

        nss wrote: Any ideas why strtol isnt performing as I understand it should? What are you expecting it to do? When calling strtol("9 a5 3 9 1 3 9 5 5 7", ...), processing stops at the space following the first 9 because that is the first character that it did not recognize as part of the number. The same is true for calling strtol("a9 5 3 9 3 9 3 8 5 3", ...), in that processing stops at the 'a' because that is the first character that it did not recognize as part of the number. Had you used base 16 instead, processing would have stopped at the space following 'a9'. Make sense?


        "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

        N Offline
        N Offline
        nss
        wrote on last edited by
        #3

        right, the first arrest is at ' ', but right afterit should choke on 'a' as well...... 23 12 a4 56..... It never prints out *pc = 'a'

        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