Like Tree2Likes
  • 2 Post By Elysia

trouble reading binary data file

This is a discussion on trouble reading binary data file within the C++ Programming forums, part of the General Programming Boards category; Hi, I was hoping to get some help to understand why I have coded this so poorly. I have re ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    12

    trouble reading binary data file

    Hi,

    I was hoping to get some help to understand why I have coded this so poorly. I have re written the code below to demonstrate the problem I face. A really good indicator that something is wrong is the fact that it crashes 50% during run time.
    I would like to know why it crashes half the time ? what conditions had changed to make it execute okay the other times.

    My main problem is that I had thought this had successfully written to file. As i have tested, by reading it back.
    But if I remove my output to file code and only try to import my file (previously created) it recognizes there are two elements but can't output its data.
    I am using code::blocks 10.05.
    btw, I was only wanting to do this as a learning experience with binary files I do understand I could use a text file. Should I not be using binary files with string.

    Many thanks in advance for your help.

    Code:
    #include <iostream>
    #include <vector>
    #include <fstream>
    
    // Demonstrating my problem with reading from binary from file
    
    using namespace std;
    
    int main()
    {
    vector<string> newBuffer;    // will output file from here
    vector<string> tempBuffer;    // will input file into here
    
    string testing = "hello world\n";
    tempBuffer.push_back(testing);
    tempBuffer.push_back(testing);
    
    
    // output to file
    ///*
            ofstream fout ("file1", ios::binary);
    
            if(!fout)
            {
                cout << "unable to open file\n";
            }
            fout.write( (char*) &tempBuffer,sizeof tempBuffer);          // casting char*
            fout.close();
    
            tempBuffer.clear();
    //*/
    
    // read from file put into new vector
    
        ifstream fin ("file1", ios::binary);
        if(!fin)
        {
            cout << "unable to open file\n";
        }
        fin.read( (char*) &newBuffer,sizeof newBuffer);
        fin.close();
    
    
    // reading new vector
    
    
        cout << "newBuffer size " << newBuffer.size() << "\n";
    
        for (unsigned short x =0;x<newBuffer.size();x++)
                cout << (string) newBuffer[x];
    
        newBuffer.clear();
    
        return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Code:
    $ odx file1
    000000 60 80 35 01 00 00 00 00 70 80 35 01 00 00 00 00  >`.5.....p.5.....<
    000010 70 80 35 01 00 00 00 00                          >p.5.....<
    000018
    The thing that is written out to file1 is NOT your string, but just a bunch of pointers to whatever a string object allocates.

    Likely as not, these pointers are deallocated when you do
    tempBuffer.clear();

    Then you pretend they exist once more when you read things in, and you're screwed when you try to access memory.


    Try writing programs without any casts.
    In fact, you should only try low level read() and write() on POD types (or arrays of them).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    If you want to write C++ objects to file, do so with the << operator. If it doesn't work, it simply means you can't serialize the objects directly. Never write a C++ container directly to file using the write method.
    You will have to do this manually, the C way (or you can use Boost.Serialize). Write the length of the data, then write the data. Then read the length of the data, allocate, then read it.
    stahta01 and garth like this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    As much as we like to see people using vectors, you are trying to learn two thing here at once, you'd be less confused, and would learn more, if you tried to learn the two things separately.
    I.e. First learn about file IO using ordinary arrays (practice that), then learn about vectors and a little about how they are implemented.

    You'll be able to combine the two together when you understand enough about each separately.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    12
    Thanks guys, obviously bit off more than I could chew. My tutorials had explained how to save a simple object to binary file using write/read. My class contains linked lists of objects and vectors of strings which I thought I might be able to apply the same theory to. Once that failed I did try to simplify it as iMalc suggested and thought I had proved to myself containers populated with int would work (no class) using my method. But I was obviously mislead by the first iteration of output that suggested I had successfully written and read back from file.
    Oh well, I think have probably been adventurous enough and should return back to appending to text files. I am using strings after all.
    p.s
    In regards to Salem remarks regarding casting. Should I let the commands that I want to use (say read) dictate to me what my class member variable types should be?
    Obviously strings are easier to work with then char *. Is it normal to go back and rewrite your code as you amend the type to avoid casting ? Or do you tend to learn what types are better used with what commands so tend to choose wisely from the beginning?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    No, you should focus on using the correct types and choosing the best methods to manipulate the data from those.
    Also, here is a fun example on how to serialize a vector (and any other container that works with iterators):

    Code:
    #include <vector>
    #include <string>
    #include <fstream>
    #include <algorithm>
    #include <iterator>
    
    int main()
    {
    	std::vector<std::string> test;
    	test.push_back("test1");
    	test.push_back("test2");
    	test.push_back("test3");
    	test.push_back("test4");
    	test.push_back("test5");
    	test.push_back("test6");
    	test.push_back("test7");
    	test.push_back("test8");
    	test.push_back("test9");
    	std::ofstream out("C:\\test.txt");
    	std::copy(test.begin(), test.end(), std::ostream_iterator<std::string>(out, " "));
    	out.close();
    	
    	test.clear();
    
    	std::ifstream in("C:\\test.txt");
    	std::copy(std::istream_iterator<std::string>(in), std::istream_iterator<std::string>(), std::back_inserter(test));
    	return 0;
    }
    I can't be bothered to explain it all, though, because I'm lazy >_<
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Just a nit... the OP needs to be including the <string> header and not rely on his implementation doing so for him through one of the existing headers.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Jul 2012
    Posts
    12
    Quote Originally Posted by Elysia View Post
    No, you should focus on using the correct types and choosing the best methods to manipulate the data from those.
    okay I think I understand that. For example if using c style char* I should stay with c libraries to manipulate it?

    The serialize a vector worked great. But my strings contained multiple words "hello big world" for example and I couldn't work out how to stop it from truncating the white spaces.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    Quote Originally Posted by garth
    For example if using c style char* I should stay with c libraries to manipulate it?
    It depends. I would prefer to deal with a higher level of abstraction like the vector, then convert to char* when needed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-26-2007, 05:48 PM
  2. need a little help reading binary data in from a file
    By andydavenport in forum C Programming
    Replies: 8
    Last Post: 07-01-2005, 09:50 AM
  3. trouble reading data from a file
    By bob56 in forum C Programming
    Replies: 14
    Last Post: 03-17-2005, 08:03 AM
  4. Reading data from a binary file
    By John22 in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 01:00 PM
  5. having trouble reading data from a file
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 11-08-2001, 10:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21