Data messed up by using fwrite

2005-11-16 09:10:41 PM
Hello gurus in this group,
I am using BCB 6.0 to development my program. And the operation
system is WIndows XP embedded. And the HDD is in fact a CF disk.
In my application, there are totally 8 COM port will be handled,
which are using the WIN32 API CreateFile/WriteFile/ReadFile to
access within 5 different threads of my program. Each thread will
create a file to record the data separately by using traditional
fopen/fwrite C run time library. So there are totally 5 threads
and want to write data to 5 different files, and maybe in the
same time.
Each thread is in fact a while loop to rearrange the data from
the COM Port and then record the data into their own file by
using fwrite().
Each record is 4 bytes timetag(tickcount), and followed by
the data itself (between 6 to 32 bytes). After the fwrite(),
a fflush() is called to make sure data in the buffer is flushed
into the CF Disk. The data file is created as a binary file by
using fopen().
My problem is, if this program executes for a while, say 12 hours.
The recorded data in some (one or more) files is messed up.
Most of the time the data is as the following pattern,
TIME DATA1 ~ DATAN (correct)
TIME DATA1 ~ DATAN (correct)
TIME DATA1 ~ DATAN GARBAGES(4 or 8 or 12 or 16 bytes)
TIME DATA1 ~ DATAN (correct)
TIME DATA1 ~ DATAN (correct)
TIME DATA1 GARBAGES(4 or 8 or 12 or 16 bytes)
TIME DATA1 ~ DATAN (correct)
TIME DATA1 ~ DATAN (correct)
I had export the make file and found that the LINK argument
-tWM is add. So, it should not be the race condition of the
C runtime library.
I still cannot figure out where those GARBAGE data came from.
They are all the same code in a while loop, the same data
length for each record.
I hope someone here can give me some idea to try to solve this
strange problem.
Allen Chang