Open file for read and write

closed account (EAXy6Up4)
Hi guys!

I ask for help for a problem that I can not solve for several days.
I need to open a file for read and write in binary mode. I need to write a struct of 64 bytes. This is the code:

#define MSG_HEADER_LENGTH 64
#define MSG_LEN 64
#define BLOCK_FILLER 60
#define FIFO_SIZE 30000

typedef struct MsgHeader {
unsigned short inputPointer;
unsigned short outputPointer;
unsigned char filler[BLOCK_FILLER];
} MSGHEADER;

typedef unsigned char MsgToSend[MSG_LEN];

void FIFOFile::PutMessage(MsgToSend msgrec) {
MSGHEADER fifoHdr;
int fifoLen;
fstream fifoFile;

fifoFile.open(fileName, ios_base::in || ios_base::out || ios_base::binary);
// Get first record to know the pointers position
fifoFile.read((char *)&fifoHdr, sizeof(fifoHdr));
if(fifoHdr.inputPointer >= fifoHdr.outputPointer)
fifoLen = fifoHdr.inputPointer - fifoHdr.outputPointer;
else
fifoLen = fifoHdr.inputPointer + FIFO_SIZE - fifoHdr.outputPointer;

fifoFile.seekp(fifoHdr.inputPointer + MSG_LEN);

fifoFile.write((char *)&msgrec, sizeof(msgrec));

fifoHdr.inputPointer = fifoHdr.inputPointer + 64;
if(fifoHdr.inputPointer == FIFO_SIZE)
fifoHdr.inputPointer = 0;
fifoFile.seekp(0);
fifoFile.write((char *)&fifoHdr, sizeof(fifoHdr));

fifoFile.close();
}

The write after the seekp fails and the rdstate is badbit.
Why???
Can you help me??

Thank you very much!
Roberto
Topic archived. No new replies allowed.