#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
#include "FixedLen.h"
|
|
|
void InsertIndex( char* ISBN, int offset ) {
fstream index, temp;
int number, length, result;
char str[256], buffer[1024];
bool found = false, duplicate = false;
|
|
|
index.open( "index.txt", fstream::out | fstream::in );
index.read( str, 6 );
if ( ( number = atoi( str ) ) == 0 ) {
index.seekp( 12, fstream::beg );
index << ISBN << '|' << FixedLen( offset, str, 5 ) << endl;
index.seekp( 0, fstream::beg );
index << FixedLen( 1, str, 6 );
index.close( );
return;
}
|
|
|
temp.open( "temp.txt", fstream::out );
temp << FixedLen( number+1, str, 6 );
index.read( str, 6 );
length = atoi( str );
temp.write( str, 6 );
for ( int i = 0; i < number && !found; i++ ) {
index.read( str, length );
if ( ( result = strncmp( ISBN, str, 10 ) ) > 0 )
temp.write( str, length );
else {
if ( result == 0 ) duplicate = true;
temp << ISBN << '|' << FixedLen( offset, buffer, 5 ) << endl;
temp.write( str, length );
do {
index.read( buffer, 1024 );
length = index.gcount( );
temp.write( buffer, length );
} while ( !index.eof( ) );
found = true;
}
}
|
|
|
if ( !found )
temp << ISBN << '|' << FixedLen( offset, buffer, 5 ) << endl;
if ( duplicate )
cout << "\n\nDuplicate ISBN: " << ISBN << "\n\n";
system( "mv temp.txt index.txt" );
index.close( );
temp.close ( );
}
|
|
|
|