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. Read tab delimited file

Read tab delimited file

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
12 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.
  • K kelprinc

    Does anybody know how to read a tab delimited file at the same time using the '\n' as a delimiter if its encountered first. I tried using getline(buffer, num_chars, '\t') It works fine when i know the number of columns. Say i had a file like line1:Col1 Col2 Col3 line2:Col1 Col2 line3:Col1 Col2 Col3 using getline(buffer, num_chars, '\t') would return Col1 Col2 Col3 Col1 Col2 Col1 Col2 Col3 everything in one line. I tried defining a macro like #define tab_eol '\t'|'\n' then use it as getline(buffer,num_chars,tab_eol) but it does not really work Any help Please Kelvin Chikomo

    T Offline
    T Offline
    toxcct
    wrote on last edited by
    #2

    kelprinc wrote:

    #define tab_eol '\t'|'\n'

    perfectly useless buddy... consider this to understand why :

    '\t' | '\n' = 0x09 | 0x10
    = 00001001 | 00010000
    = 00011001
    = 0x19
    = '\031'
    = DC3 character...


    TOXCCT >>> GEII power
    [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

    K 1 Reply Last reply
    0
    • K kelprinc

      Does anybody know how to read a tab delimited file at the same time using the '\n' as a delimiter if its encountered first. I tried using getline(buffer, num_chars, '\t') It works fine when i know the number of columns. Say i had a file like line1:Col1 Col2 Col3 line2:Col1 Col2 line3:Col1 Col2 Col3 using getline(buffer, num_chars, '\t') would return Col1 Col2 Col3 Col1 Col2 Col1 Col2 Col3 everything in one line. I tried defining a macro like #define tab_eol '\t'|'\n' then use it as getline(buffer,num_chars,tab_eol) but it does not really work Any help Please Kelvin Chikomo

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

      Does strtok() help?


      "The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

      K 1 Reply Last reply
      0
      • K kelprinc

        Does anybody know how to read a tab delimited file at the same time using the '\n' as a delimiter if its encountered first. I tried using getline(buffer, num_chars, '\t') It works fine when i know the number of columns. Say i had a file like line1:Col1 Col2 Col3 line2:Col1 Col2 line3:Col1 Col2 Col3 using getline(buffer, num_chars, '\t') would return Col1 Col2 Col3 Col1 Col2 Col1 Col2 Col3 everything in one line. I tried defining a macro like #define tab_eol '\t'|'\n' then use it as getline(buffer,num_chars,tab_eol) but it does not really work Any help Please Kelvin Chikomo

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #4

        Are the entries (e.g. "Col1") allowed spaces in them? If not this will do won't it?

        #include <iostream>
        #include <string>
        using namespace std;
        
        // Somewhere...
        string entry;
        while ( cin >> entry )
        {
             cout << entry << endl;
        }
        

        Steve

        K 1 Reply Last reply
        0
        • S Stephen Hewitt

          Are the entries (e.g. "Col1") allowed spaces in them? If not this will do won't it?

          #include <iostream>
          #include <string>
          using namespace std;
          
          // Somewhere...
          string entry;
          while ( cin >> entry )
          {
               cout << entry << endl;
          }
          

          Steve

          K Offline
          K Offline
          kelprinc
          wrote on last edited by
          #5

          Yah they have spaces between them

          S 1 Reply Last reply
          0
          • T toxcct

            kelprinc wrote:

            #define tab_eol '\t'|'\n'

            perfectly useless buddy... consider this to understand why :

            '\t' | '\n' = 0x09 | 0x10
            = 00001001 | 00010000
            = 00011001
            = 0x19
            = '\031'
            = DC3 character...


            TOXCCT >>> GEII power
            [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

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

            DO you have any suggestions

            1 Reply Last reply
            0
            • K kelprinc

              Yah they have spaces between them

              S Offline
              S Offline
              Stephen Hewitt
              wrote on last edited by
              #7

              If thay have spaces between them then the code I have should work. If they have spaces in them it will not. Steve

              1 Reply Last reply
              0
              • K kelprinc

                Does anybody know how to read a tab delimited file at the same time using the '\n' as a delimiter if its encountered first. I tried using getline(buffer, num_chars, '\t') It works fine when i know the number of columns. Say i had a file like line1:Col1 Col2 Col3 line2:Col1 Col2 line3:Col1 Col2 Col3 using getline(buffer, num_chars, '\t') would return Col1 Col2 Col3 Col1 Col2 Col1 Col2 Col3 everything in one line. I tried defining a macro like #define tab_eol '\t'|'\n' then use it as getline(buffer,num_chars,tab_eol) but it does not really work Any help Please Kelvin Chikomo

                K Offline
                K Offline
                kelprinc
                wrote on last edited by
                #8

                I got it I read each line into an an istringstream object and i use getline(buffer,num_chars,'\t') to seperate the the string. COOL Kelvin Chikomo

                1 Reply Last reply
                0
                • D David Crow

                  Does strtok() help?


                  "The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

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

                  Thanks but strtok() does not work well if you have empty columns. i.e concurrent tabs

                  D 1 Reply Last reply
                  0
                  • K kelprinc

                    Thanks but strtok() does not work well if you have empty columns. i.e concurrent tabs

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

                    True. You could always make your own version of strtok() that does not skip leading delimeters.


                    "The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

                    K 1 Reply Last reply
                    0
                    • D David Crow

                      True. You could always make your own version of strtok() that does not skip leading delimeters.


                      "The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

                      K Offline
                      K Offline
                      kelprinc
                      wrote on last edited by
                      #11

                      I managed to sort out the problem by reading the whole line into a istringstream object then tokenize the object using the getline method and '\t' as the delimiter. But for interest sake how wld you do it? Kelvin Chikomo

                      D 1 Reply Last reply
                      0
                      • K kelprinc

                        I managed to sort out the problem by reading the whole line into a istringstream object then tokenize the object using the getline method and '\t' as the delimiter. But for interest sake how wld you do it? Kelvin Chikomo

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

                        kelprinc wrote:

                        But for interest sake how wld you do it?

                        I guess that would depend on several factors. Am I limited to just C++ code? Is it a console or GUI application? What is my state of mind at that moment? Glad you got it going.


                        "The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

                        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