Monday, December 12, 2011

How do i write Binary to .dat file in C++?

I am writing a database server in C++ ..





To do this i have a metafile with tableName, columnName, dataType and file location for this tables data.





(Prior program process)


~the program runs once creates a metafile


~when it runs a second time i use istreamVariable.open(fileLocation,ios::b鈥?and write the above to the file


~third time it runs it reads in the binary information fine..





BUT..(here's my problem)





~for the table column values i will need to be saving int,long,char,etc.. in binary


~just as a test run i have INT j=4; and then i write 'j' to file in binary





!!~!! when i read the file i get 4%26lt;garbarge%26gt;4





Q. How do i properly write and read information other than char to file in binary?


Q.2 How can i write multiple variables to file in binary %26amp; be able to extract them individualy while not knowing the datatype?





Thanks for any help with either question..|||Make sure your reads are exactly symmetrical with your writes.





As to how to know what type to read next from the file, you have to either





1. Have a fixed file format so the reads know implicitly what the writes did, or.





2. You need to output a type code preceding each data item. Each type code will have an implicit size, of your choosing, so the read knows how much to read in.





Personally, for a file with some degree of complexity, I'd choose the second method, so I could easily change or expand the file format.





In either case, the reads will have to know beforehand where the data is to be stored, again implying some pre-destination.





Hope that helps.|||I think opening a binary file in windows ruins it.

No comments:

Post a Comment