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. Reading a CSV file

Reading a CSV file

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorial
10 Posts 4 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.
  • A Offline
    A Offline
    Arrun
    wrote on last edited by
    #1

    Hi Every body How to read a comma seperated file in VC++ Thanks in advance

    M K 2 Replies Last reply
    0
    • A Arrun

      Hi Every body How to read a comma seperated file in VC++ Thanks in advance

      M Offline
      M Offline
      Marc Soleda
      wrote on last edited by
      #2

      Use strtok[^] ... she said you are the perfect stranger she said baby let's keep it like this... Tunnel of Love, Dire Straits.

      R 1 Reply Last reply
      0
      • M Marc Soleda

        Use strtok[^] ... she said you are the perfect stranger she said baby let's keep it like this... Tunnel of Love, Dire Straits.

        R Offline
        R Offline
        Roland Pibinger
        wrote on last edited by
        #3

        marcdev wrote: Use strtok[^] Are you sure that strtok reads a file?? :~:confused::laugh:

        M 1 Reply Last reply
        0
        • A Arrun

          Hi Every body How to read a comma seperated file in VC++ Thanks in advance

          K Offline
          K Offline
          knapak
          wrote on last edited by
          #4

          Hi There may be other more elegant ways, but unless you are reading millions of entries, this works just fine. The char variable is read and then ignored, you only store or use the datx as you wish. char separator; ifstream FileIn("C:\\SampleData\\divedata.dat"); FileIn >> dat1 >> separator >> dat2 >> separator >> dat3 >> separator >> dat4; Good luck

          R 1 Reply Last reply
          0
          • K knapak

            Hi There may be other more elegant ways, but unless you are reading millions of entries, this works just fine. The char variable is read and then ignored, you only store or use the datx as you wish. char separator; ifstream FileIn("C:\\SampleData\\divedata.dat"); FileIn >> dat1 >> separator >> dat2 >> separator >> dat3 >> separator >> dat4; Good luck

            R Offline
            R Offline
            Roland Pibinger
            wrote on last edited by
            #5

            knapak wrote: here may be other more elegant ways, but unless you are reading millions of entries, this works just fine. The char variable is read and then ignored, you only store or use the datx as you wish. char separator; ifstream FileIn("C:\\SampleData\\divedata.dat"); FileIn >> dat1 >> separator >> dat2 >> separator >> dat3 >> separator >> dat4; This can hardly work! Good luck Agreed :|

            K 1 Reply Last reply
            0
            • R Roland Pibinger

              knapak wrote: here may be other more elegant ways, but unless you are reading millions of entries, this works just fine. The char variable is read and then ignored, you only store or use the datx as you wish. char separator; ifstream FileIn("C:\\SampleData\\divedata.dat"); FileIn >> dat1 >> separator >> dat2 >> separator >> dat3 >> separator >> dat4; This can hardly work! Good luck Agreed :|

              K Offline
              K Offline
              knapak
              wrote on last edited by
              #6

              It would be more useful if you could explain why this can hardly work and provide examples of the alternatives. I've used it extensively and had no problems at all, but if there's a good reason to do something else we would be very happy to oblige. Otherwise is just empty discourse. Every time you assign a value to a variable, it takes the new value. If the variable is not used at all, given it is a single char variable, virtually no memory is wasted... why wouldn't it work?:wtf:

              R 1 Reply Last reply
              0
              • K knapak

                It would be more useful if you could explain why this can hardly work and provide examples of the alternatives. I've used it extensively and had no problems at all, but if there's a good reason to do something else we would be very happy to oblige. Otherwise is just empty discourse. Every time you assign a value to a variable, it takes the new value. If the variable is not used at all, given it is a single char variable, virtually no memory is wasted... why wouldn't it work?:wtf:

                R Offline
                R Offline
                Roland Pibinger
                wrote on last edited by
                #7

                knapak wrote: It would be more useful if you could explain why this can hardly work Try this line with your code:

                This,is,a comma seperated,line

                K 1 Reply Last reply
                0
                • R Roland Pibinger

                  marcdev wrote: Use strtok[^] Are you sure that strtok reads a file?? :~:confused::laugh:

                  M Offline
                  M Offline
                  Marc Soleda
                  wrote on last edited by
                  #8

                  Obviously not, but the question is not how to read a file ... Maybe you need that I was more specific: 1st. Open the file. 2nd. Read each line putting it in a string. 3rd. Process the string using strtok to separate it by commas. Is strtok useful now ? ;P Marc Soleda. ... she said you are the perfect stranger she said baby let's keep it like this... Tunnel of Love, Dire Straits.

                  1 Reply Last reply
                  0
                  • R Roland Pibinger

                    knapak wrote: It would be more useful if you could explain why this can hardly work Try this line with your code:

                    This,is,a comma seperated,line

                    K Offline
                    K Offline
                    knapak
                    wrote on last edited by
                    #9

                    Fine, if you are mostly writing programs that deal with strings of characters, I can see the problem. However, it seems that were are here talking about reading ONLY numeric data, in which case my approach won't fail and IS SIMPLER. Nevertheless, if you think your solution is more "elegant" why don't you provide a more comprehensive response, something beyond "use this", with clear sample code. Cheers

                    R 1 Reply Last reply
                    0
                    • K knapak

                      Fine, if you are mostly writing programs that deal with strings of characters, I can see the problem. However, it seems that were are here talking about reading ONLY numeric data, in which case my approach won't fail and IS SIMPLER. Nevertheless, if you think your solution is more "elegant" why don't you provide a more comprehensive response, something beyond "use this", with clear sample code. Cheers

                      R Offline
                      R Offline
                      Roland Pibinger
                      wrote on last edited by
                      #10

                      knapak wrote: Nevertheless, if you think your solution is more "elegant" why don't you provide a more comprehensive response, something beyond "use this", with clear sample code. Why not just look at (and re-use) something others have already done, at CodeProject or elsewhere? XTokenString[^] string tokenize[^]

                      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