I have a simple task to accomplish with this routine where, all it has to do is, open the file, append data from a buffer & close.
I am using ‘open’ & ‘write’ for that purpose on a linux machine. Although the return code after ‘write()’ is positive, the file size does not increase and it is always empty. I ma pulling my hair to figure out what the issue with the below code. Thought some fresh eyes can shed some light.
#define BIT_Q_FILE ".\\bitq.dat"
int BQWrite(void *p)
{
int fd ;
int rc = -1 ;
fd = open(BIT_Q_FILE, O_RDWR | O_APPEND ) ;
if (fd < 0)
return -1;
memset(&BITQBuff,0,sizeof(typeBITQFile));
memcpy(&BITQBuff.pBitQueue,p,sizeof(typeBITQueue));
rc = write(fd, &BITQBuff,sizeof(typeBITQFile)) ;
close(fd) ;
if(rc!=sizeof(typeBITQFile))
{
return -1;
}
rc = sizeof(typeBITQueue);
return rc ;
}
I got your problem right here:
You’ve hit a trifecta of Windows-to-Unix porting gotchas:
/, not\.\in the middle of a file name. (The only bytes — and I really mean bytes, not characters — that cannot appear in a pathname component are those with the values 0x2F and 0x00.)lsdoes not print any file names that begin with a dot.So you are expecting data to be written to a file named
bitq.datin the current directory, but it is actually being written to a file named.\bitq.dat, still in the current directory. That file is hidden by default, so it looks like the data is disappearing into thin air.ls -awill reveal the hidden file, andrm .\\bitq.datwill delete it. To fix your code, just change the define toIt is not necessary to put a leading
./on the path passed toopen.This may not be the only problem with your code, but I don’t see anything else obviously wrong. If you need more help, please post a new question with a complete, minimal test program that people can compile and run for themselves.