0

I am writing a driver and using fwrite for logging data to a file. I have a logger class which uses fwrite. Now i am passing the pointer and size but the data written into the file seems to be 161 bytes while I pass 160 as the length (call by ref as an arg of the function of the logger class - a fwrite wrapper). In 5/6 ases, the 160bytes are written but in 1 case 161 bytes are written. I am not able to understand how.

Additional info: I am on Win7. Using VS/DDK, Using Notepad++ hex editor to view the data, data is written with '\n' as separating the data tags so that it can be seen in notepad also.

code:

size_t AbhiLogger :: addData(const void * data, size_t length)  {
    if(m_pFile== NULL)
        return E_BADFILE;

    return fwrite(data, 1,length, m_pFile);
}

AbhiLogger encapsulates the path, filename and extensions along with the filehandle. The reason is that you can change the name/extn/path any or all and it will associate the handle to newly opened after closing the previous file.

Abhinav
  • 1,496
  • 3
  • 15
  • 31
  • 3
    could do with seeing some code – ColWhi May 28 '12 at 08:54
  • 2
    Did you use text mode to open the file? Because on text mode '\n' will change to "\r\n" on Windows. – Celebi May 28 '12 at 08:57
  • \r\n is fine as i can see them repeating and they are as CRLF everywhere. The doubt is fwrite – Abhinav May 28 '12 at 09:19
  • @Sasquiha there is not much code inside wrapper method. just checking if the filehandle is NULL and writing accordingly using fwrite(). – Abhinav May 28 '12 at 09:21
  • please give us the code, where you access the file handle (the fopen/fclose/fwrite part) – Peter Miehle May 28 '12 at 09:27
  • maybe your code is writing an old-style-windows end-of-file (^Z) at the end of your file? – Peter Miehle May 28 '12 at 09:28
  • @PeterMiehle added code snippet. – Abhinav May 28 '12 at 13:23
  • Any issues like alignment, asynchronous return and data synchronization affects the fwrite? I want byte by byte so I specify 1. The data is actually making sense as 16 bytes group but I should nt care abut that as i know the range to be copied. Doses that makes sense? – Abhinav May 28 '12 at 13:26
  • Your return type is `size_t`. How will you detect an error? Since most of us are not psychic, we will need more code to diagnose the problem. – JimR May 28 '12 at 13:42

0 Answers0