LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

fwrite error : trying to write 26 bytes to file and having issues

Solved!
Go to solution

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

0 Kudos
Message 1 of 3
(5,050 Views)
Solution
Accepted by topic author smartprogrammer

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).



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 3
(5,041 Views)

thanks alot my dear respected friend you are always there to help ,,  i admire your helping nature and knowledge. thanks alot

Message 3 of 3
(5,025 Views)