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. avenger_sb25//read, write data files

avenger_sb25//read, write data files

Scheduled Pinned Locked Moved C / C++ / MFC
question
11 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.
  • D dairiseky

    Hi. Everything is nice about what u said to me for reading and writing to files. Except one or two things. :( Well first of all it cannot convert from objectA to char*. That's why I didn't use this method before, because I couldn't handle this stuff. Anyway why do u have to use binary files?

    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.

    THX

    S Offline
    S Offline
    Steve S
    wrote on last edited by
    #2

    Ah. You've been told how to without any understanding of what's happening, which makes it difficult to fix this minor problem. The line outfile.write( (char*) objectA, sizeof(objectA)); should probably read outfile.write( (char*)&objectA, sizeof(objectA)); as you need to specify the address. Essentially, it's writing everything from memory to do with objectA's storage out to disk. This is not necessarily a good thing. If objectA contains any pointers, or has other objects embedded within it, you may well be able to read objectA back some time later, but then not be able to use it. Serialising an object is something that the object itself should know how to do, since it is possibly the only entity aware of all it's members. A blind write (and read) of the space occupied does not take into account the fact that the object may refer to other things, such as a char* member. In that case, the address in the char* member would be written out, and not the string it's pointing to1. When the object is later read back, the same address is assigned. Now if the memory map has changed significantly, it's possible that the address is not only not pointing to the right data, but it may in fact be pointing to unallocated or deallocated memory, which will cause an exception in your app. Steve S 1Yes, I know it's not a proper string, it's only an illustration, so calm down... :)

    A 1 Reply Last reply
    0
    • D dairiseky

      Hi. Everything is nice about what u said to me for reading and writing to files. Except one or two things. :( Well first of all it cannot convert from objectA to char*. That's why I didn't use this method before, because I couldn't handle this stuff. Anyway why do u have to use binary files?

      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.

      THX

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

      dairiseky wrote: Anyway why do u have to use binary files? See if these help: http://www.felgall.com/cplus4.htm http://www.techtv.com/screensavers/answerstips/story/0,24330,3373133,00.html http://forums.devshed.com/archive/t-105787


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

      D 1 Reply Last reply
      0
      • D David Crow

        dairiseky wrote: Anyway why do u have to use binary files? See if these help: http://www.felgall.com/cplus4.htm http://www.techtv.com/screensavers/answerstips/story/0,24330,3373133,00.html http://forums.devshed.com/archive/t-105787


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

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

        ok got y. thx again. Though u didn't say to me what u meant by the lenght of a line. Does space and zero count in this lenght? i'm talking about the seekg(). Is this seekg() givin' me the possibility to read only a certain length of the file? THX

        D 1 Reply Last reply
        0
        • D dairiseky

          ok got y. thx again. Though u didn't say to me what u meant by the lenght of a line. Does space and zero count in this lenght? i'm talking about the seekg(). Is this seekg() givin' me the possibility to read only a certain length of the file? THX

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

          dairiseky wrote: Is this seekg() givin' me the possibility to read only a certain length of the file? I'm not exactly sure what you are asking here, but seekg() can be used to get a file's length.

          file.seekg(0, ios::end); // seek to the end
          long filesize = file.tellg();


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

          1 Reply Last reply
          0
          • D dairiseky

            Hi. Everything is nice about what u said to me for reading and writing to files. Except one or two things. :( Well first of all it cannot convert from objectA to char*. That's why I didn't use this method before, because I couldn't handle this stuff. Anyway why do u have to use binary files?

            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.

            THX

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

            Sorry! my mistake. Thanks a lot to Steve for clearing the doubts. You need not even include the (char*) part in read and write functions. Only outfile.write (&objectA, sizeof(objectA)); should also work. Keep in ur mind the things told by Steve about Pointers. Avoid including pointers in classes that you have to write to the disk. But as far as simple data types and arrays are concerned, it would do just fine. I hope that you know by now "why binary?" Sorry again for the mistake on my part. thx steve


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

            D 1 Reply Last reply
            0
            • S Steve S

              Ah. You've been told how to without any understanding of what's happening, which makes it difficult to fix this minor problem. The line outfile.write( (char*) objectA, sizeof(objectA)); should probably read outfile.write( (char*)&objectA, sizeof(objectA)); as you need to specify the address. Essentially, it's writing everything from memory to do with objectA's storage out to disk. This is not necessarily a good thing. If objectA contains any pointers, or has other objects embedded within it, you may well be able to read objectA back some time later, but then not be able to use it. Serialising an object is something that the object itself should know how to do, since it is possibly the only entity aware of all it's members. A blind write (and read) of the space occupied does not take into account the fact that the object may refer to other things, such as a char* member. In that case, the address in the char* member would be written out, and not the string it's pointing to1. When the object is later read back, the same address is assigned. Now if the memory map has changed significantly, it's possible that the address is not only not pointing to the right data, but it may in fact be pointing to unallocated or deallocated memory, which will cause an exception in your app. Steve S 1Yes, I know it's not a proper string, it's only an illustration, so calm down... :)

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

              Thanx steve for pointing out my error:doh:, i wrote that reply in a hurry. I'll make no such mistakes in future.;)


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

              1 Reply Last reply
              0
              • A avenger_sb25

                Sorry! my mistake. Thanks a lot to Steve for clearing the doubts. You need not even include the (char*) part in read and write functions. Only outfile.write (&objectA, sizeof(objectA)); should also work. Keep in ur mind the things told by Steve about Pointers. Avoid including pointers in classes that you have to write to the disk. But as far as simple data types and arrays are concerned, it would do just fine. I hope that you know by now "why binary?" Sorry again for the mistake on my part. thx steve


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

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

                yep is ok about binary. Yet there is still a problem. The objectA must be a const char*, that is not. U said to me :

                class A
                {
                public:
                double x;
                double y;
                double speed;
                double z;
                };

                As u can see here if I want to write the x, y, z. I could do as:

                main(int argc, char *argv[])
                {
                A objectA;

                fstream File;
                File.open ("test.txt", ios::binary);
                
                File.seekp(0);
                
                objectA.x=25.314;//xcoord
                objectA.y=-478.215;//ycoord
                objectA.z=34821.01;//zcoord
                

                Up to here nothing wierd. But then when I want to write into file I will use:

                File.write(&objectA.x, sizeof(objectA.x));

                And the error will be: 'class ostream &__thiscall ostream::write(const char *,int)' : cannot convert parameter 1 from 'double *' to 'const char *' And even if I will write simply :

                File.write(&objectA, sizeof(objectA));

                There will still be the same error: 'class ostream &__thiscall ostream::write(const char *,int)' : cannot convert parameter 1 from 'class A *' to 'const char *' :confused: I might have get things wrong...:doh: THX

                A A 2 Replies Last reply
                0
                • D dairiseky

                  yep is ok about binary. Yet there is still a problem. The objectA must be a const char*, that is not. U said to me :

                  class A
                  {
                  public:
                  double x;
                  double y;
                  double speed;
                  double z;
                  };

                  As u can see here if I want to write the x, y, z. I could do as:

                  main(int argc, char *argv[])
                  {
                  A objectA;

                  fstream File;
                  File.open ("test.txt", ios::binary);
                  
                  File.seekp(0);
                  
                  objectA.x=25.314;//xcoord
                  objectA.y=-478.215;//ycoord
                  objectA.z=34821.01;//zcoord
                  

                  Up to here nothing wierd. But then when I want to write into file I will use:

                  File.write(&objectA.x, sizeof(objectA.x));

                  And the error will be: 'class ostream &__thiscall ostream::write(const char *,int)' : cannot convert parameter 1 from 'double *' to 'const char *' And even if I will write simply :

                  File.write(&objectA, sizeof(objectA));

                  There will still be the same error: 'class ostream &__thiscall ostream::write(const char *,int)' : cannot convert parameter 1 from 'class A *' to 'const char *' :confused: I might have get things wrong...:doh: THX

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

                  :doh:Ooops! a small mistake on my part again. I was actually mixing the read function of C with read member of C++. In C++, the prototype for write is: istream& read(signed char*, int); So whatever we write, we have to cast its pointer to (char*) So if we have to write some object AAA to the file, we will use the following syntax:

                  ofstream outfile("test.txt", ios::binary);
                  outfile.write((char *) &AAA, sizeof(AAA));

                  Now let me tell you a few things: Dont try to retrieve data from a file using read where you have manually entered data. What i mean is, if you have created a file from notepad and have manually typed in the data, you cannot retrieve those data using istream::read() So the point is:- retrieve only those data from the file using istream::read that have been written to it using ostream::write. The following code is tested on my system and will help you in understanding all this better. After running the program, open the "123.txt" file in c:\ drive to view how data has been written in binary.

                  #include <iostream.h>
                  #include <fstream.h>

                  int main()
                  {
                  class A
                  {
                  public:
                  double x;
                  double y;
                  double z;
                  };

                  A objectA,objectB;

                  objectA.x=123.345;
                  objectA.y=345.003;
                  objectA.z=456.2;

                  cout<<"Following data will be written to 123.txt"<
                  cout<<"object A has been written to the file"<ifstream infile("c:\\123.txt",ios::binary); //create stream
                  infile.read((char*)&objectB,sizeof(objectB)); //read object from stream
                  outfile.close(); //close stream
                  cout<<"B.x="<After the for loop, objectA will contain the data from the 3rd record;
                  Ask if u have any problems.

                  1 Reply Last reply
                  0
                  • D dairiseky

                    yep is ok about binary. Yet there is still a problem. The objectA must be a const char*, that is not. U said to me :

                    class A
                    {
                    public:
                    double x;
                    double y;
                    double speed;
                    double z;
                    };

                    As u can see here if I want to write the x, y, z. I could do as:

                    main(int argc, char *argv[])
                    {
                    A objectA;

                    fstream File;
                    File.open ("test.txt", ios::binary);
                    
                    File.seekp(0);
                    
                    objectA.x=25.314;//xcoord
                    objectA.y=-478.215;//ycoord
                    objectA.z=34821.01;//zcoord
                    

                    Up to here nothing wierd. But then when I want to write into file I will use:

                    File.write(&objectA.x, sizeof(objectA.x));

                    And the error will be: 'class ostream &__thiscall ostream::write(const char *,int)' : cannot convert parameter 1 from 'double *' to 'const char *' And even if I will write simply :

                    File.write(&objectA, sizeof(objectA));

                    There will still be the same error: 'class ostream &__thiscall ostream::write(const char *,int)' : cannot convert parameter 1 from 'class A *' to 'const char *' :confused: I might have get things wrong...:doh: THX

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

                    :doh:Ooops! a small mistake on my part again. I was actually mixing the read function of C with read member of C++. In C++, the prototype for write is: istream& read(signed char*, int); So whatever we write, we have to cast its pointer to (char*) So if we have to write some object AAA to the file, we will use the following syntax:

                    ofstream outfile("test.txt", ios::binary);
                    outfile.write((char *) &AAA, sizeof(AAA));

                    Now let me tell you a few things: Dont try to retrieve data from a file using read where you have manually entered data. What i mean is, if you have created a file from notepad and have manually typed in the data, you cannot retrieve those data using istream::read() So the point is:- retrieve only those data from the file using istream::read that have been written to it using ostream::write. The following code is tested on my system and will help you in understanding all this better. After running the program, open the "123.txt" file in c:\ drive to view how data has been written in binary.

                    #include <iostream.h>
                    #include <fstream.h>

                    int main()
                    {
                    class A
                    {
                    public:
                    double x;
                    double y;
                    double z;
                    };

                    A objectA,objectB;

                    objectA.x=123.345;
                    objectA.y=345.003;
                    objectA.z=456.2;

                    cout<<"Following data will be written to 123.txt"<
                    cout<<"object A has been written to the file"<ifstream infile("c:\\123.txt",ios::binary); //create stream
                    infile.read((char*)&objectB,sizeof(objectB)); //read object from stream
                    outfile.close(); //close stream
                    cout<<"B.x="<After the for loop, objectA will contain the data from the 3rd record;
                    Ask if u have any problems.

                    D 1 Reply Last reply
                    0
                    • A avenger_sb25

                      :doh:Ooops! a small mistake on my part again. I was actually mixing the read function of C with read member of C++. In C++, the prototype for write is: istream& read(signed char*, int); So whatever we write, we have to cast its pointer to (char*) So if we have to write some object AAA to the file, we will use the following syntax:

                      ofstream outfile("test.txt", ios::binary);
                      outfile.write((char *) &AAA, sizeof(AAA));

                      Now let me tell you a few things: Dont try to retrieve data from a file using read where you have manually entered data. What i mean is, if you have created a file from notepad and have manually typed in the data, you cannot retrieve those data using istream::read() So the point is:- retrieve only those data from the file using istream::read that have been written to it using ostream::write. The following code is tested on my system and will help you in understanding all this better. After running the program, open the "123.txt" file in c:\ drive to view how data has been written in binary.

                      #include <iostream.h>
                      #include <fstream.h>

                      int main()
                      {
                      class A
                      {
                      public:
                      double x;
                      double y;
                      double z;
                      };

                      A objectA,objectB;

                      objectA.x=123.345;
                      objectA.y=345.003;
                      objectA.z=456.2;

                      cout<<"Following data will be written to 123.txt"<
                      cout<<"object A has been written to the file"<ifstream infile("c:\\123.txt",ios::binary); //create stream
                      infile.read((char*)&objectB,sizeof(objectB)); //read object from stream
                      outfile.close(); //close stream
                      cout<<"B.x="<After the for loop, objectA will contain the data from the 3rd record;
                      Ask if u have any problems.

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

                      Ok, I see. I just tested a bit this, and it's working ok. I will need some time to digest all this info (and faked info too ;P). If new quiery appear I will shout for help.:-D Thx, can I send to u some info about the robot, and some methods I intend to use to solve some problems? By the way are u good in maths? (probability and statistics?) If u want to answer to this, send the answer on my e-mail, plz. _____________________________ THX

                      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