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. Managed C++/CLI
  4. File I/O

File I/O

Scheduled Pinned Locked Moved Managed C++/CLI
helpc++data-structuresquestion
8 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 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....

    S R 2 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....

      S Offline
      S Offline
      Saksida Bojan
      wrote on last edited by
      #2

      you can have struct array or array in struct.

      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....

        R Offline
        R Offline
        RichardS
        wrote on last edited by
        #3

        Kuroro Rucilful wrote:

        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....

        This all depends on what you need them afterwards. If the char,int,long int,etc are all related then I would use a struct to group the info. If they have no relation, then I would use different array's.

        Kuroro Rucilful wrote:

        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.

        Check out the fstream library. It has a function whereby you can read the file a line at a time (.getline I think). There are also C-runtime mechanisms to do a complete file read (using fread). regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

        2 K 2 Replies Last reply
        0
        • R RichardS

          Kuroro Rucilful wrote:

          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....

          This all depends on what you need them afterwards. If the char,int,long int,etc are all related then I would use a struct to group the info. If they have no relation, then I would use different array's.

          Kuroro Rucilful wrote:

          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.

          Check out the fstream library. It has a function whereby you can read the file a line at a time (.getline I think). There are also C-runtime mechanisms to do a complete file read (using fread). regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

          2 Offline
          2 Offline
          2bee
          wrote on last edited by
          #4

          Or if he's using C++/CLI or managed c++, then he should risk a look into the System::IO namespace. for instance: File::ReadAllLines(); // only .Net 2.0 or StreamReader, + Binary- and Textreader... etc. Tobias

          K 1 Reply Last reply
          0
          • S Saksida Bojan

            you can have struct array or array in struct.

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

            Thanks a lot Saksida, i'll use struct array. you give me the hint that uncovers a mile area. i'm currently studying how to do that.... at this website.. http://wps.aw.com/aw\_savitch\_abscpp\_2s/0,10408,1982372-,00.html there are few problems: this is the overview of my project, i mean a chunk of my project.. 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 How am I suppose to say this….. char_val = string or character int_val1 = integer int_val2 = integer int_val3 = integer longint_val(n) = an array of long integers 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,15, 55342,55662,55982,56302" , 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).

            2 1 Reply Last reply
            0
            • 2 2bee

              Or if he's using C++/CLI or managed c++, then he should risk a look into the System::IO namespace. for instance: File::ReadAllLines(); // only .Net 2.0 or StreamReader, + Binary- and Textreader... etc. Tobias

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

              Thanks Tobias, I'm using C++ BuilderX, a freeware. i've got C++ Builder 5. more like Visual C++. Actually i'm not a great fan of high level languages and object oriented stuff but i'm force to used C++... Huhuhuhuh... but i find it great. it's nice to explore things beside assembler stuff. Thanks bro....

              1 Reply Last reply
              0
              • R RichardS

                Kuroro Rucilful wrote:

                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....

                This all depends on what you need them afterwards. If the char,int,long int,etc are all related then I would use a struct to group the info. If they have no relation, then I would use different array's.

                Kuroro Rucilful wrote:

                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.

                Check out the fstream library. It has a function whereby you can read the file a line at a time (.getline I think). There are also C-runtime mechanisms to do a complete file read (using fread). regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

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

                Thanks bro... i'm currently studying it. it's somewhat difficult for me learning C++ at the same time designing the Hardware and Software side of a USB interface.... Huhuhuhuhuh.... thanks for the support... i feel i'm not alone now in this square figure called cubicle.. Thanks, Kuroro Rucilful

                1 Reply Last reply
                0
                • K Kuroro Rucilful

                  Thanks a lot Saksida, i'll use struct array. you give me the hint that uncovers a mile area. i'm currently studying how to do that.... at this website.. http://wps.aw.com/aw\_savitch\_abscpp\_2s/0,10408,1982372-,00.html there are few problems: this is the overview of my project, i mean a chunk of my project.. 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 How am I suppose to say this….. char_val = string or character int_val1 = integer int_val2 = integer int_val3 = integer longint_val(n) = an array of long integers 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,15, 55342,55662,55982,56302" , 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).

                  2 Offline
                  2 Offline
                  2bee
                  wrote on last edited by
                  #8

                  >>is it possible to store these stuff in a struct or an array of some sort? just define your own structtype like the one below or use an odinary struct. typedef struct { char* charval; int intval1; int intval2; int intval3; long* longintarray; // if you know the length of your array at compile time then you could use // long longintarray[n]; instead. }tItem; Read your data line by line from your file and create in a loop an instance of tItem for each line. Fill it with data and add it to your array. An implementation could look like this : Please note that this is no valid code, it's just a little PSEUDOCODE representation. define an array of tItem while(!= end of file) { buffer = readoneline tItem tmpStruct; tmpStruct.charval = new char ( lenght of your read char(s)) tmpStruct.charval = copy the data from your buffer to the location where the pointer is pointing to // strcpy for instance tmpStruct.intval1 = atoi(pointer to your buffer) tmpStruct.intval2 = atoi(pointer to your buffer) tmpStruct.intval3 = atoi(pointer to your buffer) evaluate count of long int in the current line tmpStruct.longintarray = new long (count of long int's); pointertoyour buffer = a pointer on the first long; for( int i = 0; i < count of long values; i++) { tmpStruct.longintarray = atol(pointertoyourbuffer) tmpStruct.longintarray++; pointertoyourbuffer++; } add the tmpStruct to your array of structs } have fun Tobias

                  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