- #1
Epic Sandwich
- 25
- 0
Hi,
I've got a program which recalls strings randomly from a file. The file contains the strings, with each one taking up 200 bytes and if it's not long enough, I have spaces inserted after to make up the length. So they are all end to end, and there are no line breaks.
The program works out the length of the file, thus working out the number of strings stored in it ( length / 200 ). Then, it calls a random number and prints the string associated with that number.
That section of the program all works, however I am trying to write another program that adds strings to the file, and properly adding the spaces in afterwards to make the string 200 bytes long (since it would be tedious to do by hand). This works fine, however I am trying to implement a function that looks to see if the string is already in the file before it is written, to stop duplicates from appearing.
This is the function:
You pass it a string, it opens the file, searches and then returns true if the string already exists in the file (and false if it doesn't). I have tried multiple ways of doing this, but so far this is how far I've got.
I open the entire file and store it in a string, then use strstr to work out if the entered string exists in the entire file.
I'm pretty sure I'm doing this wrong, however all my attempts have currently failed.
Can anyone help?
Thanks :)
I've got a program which recalls strings randomly from a file. The file contains the strings, with each one taking up 200 bytes and if it's not long enough, I have spaces inserted after to make up the length. So they are all end to end, and there are no line breaks.
The program works out the length of the file, thus working out the number of strings stored in it ( length / 200 ). Then, it calls a random number and prints the string associated with that number.
That section of the program all works, however I am trying to write another program that adds strings to the file, and properly adding the spaces in afterwards to make the string 200 bytes long (since it would be tedious to do by hand). This works fine, however I am trying to implement a function that looks to see if the string is already in the file before it is written, to stop duplicates from appearing.
This is the function:
Code:
bool CheckString( char * string )
{
FILE * file; //Define file pointer
long fileLength;
char * currentFacts;
char tempString[ kMaxFactLength ];
strcpy( tempString, string );
tempString[ sizeof( tempString ) - 1 ] = ' ';
if( ( file = fopen( kFactsFilePath, "r" ) ) == NULL ) //Open file
return true;
if( fseek( file, 0L, SEEK_END ) != 0 ) //Set pointer to end. .
Error( "fseek" );
if( ( fileLength = ftell( file ) ) == -1L ) //. . and output position to work out size of file
Error( "ftell ");
if( ( currentFacts = malloc( sizeof( char ) * fileLength ) ) == NULL ) //Give currentFacts enough space to hold everything
Error( "malloc" );
if( fseek( file, 0L, SEEK_SET ) != 0 ) //Set pointer back to the start
Error( "fseek" );
if( fgets( currentFacts, fileLength, file ) == NULL ) //Read all the facts into currentFacts
Error( "fread" );
if( strstr( currentFacts, tempString ) == NULL )
{
free( currentFacts );
fclose( file );
return false;
}
free( currentFacts );
fclose( file );
return true;
}
You pass it a string, it opens the file, searches and then returns true if the string already exists in the file (and false if it doesn't). I have tried multiple ways of doing this, but so far this is how far I've got.
I open the entire file and store it in a string, then use strstr to work out if the entered string exists in the entire file.
I'm pretty sure I'm doing this wrong, however all my attempts have currently failed.
Can anyone help?
Thanks :)