CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2002
    Posts
    61

    working with binary files

    I have a few functions I'm using to write values to a binary file. The problem is when I read from the file to retrieve values all I get is the last value written.

    The structure I am inserting is:
    Code:
    struct tsDataS
    {
    	unsigned	ts		: 32;
    	unsigned	volID	: 8;
    	unsigned	offset	: 32;
    };
    I calll the function add to insert (update) the value:
    Code:
    bool timestamp::add(tsData &ts)
    {
    
    	if(!myDB) return 0;
    
    	//change this to seek for empty spots first
    	fstream mFile;
    	int location, findRec;
    
    	objKey1.ts = TS_EMPTY;
    
    	//do we have an open entry space?
    	findRec = seekLocation(objKey1, TS_TST);//used to determine the record number
    	
    	mFile.open(myDB,ios::binary|ios::out);
    	location = (findRec - 1)*sizeof(ts);
    
    	mFile.seekp (location);//used to determine location of record
    	mFile.write((char*)&ts,sizeof(ts));
    
    	//replace old stamp
    	if(findRec < DBSize)
    	{
    		//increment counter
    		activeTScount++;
    	}
    
    	//need to make a new entry
    	else
    	{
    		//increment counters
    		DBSize++;
    		activeTScount++;
    	}
    	return 1;
    
    }
    And then add calls this to get the location of any "empty" (TS_EMPTY == ts) values:

    Code:
    int timestamp::seekLocation(tsData &ts, int tok)
    //Find a timestamp matching field tok in ts
    {
    
    	fstream seekFile;
    	int count =0;
    	bool found = false;
    	seekFile.open(myDB,ios::binary | ios::in);
    	seekFile.seekg(0,ios::beg);
    	while(seekFile.read((char*)&objKey1,sizeof(objKey1)))
    	{
    		++count;
    	if( tok == TS_VOL )
    		if(ts.volID == objKey1.volID)
    		{
    			found = true;
    			break;
    		}
    	else if ( tok == TS_OFF )
    		if(ts.offset == objKey1.offset)
    		{
    			found = true;
    			break;
    		}
    	else if ( tok == TS_TST )
    		if(ts.ts == objKey1.ts)
    		{
    			found = true;
    			break;
    		}
    	else if ( tok == TS_VOL + TS_OFF )
    		if(ts.offset == objKey1.offset && ts.volID == objKey1.volID)
    		{
    			found = true;
    			break;
    		}
    	}
    	seekFile.close();
    	
    	if(found) return count;
    	return count + 1;
    
    }
    Also another interesting point is that sizeof(objKey1) == 12 and sizeof(ts) == 4 even thought they are both struct tsDataS which I thought should equal 9??

    thanks,
    Devan
    Last edited by DevLip; March 30th, 2004 at 11:58 AM.

  2. #2
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715
    a few problems...


    from inside your insert file....
    Code:
    mFile.open(myDB,ios::binary|ios::out);
    	location = (findRec - 1)*sizeof(ts);
    
    	mFile.seekp (location);//used to determine location of record
    	mFile.write((char*)&ts,sizeof(ts));
    The problem here is that you cannot seek paste the end of a file location. So if you have a file with one record in it and wish to seek to record too your seek will fail. And you'll never know it because you're not checking the return value....

    So your insert/add is hosed up...

    ****************************************************

    Also another interesting point is that sizeof(objKey1) == 12 and sizeof(ts) == 4 even thought they are both struct tsDataS which I thought should equal 9??
    how do you get nine? All the variables are unsigned longs or 4 bytes.. 3x4 is 12!

    code:--------------------------------------------------------------------------------struct tsDataS
    {
    unsigned ts : 32; // 32 bits or 4 bytes
    unsigned volID : 8; // 32 bits or 4 bytes
    unsigned offset : 32; // 32 bits or 4 bytes
    };--------------------------------------------------------------------------------


    sizeof(objKey1) == 12 and sizeof(ts) == 4
    That's interesting... objkey1 is a a structure and that should be 12. ts is a refference to a structure which should be 12 too. The only reason it could be 4 is if you were getting the size of a pointer here. something like

    Code:
    sizeof(&ts)
    That would give you 4..... because thats the size of pointer values in a 32bit OS.

    hope I helped.... check all your return values and log any errors...

  3. #3
    Join Date
    Apr 2002
    Posts
    61
    THanks guys, I figured it out, there was actually a bug in the retrieval function so that it was overwritting the data :O

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured