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 a certain line from a text file

read a certain line from a text file

Scheduled Pinned Locked Moved C / C++ / MFC
iosperformancehelptutorialquestion
15 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 avenger_sb25

    First of all tell me one thing: do you need answers in terms of the "<<" operator or you just want an elegent solution? There is much better and elegent solution if you dont use "<<" technique.:-D Do you want the alternate technique or some modifications using "<<":confused:? Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs :)

    D Offline
    D Offline
    dairiseky
    wrote on last edited by
    #6

    i used here "<< " just to visualise things better, but if u have a better idea say it to me please :) THX

    A 1 Reply Last reply
    0
    • D David Crow

      You could if each line were the same length, or each xpos/ypos/speed group was the same size. The seekg() method is used to position the file pointer for the next read operation.


      "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

      D Offline
      D Offline
      dairiseky
      wrote on last edited by
      #7

      First thx for friday. Then what do u mean by the same length? Is space or tab considered as getting in the value's lenth? Or just by adding zeros at the end? I looked in a tutorial for the skeeg stuff and didn't understand well how it works. Could u breif me about this, please? THX

      D 1 Reply Last reply
      0
      • D dairiseky

        i used here "<< " just to visualise things better, but if u have a better idea say it to me please :) THX

        A Offline
        A Offline
        avenger_sb25
        wrote on last edited by
        #8

        See, the best way (that i feel) of reading and writing srtuctured data to a file is using the following functions: istream::read ostream::write Function prototypes are: read( const unsigned char* ptr, int n) Description: The read function reads n characters starting form the location pointed to by ptr. In other words, read function reads n bytes from the address pointed to by ptr. The write has similar prototype and similar description (but used to write data) The above functions work best for reading/writing classes and structures from/to files. **EXAMPLE:**I include all your data in a class, say "A" class A { double x; double y; double speed; double z; int something1; char something2; }; Then i write all these this to a file using the code: main() { A objectA; //take input from the user cin>>objectA.x; cin>>objectA.y; cin>>objectA.z; //and so on ofstream outfile ("c:\\test.txt", ios::binary); outfile.write ((char*)objectA, sizeof(objectA)); _//this line writes the complete objectA(field by field) into the file test.txt. Remember, the file contents are now binary. So u may see many strange character when you open test.txt in a notepad._ } Once the object has been written into a file, you can read it anytime using using the code: main() { A objectB; ifstream infile ("c:\\test.txt", ios::binary); infile.read ((char*)objectB, sizeof(objectB)); _//after reading from the file objectB now contains the complete info of one class._ //DISPLAY the read results cout< YOU CAN CALL WRITE AGAIN AND AGAIN TO APPEND MORE TAHN ONE RECORDS INTO THE FILE SIMILAR IS THE READ FUNCTION. WHEN U CALL READ AGAIN, IT WILL READ THE NEXT OBJECT WRITTEN INTO THE FILE. All this may appear a bit fuzzy and complicated to you, BUT TRUST ME, this is the best way to handle structured data in files in C++. Ask if u have any doubts;) * * * Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs

        D 1 Reply Last reply
        0
        • D dairiseky

          First thx for friday. Then what do u mean by the same length? Is space or tab considered as getting in the value's lenth? Or just by adding zeros at the end? I looked in a tutorial for the skeeg stuff and didn't understand well how it works. Could u breif me about this, please? THX

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

          dairiseky wrote: Is space or tab considered as getting in the value's lenth? Yes, just because the character is not printable does not make it any less of a character. The seekg() method goes to a certain position within the file. It knows nothing of line-length. So if you had a two-line file, with the first line being 5 characters and the second line being 15 characters, seekg(6) would go to the start of the second line, but if the length of the first line ever changed, it wouldn't. Understand?


          "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

          A D 3 Replies Last reply
          0
          • D David Crow

            dairiseky wrote: Is space or tab considered as getting in the value's lenth? Yes, just because the character is not printable does not make it any less of a character. The seekg() method goes to a certain position within the file. It knows nothing of line-length. So if you had a two-line file, with the first line being 5 characters and the second line being 15 characters, seekg(6) would go to the start of the second line, but if the length of the first line ever changed, it wouldn't. Understand?


            "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

            A Offline
            A Offline
            Anonymous
            wrote on last edited by
            #10

            OK thx. Got the trick ! sorry for the deformation of skeeg :) I was in a rush to get the train.

            1 Reply Last reply
            0
            • D David Crow

              dairiseky wrote: Is space or tab considered as getting in the value's lenth? Yes, just because the character is not printable does not make it any less of a character. The seekg() method goes to a certain position within the file. It knows nothing of line-length. So if you had a two-line file, with the first line being 5 characters and the second line being 15 characters, seekg(6) would go to the start of the second line, but if the length of the first line ever changed, it wouldn't. Understand?


              "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

              D Offline
              D Offline
              dairiseky
              wrote on last edited by
              #11

              OK thx. Got the trick ! sorry for the deformation of skeeg :) I was in a rush to get the train.

              1 Reply Last reply
              0
              • A avenger_sb25

                See, the best way (that i feel) of reading and writing srtuctured data to a file is using the following functions: istream::read ostream::write Function prototypes are: read( const unsigned char* ptr, int n) Description: The read function reads n characters starting form the location pointed to by ptr. In other words, read function reads n bytes from the address pointed to by ptr. The write has similar prototype and similar description (but used to write data) The above functions work best for reading/writing classes and structures from/to files. **EXAMPLE:**I include all your data in a class, say "A" class A { double x; double y; double speed; double z; int something1; char something2; }; Then i write all these this to a file using the code: main() { A objectA; //take input from the user cin>>objectA.x; cin>>objectA.y; cin>>objectA.z; //and so on ofstream outfile ("c:\\test.txt", ios::binary); outfile.write ((char*)objectA, sizeof(objectA)); _//this line writes the complete objectA(field by field) into the file test.txt. Remember, the file contents are now binary. So u may see many strange character when you open test.txt in a notepad._ } Once the object has been written into a file, you can read it anytime using using the code: main() { A objectB; ifstream infile ("c:\\test.txt", ios::binary); infile.read ((char*)objectB, sizeof(objectB)); _//after reading from the file objectB now contains the complete info of one class._ //DISPLAY the read results cout< YOU CAN CALL WRITE AGAIN AND AGAIN TO APPEND MORE TAHN ONE RECORDS INTO THE FILE SIMILAR IS THE READ FUNCTION. WHEN U CALL READ AGAIN, IT WILL READ THE NEXT OBJECT WRITTEN INTO THE FILE. All this may appear a bit fuzzy and complicated to you, BUT TRUST ME, this is the best way to handle structured data in files in C++. Ask if u have any doubts;) * * * Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs

                D Offline
                D Offline
                dairiseky
                wrote on last edited by
                #12

                Ok I like this idea. I will try to use it. Could i, in case i want more details contact u on ur private mail? THX

                A 1 Reply Last reply
                0
                • D dairiseky

                  Ok I like this idea. I will try to use it. Could i, in case i want more details contact u on ur private mail? THX

                  A Offline
                  A Offline
                  avenger_sb25
                  wrote on last edited by
                  #13

                  If u follow the above mentioned technique, u can display any field, any time. Also u dont have to mess with the seek stuff. Thats very unreliable. You can personally contact me at avenger_sb25@yahoo.com or at avenger_sb25@hotmail.com I daily check both these mails. If u want, u can add me to your Yahoo Messenger or MSN Messenger ...cya


                  Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs

                  1 Reply Last reply
                  0
                  • D David Crow

                    dairiseky wrote: Is space or tab considered as getting in the value's lenth? Yes, just because the character is not printable does not make it any less of a character. The seekg() method goes to a certain position within the file. It knows nothing of line-length. So if you had a two-line file, with the first line being 5 characters and the second line being 15 characters, seekg(6) would go to the start of the second line, but if the length of the first line ever changed, it wouldn't. Understand?


                    "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                    D Offline
                    D Offline
                    dairiseky
                    wrote on last edited by
                    #14

                    Is it possible to get the size of datas to be written to file and then use this (their size), to be able to retrieve the right data? THX

                    D 1 Reply Last reply
                    0
                    • D dairiseky

                      Is it possible to get the size of datas to be written to file and then use this (their size), to be able to retrieve the right data? THX

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

                      Yes. Wasn't that answered here?


                      "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                      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