06-04-2015 06:23 AM
i am trying to write 26 Hex Bytes data to file using fwrite and i am having unknown issues due to which 27 bytes get written
/////////////////////////////////// Code ///////////////////////////////////////////////////////////////////
FILE *READ;
READ = fopen ("DataReceived.dat" , "w"); // opening file for writing
unsigned char data2send[26] = { 0x00 , 0x00 , 0x00, 0x00, 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00,
0x00 , 0x00, 0x00, 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00, 0x00 , 0x00 , 0x00 , 0x00, 0x00 , 0x00 };
data2send[0]=0xEB; // assigning Data
data2send[1]=0x90; // assigning data
data2send[10]=0x08;// assigning Data
crcsent = calCRC16(data2send, 24); // calculating CRC of 24 bytes and adding (16 bit) crc to last two bytes of data
data2send[24]=(crcsent>>8) & 0xFF; // crc added to second last byte
data2send[25]= (crcsent & 0xFF); // crc added to last byte
if(ComWrt( coms , data2send , 26)==26) // sending data to com port
{
fwrite (data2send, 1, 26 , READ); // ===> writing data to file where there is problem
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
Output (WRITTEN to the file )should be ::
0xEB 0x90 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x08 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x0A 0xA1
Output which i get on the file ( which is incorrect ) and it should not be done because i have specified 26 bytes to write
0xEB 0x90 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x08 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x0D 0x0A 0xA1
how 0x0D is added to my data although i am only adding data to byte 0,1,10,24,25
when i am writing 26 bytes then how 27 bytes get written on to the file ??
thanks in advance
ahsan
Solved! Go to Solution.
06-04-2015 08:21 AM
I can only guess that the problem may arise from opening the file in text mode and writing a newline character to it (0x0A): the system may automatically add a carriage return (0x0D) to maintain the CRLF standard. Try opening the file in binary mode instead (fopen with "wb" specification).
06-05-2015 12:56 AM
thanks alot my dear respected friend you are always there to help ,, i admire your helping nature and knowledge. thanks alot