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. File I/O

File I/O

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++data-structuresquestion
13 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.
  • K Offline
    K Offline
    Kuroro Rucilful
    wrote on last edited by
    #1

    I'm an infant when it comes to C++. Can anybody help me to access a text file which has a comma that separates every item and after which put that file into an array which can be access later. The problem is the items in a file are in this manner, each line: char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ ........ ............. ......... ...... upto 57-60 lines Do i have to separate the char and int and long int and put it in different array? Or put the items in a struct....

    Richard Andrew x64R S C 4 Replies Last reply
    0
    • K Kuroro Rucilful

      I'm an infant when it comes to C++. Can anybody help me to access a text file which has a comma that separates every item and after which put that file into an array which can be access later. The problem is the items in a file are in this manner, each line: char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ ........ ............. ......... ...... upto 57-60 lines Do i have to separate the char and int and long int and put it in different array? Or put the items in a struct....

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      The way you have presented it, it looks like the choice is one of personal preference, rather than necessity. How much coding is already done? Do you already have in place the code for reading the file line by line and parsing the data, or not? Rich

      K 1 Reply Last reply
      0
      • K Kuroro Rucilful

        I'm an infant when it comes to C++. Can anybody help me to access a text file which has a comma that separates every item and after which put that file into an array which can be access later. The problem is the items in a file are in this manner, each line: char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ ........ ............. ......... ...... upto 57-60 lines Do i have to separate the char and int and long int and put it in different array? Or put the items in a struct....

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

        What does the dots at the end of each line signify? Steve

        K 1 Reply Last reply
        0
        • K Kuroro Rucilful

          I'm an infant when it comes to C++. Can anybody help me to access a text file which has a comma that separates every item and after which put that file into an array which can be access later. The problem is the items in a file are in this manner, each line: char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ ........ ............. ......... ...... upto 57-60 lines Do i have to separate the char and int and long int and put it in different array? Or put the items in a struct....

          C Offline
          C Offline
          Chris Gao
          wrote on last edited by
          #4

          Hi Kuroro, How about fscanf ? Regards, Chris

          K 1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            The way you have presented it, it looks like the choice is one of personal preference, rather than necessity. How much coding is already done? Do you already have in place the code for reading the file line by line and parsing the data, or not? Rich

            K Offline
            K Offline
            Kuroro Rucilful
            wrote on last edited by
            #5

            Currently I've coded a chunk of code that extracts line by line. I've coded in Visual Basic and successfully extracted each of the input using this code. Open FilePath For Input As #1 Delimiter = "," SegmentCount = 1 ReDim TheSegments(1) Do Until EOF(1) DoEvents If UBound(TheSegments) = SegmentCount Then ReDim Preserve TheSegments(SegmentCount + 10) End If ' Read a line from the vin file Line Input #1, TheString ' Parse the line SubStrings = Split(TheString, Delimiter, -1, vbTextCompare) ' Transfer all data to a Segment '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ' Get the name(description) TheSegments(SegmentCount).Name = SubStrings(0) ' Get the Front Plane TheSegments(SegmentCount).FrontPlane = Val(SubStrings(1)) ' Get the Back Plane TheSegments(SegmentCount).BackPlane = Val(SubStrings(2)) ' Get the Color TheSegments(SegmentCount).Color = Val(SubStrings(3)) ' Calculate the number of pixels PixelCount = UBound(SubStrings) - 4 TheSegments(SegmentCount).PixelCount = PixelCount ' Now get all the pixel positions For Count = 1 To PixelCount TempLong = Val(SubStrings(Count + 3)) 'TheSegments(SegmentCount).Pixels(Count).Val = TempLong TheSegments(SegmentCount).Pixels(Count).x = TempLong Mod 320 TheSegments(SegmentCount).Pixels(Count).y = TempLong \ 320 Next '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& SegmentCount = SegmentCount + 1 ''''''''''''''''''''''''''' Loop Close #1 End Sub In C++ i have no idea how to deal with this... I'm currently into self indulgement with this language at the same time working on Hardware and Software design of my project LCD USB Emulator...

            Richard Andrew x64R 1 Reply Last reply
            0
            • C Chris Gao

              Hi Kuroro, How about fscanf ? Regards, Chris

              K Offline
              K Offline
              Kuroro Rucilful
              wrote on last edited by
              #6

              I'm currently studying string extractions. I can't focus really on software side coz i'm also designing the hardware... Thanks Chris, I'll be trying it....

              1 Reply Last reply
              0
              • K Kuroro Rucilful

                Currently I've coded a chunk of code that extracts line by line. I've coded in Visual Basic and successfully extracted each of the input using this code. Open FilePath For Input As #1 Delimiter = "," SegmentCount = 1 ReDim TheSegments(1) Do Until EOF(1) DoEvents If UBound(TheSegments) = SegmentCount Then ReDim Preserve TheSegments(SegmentCount + 10) End If ' Read a line from the vin file Line Input #1, TheString ' Parse the line SubStrings = Split(TheString, Delimiter, -1, vbTextCompare) ' Transfer all data to a Segment '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ' Get the name(description) TheSegments(SegmentCount).Name = SubStrings(0) ' Get the Front Plane TheSegments(SegmentCount).FrontPlane = Val(SubStrings(1)) ' Get the Back Plane TheSegments(SegmentCount).BackPlane = Val(SubStrings(2)) ' Get the Color TheSegments(SegmentCount).Color = Val(SubStrings(3)) ' Calculate the number of pixels PixelCount = UBound(SubStrings) - 4 TheSegments(SegmentCount).PixelCount = PixelCount ' Now get all the pixel positions For Count = 1 To PixelCount TempLong = Val(SubStrings(Count + 3)) 'TheSegments(SegmentCount).Pixels(Count).Val = TempLong TheSegments(SegmentCount).Pixels(Count).x = TempLong Mod 320 TheSegments(SegmentCount).Pixels(Count).y = TempLong \ 320 Next '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& SegmentCount = SegmentCount + 1 ''''''''''''''''''''''''''' Loop Close #1 End Sub In C++ i have no idea how to deal with this... I'm currently into self indulgement with this language at the same time working on Hardware and Software design of my project LCD USB Emulator...

                Richard Andrew x64R Offline
                Richard Andrew x64R Offline
                Richard Andrew x64
                wrote on last edited by
                #7

                So you want to pass the data to a C++ routine after it has been extracted by the VB program, or you just want to translate all of the VB code into a C++ program?

                K 1 Reply Last reply
                0
                • S Stephen Hewitt

                  What does the dots at the end of each line signify? Steve

                  K Offline
                  K Offline
                  Kuroro Rucilful
                  wrote on last edited by
                  #8

                  Steve, Sorry for the dot stuff, that signifies another input of type long int... actually the first entry is Char, then the second, third and the fourth is of type int, the fifth until end of line is a type of long int. each line have different number of items. I'm not good in explaining things. Thanks Steve

                  J 1 Reply Last reply
                  0
                  • Richard Andrew x64R Richard Andrew x64

                    So you want to pass the data to a C++ routine after it has been extracted by the VB program, or you just want to translate all of the VB code into a C++ program?

                    K Offline
                    K Offline
                    Kuroro Rucilful
                    wrote on last edited by
                    #9

                    I just want to translate the VB code into C++ program. It's a bit difficult on my part. Thanks

                    1 Reply Last reply
                    0
                    • K Kuroro Rucilful

                      I'm an infant when it comes to C++. Can anybody help me to access a text file which has a comma that separates every item and after which put that file into an array which can be access later. The problem is the items in a file are in this manner, each line: char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ char,int,int,int,long int,long int........ ........ ............. ......... ...... upto 57-60 lines Do i have to separate the char and int and long int and put it in different array? Or put the items in a struct....

                      S Offline
                      S Offline
                      Stephen Hewitt
                      wrote on last edited by
                      #10
                      char char_val;
                      int int_val1, int_val2, int_val3;
                      long int longint_val1;
                       
                      char comma;
                       
                      ifstream fs("C:\\a.txt");
                      if ( !(fs >> char_val >> comma) || comma!=',')
                      {
                           // Bail....
                      }
                      if ( !fs >> int_val1 >> comma) || comma!=',')
                      {
                           // Bail....
                      }
                      // etc...
                      

                      Steve

                      K 2 Replies Last reply
                      0
                      • S Stephen Hewitt
                        char char_val;
                        int int_val1, int_val2, int_val3;
                        long int longint_val1;
                         
                        char comma;
                         
                        ifstream fs("C:\\a.txt");
                        if ( !(fs >> char_val >> comma) || comma!=',')
                        {
                             // Bail....
                        }
                        if ( !fs >> int_val1 >> comma) || comma!=',')
                        {
                             // Bail....
                        }
                        // etc...
                        

                        Steve

                        K Offline
                        K Offline
                        Kuroro Rucilful
                        wrote on last edited by
                        #11

                        Thanks a lot Steve, I'm currently coding it in my C++ BuilderX compiler. Thanks........

                        1 Reply Last reply
                        0
                        • S Stephen Hewitt
                          char char_val;
                          int int_val1, int_val2, int_val3;
                          long int longint_val1;
                           
                          char comma;
                           
                          ifstream fs("C:\\a.txt");
                          if ( !(fs >> char_val >> comma) || comma!=',')
                          {
                               // Bail....
                          }
                          if ( !fs >> int_val1 >> comma) || comma!=',')
                          {
                               // Bail....
                          }
                          // etc...
                          

                          Steve

                          K Offline
                          K Offline
                          Kuroro Rucilful
                          wrote on last edited by
                          #12

                          Steve, Actual items in the file M637.VIN M2,2,1,15,55342,55662,55982,56302 A,4,1,15,58856,59176,59496,59816 P,5,1,15,55327,55647,55967 B,7,1,15,55310,55630,55950,56270 4B,8,2,15,27769,25530 General format: char_val,int_val1,int_val2,int_val3,longint_val1 is it possible to store these stuff in a struct or an array of some sort? Coz i need int_val1 and int_val2 as a reference to an output port. in line 1 "M2,2,1" , i need to access the extracted input 2 and 1 coz it corresponds to an output port2 and port1. on the other hand, char_val must be taken into account coz it serves as a reference to longint_val(n). Thanks, Kuroro

                          1 Reply Last reply
                          0
                          • K Kuroro Rucilful

                            Steve, Sorry for the dot stuff, that signifies another input of type long int... actually the first entry is Char, then the second, third and the fourth is of type int, the fifth until end of line is a type of long int. each line have different number of items. I'm not good in explaining things. Thanks Steve

                            J Offline
                            J Offline
                            jhwurmbach
                            wrote on last edited by
                            #13

                            To me, each line looks like a struct constisting of a std::string, 3 ints and a std::vector of longints. The constructor of this struct can take a line of the file, parse it and fill the fields. getline would be the method to - erm - get the line from the file. For parsing, you could use fscanf or look at e.g. www.boost.org[^]for a parsing library.


                            "We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.

                            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