Reading a .bin file

Posts 1–2 of 2 · Page 1 of 1
Reading a .bin file
Using streams to read a .bin file. Inside these .bin files contain a directory path and file type and size. How would i go about reading the line in order to make sure that i get the correct data.

example of some data in the .bin file...


Code:
$Owner\Pets\Dog.txt$15$
Directory File Folder: Owner\Pets
File Name: Dog.txt
File Size: 15kbs

Code:
#include <iostream>
#include <fstream>
#include <cstdlib>

struct MyFile
{
	std::string Directory;
	std::string Filename;
	unsigned filesize;
};

int main()
{
   std::ifstream readfile ( "mybin.bin");
   if(!readfile)
	{
		std::cout << "Please check that the file exists" << std::endl;
		exit(1);
	}
   while (!readfile.eof())
   {
	   //getline of directory
          // getline of filename
         // get line of filesize   
   }
     // copy all the data to a new file and save it..
}
Since the format inside a .bin file could really be anything, this is something that you'll need to work out yourself. Notice that .bin stands for binary which means that normal line ending conventions are not used, and that you're most probably dealing with a file that only consists of numbers.

Unless you have a token that is used to signify the end of a line, getline() won't work. If the $ is an delimiter you could use the string::find() and string::substring() functions to split the file into meaningful data. I'll leave the splitting of directory and filename to you:

Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>

using namespace std;

struct MyFile
{
	string Directory;
	string Filename;
	unsigned filesize;
};

char getc( fstream& f ){
	char buf = 0;
	try{
		f.read( &buf, 1 );
	} catch( std::exception &e )
	{
		cout << "Error: " << e.what() << endl;
		return -1;
	}
	return buf;
}


int main( int argc, char* argv[] )
{
   vector< string > data;
   string CurrentItem = "";

   fstream readfile ( argv[1] , ios::in | ios::binary );
   if(!readfile)
	{
		std::cout << "Please check that the file exists" << std::endl;
		exit(1);
	}
   while (!readfile.eof())
   {
	   // start of a new line!
		if( getc( readfile ) == '$' )
		{
			do{
				// oops
				if( readfile.eof() ){
					cout << "Error: expected '$' found: eof!" << endl;
					return 0;
				}

				char c = getc( readfile );

				if( c == '$' )
					break;

				CurrentItem += c;

				// closing token
			} while(  true );

			// collect and flush data
			data.push_back( CurrentItem );
			CurrentItem.clear();

			// done reading?
			getc( readfile );
			if( readfile.eof() )
				break;

			//rewind two characters, end line token is also newline token. 
			int Location =  readfile.tellg();
			readfile.seekg( Location - 2 );

		}

   }
    cout << "File read:" << endl;

	for( unsigned i = 0; i < data.size(); i++ )
	{
		cout << "Read: " << data.at( i ).c_str() << endl;
	}


     // copy all the data to a new file and save it..
}
Posts 1–2 of 2 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us