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..
}