Hi guys,
I'm new at C++ and I'm learning the basics on binary files

This is what I've done:
1.- I created a class Criminal with basic data (age, crime type, criminal name)

class Criminal
    int vnSerie;
    char vNombre[ 36 ];
    char vtCrimen[ 20 ];
    int vEdad;

    Criminal(int = 0, std::string = "", std::string = "", int = 0);
    ... Functions to get and set data

2.- In my main I used

listaSalida.open( "Delincuentes.dat", std::ios::out | std::ios::binary | std::ios::app );

and I add elements to the file

3.- I allowed the user to choose the part in the array to save his data with the Criminal's serial number with:

listaSalida.seekp( (primerLista.jalanSerie() - 1) * sizeof( Criminal ) );

4.- I also used strings and getline to give the user freedom in case the Name is some fancy name but in the class's functions the strings are converted to null terminated char arrays.

5.- Writing the file was successful!!

6.- I created another cpp to read the file, I have a function to search for the serial so that was a success

7.- I also have a function to allow the user to print to the screen the entire data in the file


I am trying to implement a search query, for example if the users types "Theft" the database returns all the entries with theft on the type of crime

I've hit a wall on this matter.
This is what I'm trying to do, I need pointers on this:
1.- Read the file as a string array
2.- Search for the word on the file
3.- Return the position of the word in the file
4.- Seekg the position of the word and print out the contents
5.- Be happy??

Am I on the right track? Is there another way to do it, easier?

6 Years
Discussion Span
Last Post by Ketsuekiame

I personally, would read in each criminal as a whole entity. Check if the Criminal has a crime type of "Theft" and if so, save this in a vector or list. Once you've iterated through the entire file, return the list to the calling method.

So some pseudocode

Open File
While Not End of Criminal File
    Read a Criminal From File into Criminal Object
    If CrimeType Equals "Theft"
        List.Add Criminal
    Increment File Pointer Position
    Continue Loop
Close File
Return List

Edited by Ketsuekiame: n/a

Votes + Comments
Thanks!!! really helped me out on this subject
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.