• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1092
  • Last Modified:

FILE and CFile differences

Hi All,
When I write my data into a file using fwrite, there is always one redundant byte in the file. This doesn't happen when I use CFile. I give the two functions:
// Use FILE
void SaveData(CString filename, MYDATAPOINTER data, int datasize)
{
    FILE *stream;
    if( (stream = fopen( filename, "w" )) == NULL )  
            return;
     fwrite(data, datasize,1,stream);  
     fclose(stream);
}
// Use CFile
void SaveData(CString filename, MYDATAPOINTER data, int datasize)
{
    CFile stream;
    if(! stream.Open(filename,CFile::modeWrite))  
            return;
     stream.Write(data,datasize);
     stream.Close();
}
I assume that my datasize is 1200 bytes. If I use FILE, the data file size will be 1201 bytes. If I use CFile, the data file size will be 1200 bytes.
Please let me know what's the redundant byte in data file when use FILE!
0
notmegonow
Asked:
notmegonow
  • 6
  • 6
  • 2
  • +1
2 Solutions
 
Jase-CoderCommented:
I think this extra byte is the marker that signifies the EOF
0
 
mahesh1402Commented:
>>I assume that my datasize is 1200 bytes. If I use FILE, the data file size will be 1201 bytes

fwrite writes exactly how much you tell it to....check what is in datasize.... how you are calculating datasize using sizeof() ?

if you use like following.. u will find exact 1200 byte file..

fopen( filename, "wb" ); //open in binary
fwrite(data, 1200,1,stream);  // specify size manually 1200 and try


-MAHESH
0
 
mahesh1402Commented:
also note that if there are newline or NULL in string or buffer sizeof() will return extra byte..

like for example :

unsigned char buf[]="DATA" <==     sizeof(buf) = 5 as its string with terminated NULL

and

unsigned char buf[]={'D','A','T','A'} <==    sizeof(buf) = 4


-MAHESH
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
notmegonowAuthor Commented:
I think that extra byte can't be EOF.
That's right, my data is a pointer of a struct, I use sizeof() function to get its size. I just write only one item into file and compare both result files in debug mode and in Window Explorer. FILE really makes an extra byte in both cases. I will try fwrite(data, 1200,1,stream) tomorrow. If FILE write exactly, I must consider your second post. It may be the reason because there are some buffers in my struct. Right now, I think sizeof() works right. If it can return extra byte, CFile also write that extra byte into file and there should has been nothing difference.
I will return back after checking!
Thanks a lot!
0
 
mahesh1402Commented:
>>my data is a pointer of a struct

ALSO NOTE that this could be due to your compilers packing setting.
Basically, your compiler will pack structs/classes to a certain byte boundary.
You can modify this with the following code:

// align to 1 byte boundary (tightly pack)
#pragma pack(push, 1)

struct myStruct
{
.....
.....
};

#pragma pack(pop)

-MAHESH
0
 
notmegonowAuthor Commented:
wow, I heard about #pragma a long time ago. Yes, it can be the reason, I almost forgot it!
It's very kind of you! I will check it!
0
 
itsmeandnobodyelseCommented:
>>>> Please let me know what's the redundant byte in data file when use FILE!

fopen( filename, "w" )) opens/creates a text file by default. You would need "wb" option to create a binary file.

stream.Open(filename,CFile::modeWrite))  opens the file as a binary file instead of a text file. (CFile::modeWrite | CFile::typeText).

Windows OS turns any LF character (linefeed = '\n') to a CRLF (carriage return/linefeed = "\r\n") pair if a textfile is written to disk. That is omitted if it is opened as binary file. I would assume that the 1200 byte buffer contains exactly one linefeed character.

You may check the difference of both files by using the fc utility from command window (DOS box). Type

   fc /b file1 file2

and you should get the difference. If my guess was right one file contains 0x0A while the other contains  0x0D 0x0A instead.

Regards, Alex
0
 
mahesh1402Commented:
>>Windows OS turns any LF character (linefeed = '\n') to a CRLF (carriage return/linefeed = "\r\n") pair if a textfile is written to disk. That is omitted if it is opened as binary file

thats why I suggested as above try opening file in binary mode and write exact 1200 bytes specifying manually in fwrite

-MAHESH
0
 
notmegonowAuthor Commented:
Hi itsmeandnobodyelse,
Thanks for your recommendation. I tried many cases regarding to mahesh1402's suggestion I there are many results. When I use a constant char buffer, it returns wrong size more than 1 byte. When I open file with "wb", everything is ok, file size is correct.
Thank all of you for your help.
0
 
notmegonowAuthor Commented:
wow, i'm very sorry for a mistake, can any moderator help me? I assigned wrong mark for them.
mahesh1402: 50
itsmeandnobodyelse: 100
0
 
mahesh1402Commented:
notmegonow ,
you can post 0 points question in community support by specifying link to this question that you want to change this..
http://www.experts-exchange.com/Community_Support/


so have you got your answer ? if you have got it why grade B for 150 points question ??

-MAHESH
0
 
notmegonowAuthor Commented:
I got the anwser but not 100%. I will test and find out more explanations in the near future. There are still some things I need to make clear myself (especially with your suggestions). You all support me enough for the first step. I don't want to ask more if I have no full thought for it!
Thanks.

0
 
mahesh1402Commented:
ok do u want to change points ?
0
 
itsmeandnobodyelseCommented:
>>>>  I got the anwser but not 100%

You asked why you got an extra-byte when using fopen. What are the xx% you not got answered?

Note, the grade you give for the correct answer(s). Not for the thread and not for the answers that were not correct.

Regards, Alex

0
 
notmegonowAuthor Commented:
oK, I understand what u mean. If I got my answer, I should admit that the solution is excellent than confusing about the solutions. Thank all of you.
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 6
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now