2

in my lab work we gotta read datas from txt and write them to .dat file as a binary format, then we should search and find datas by given id in binary file. Our worker.txt file in this

format:

ID firstname lastname age salary

like:

341 Alex Morgan 32 3400
234 Jessica Gibson 28 2000
...

Program reads these datas then writes all of them to worker.dat as a binary file.

The problem is when entered valid Id for searching program find and print it but then close with windows stopped execution error, I need your help to handle it.

My searchByID function:

void searchByID( int key )
{
bool find=false;
int id,age,sal;
string fn,ln;
ifstream InFile;
InFile.open("worker.dat", ios::in | ios::binary);
int i = getSize( initialArr );
int j;
for( j=0; j<i; j++ ){
    InFile.read( (char*)&id, sizeof(int));
    InFile.read( (char*)&fn, sizeof(string));
    InFile.read( (char*)&ln, sizeof(string));
    InFile.read( (char*)&age, sizeof(int));
    InFile.read( (char*)&sal, sizeof(int));
    if( id == key )
    {
        cout << "Datas for Entered ID:\n"
            << id <<" "<< fn <<" "
            << ln <<" "<< age <<" "
            << sal << endl;
        find = true;
    }
}
if( !find )
    cout << "Entered ID not Found on File" << endl;
InFile.close();
}
7
  • 3
    This is a text file not a binary file. Commented Mar 10, 2013 at 15:21
  • You describe the format of worker.txt but not the format of worker.dat Commented Mar 10, 2013 at 15:24
  • Firstly program reads datas from txt file then writes all informations to binary file, you can reach binary file from this link: speedyshare.com/97mth/worker.dat Commented Mar 10, 2013 at 15:24
  • @Hogan I'm guessing the byte layout of worker.dat is pretty close to the code he's currently having issues with. It would obviously help to see the writer code. Commented Mar 10, 2013 at 15:25
  • @WhozCraig does that mean the strings are null terminated? Commented Mar 10, 2013 at 15:30

2 Answers 2

2

Doing something like this isn't going to work:

InFile.read( (char*)&fn, sizeof(string));

A string usually internally contains a pointer to the actual characters, so the actual text wouldn't be stored in the file anywhere. You would need to use a char array to read and write your strings instead.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 He should also be using known-width values (uint32_t, etc.) and ideally network-ordering them before the write, de-ordering them on the read, unless there is no interest whatsoever in portability.
Thank you for answering after changed all strings to char array program worked.
1

There seems to be a number of problems here, as I said in the comments you are describing the format of worker.txt and reading from a file called worker.dat. Are they the same format? If not then you are reading a text file. If so then you don't know the length of the strings -- but I would guess they are null terminated.

Here is an example of one of your problems -- sizeof(string) will always be the size of the pointer not the length of the string you are reading.

If this is a text file what you want to do is this:

  • Read a line from the text file.

  • Parse that line

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.