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

Querry!!

Scheduled Pinned Locked Moved C / C++ / MFC
question
12 Posts 7 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.
  • R Razanust

    I have names of certain students and their ages stored in a text file 'records.txt'. LIKE: John 22 Paul 21 Sam 24 Harry 23 Now i want to store these ages and names in a structure 'records(r)' directly from the file. (Here i am not taking any input from the user) as r.name & r.age. Any clue to the solution???

    C Offline
    C Offline
    Chandrasekharan P
    wrote on last edited by
    #2

    I believe this query was answered

    1 Reply Last reply
    0
    • R Razanust

      I have names of certain students and their ages stored in a text file 'records.txt'. LIKE: John 22 Paul 21 Sam 24 Harry 23 Now i want to store these ages and names in a structure 'records(r)' directly from the file. (Here i am not taking any input from the user) as r.name & r.age. Any clue to the solution???

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

      I couldn't get that. Plesae elaborate the clue so that i can learn a bit more. I am trying to use the "isdigit" command for this purpose. Would that be suitable?

      H 1 Reply Last reply
      0
      • R Razanust

        I couldn't get that. Plesae elaborate the clue so that i can learn a bit more. I am trying to use the "isdigit" command for this purpose. Would that be suitable?

        H Offline
        H Offline
        Harsh Shankar
        wrote on last edited by
        #4

        Dear Razanust, I'll first suggess you not to create a new thread if you aren't satisfied with the answer.

        Razanust wrote:

        couldn't get that. Plesae elaborate the clue so that i can learn a bit more. I am trying to use the "isdigit" command for this purpose. Would that be suitable?

        Now, as your file is a text file, :confused: First Clear whether you have stored it by formating by sprintf and then WriteFile etc or Writing the structure directly? HARSH Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning.

        modified on Tuesday, June 30, 2009 3:59 AM

        1 Reply Last reply
        0
        • R Razanust

          I have names of certain students and their ages stored in a text file 'records.txt'. LIKE: John 22 Paul 21 Sam 24 Harry 23 Now i want to store these ages and names in a structure 'records(r)' directly from the file. (Here i am not taking any input from the user) as r.name & r.age. Any clue to the solution???

          M Offline
          M Offline
          Michael Schubert
          wrote on last edited by
          #5

          Razanust wrote:

          Any clue to the solution???

          1. Read the file line by line 2. use something like strtok to tokenize each line 3. fill the structure

          H 1 Reply Last reply
          0
          • R Razanust

            I have names of certain students and their ages stored in a text file 'records.txt'. LIKE: John 22 Paul 21 Sam 24 Harry 23 Now i want to store these ages and names in a structure 'records(r)' directly from the file. (Here i am not taking any input from the user) as r.name & r.age. Any clue to the solution???

            R Offline
            R Offline
            Rajesh R Subramanian
            wrote on last edited by
            #6

            You need to open the file, read the contents, parse the read contents, store them into an array of structures. Which part of this exercise are you having a problem with? And what have you done so far?

            It is a crappy thing, but it's life -^ Carlo Pallini

            1 Reply Last reply
            0
            • M Michael Schubert

              Razanust wrote:

              Any clue to the solution???

              1. Read the file line by line 2. use something like strtok to tokenize each line 3. fill the structure

              H Offline
              H Offline
              Harsh Shankar
              wrote on last edited by
              #7

              I Don't know why u are trying there tipical methods. Just Try the "C" way: -

              struct MyStruct
              {
              	char Name\[20\];
              	int age;
              }
              // Fill the Structure with the appropriate values
              MyStruct myStruct;
              FILE \*pFile = fopen("Record.txt", "Wb+");
              fwrite((char\*)&myStruct, sizeof(MyStruct), 1, pFile);
              

              and to reead :-

              fread((char*)&myStruct, sizeof(MyStruct), 1, pFile);

              HARSH Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning.

              M 1 Reply Last reply
              0
              • H Harsh Shankar

                I Don't know why u are trying there tipical methods. Just Try the "C" way: -

                struct MyStruct
                {
                	char Name\[20\];
                	int age;
                }
                // Fill the Structure with the appropriate values
                MyStruct myStruct;
                FILE \*pFile = fopen("Record.txt", "Wb+");
                fwrite((char\*)&myStruct, sizeof(MyStruct), 1, pFile);
                

                and to reead :-

                fread((char*)&myStruct, sizeof(MyStruct), 1, pFile);

                HARSH Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning.

                M Offline
                M Offline
                Michael Schubert
                wrote on last edited by
                #8

                Harsh Shankar wrote:

                I Don't know why u are trying there tipical methods.

                What?

                H 1 Reply Last reply
                0
                • M Michael Schubert

                  Harsh Shankar wrote:

                  I Don't know why u are trying there tipical methods.

                  What?

                  H Offline
                  H Offline
                  Harsh Shankar
                  wrote on last edited by
                  #9

                  Wouldn't it be easier to read and write the data in the file by my given way??

                  HARSH Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning.

                  modified on Tuesday, June 30, 2009 5:25 AM

                  M 1 Reply Last reply
                  0
                  • R Razanust

                    I have names of certain students and their ages stored in a text file 'records.txt'. LIKE: John 22 Paul 21 Sam 24 Harry 23 Now i want to store these ages and names in a structure 'records(r)' directly from the file. (Here i am not taking any input from the user) as r.name & r.age. Any clue to the solution???

                    A Offline
                    A Offline
                    Aric Wang
                    wrote on last edited by
                    #10

                    supposing your use C language.may be this is a simply.You could use fscanf() function to realize it.You may find out the method of "fscanf()".

                    Like C++ more

                    1 Reply Last reply
                    0
                    • H Harsh Shankar

                      Wouldn't it be easier to read and write the data in the file by my given way??

                      HARSH Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning.

                      modified on Tuesday, June 30, 2009 5:25 AM

                      M Offline
                      M Offline
                      Michael Schubert
                      wrote on last edited by
                      #11

                      I don't see anything in his post about writing the data back to a file. He only wants to read the file and store the tokens in a structure (or presumably an array of structures).

                      1 Reply Last reply
                      0
                      • R Razanust

                        I have names of certain students and their ages stored in a text file 'records.txt'. LIKE: John 22 Paul 21 Sam 24 Harry 23 Now i want to store these ages and names in a structure 'records(r)' directly from the file. (Here i am not taking any input from the user) as r.name & r.age. Any clue to the solution???

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

                        Razanust wrote:

                        Any clue to the solution???

                        Well, something like this comes to mind:

                        std::ifstream fin;
                        std::vector<Record> records;

                        fin.open("c:\\Records.txt");
                        std::copy(std::istream_iterator<Record>(fin), std::istream_iterator<Record>(), std::back_inserter(records));
                        fin.close();

                        Hint: you'll need to create the Record class. ;)

                        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                        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