Solved

FILE and CFile differences

Posted on 2006-06-29
16
1,049 Views
Last Modified: 2013-11-20
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
Comment
Question by:notmegonow
  • 6
  • 6
  • 2
  • +1
16 Comments
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 17007930
I think this extra byte is the marker that signifies the EOF
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17008022
>>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
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17008140
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
 

Author Comment

by:notmegonow
ID: 17010222
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
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17010313
>>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
 

Author Comment

by:notmegonow
ID: 17010411
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
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 100 total points
ID: 17011380
>>>> 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 22

Assisted Solution

by:mahesh1402
mahesh1402 earned 50 total points
ID: 17015605
>>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
 

Author Comment

by:notmegonow
ID: 17015607
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
 

Author Comment

by:notmegonow
ID: 17015620
wow, i'm very sorry for a mistake, can any moderator help me? I assigned wrong mark for them.
mahesh1402: 50
itsmeandnobodyelse: 100
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17015634
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
 

Author Comment

by:notmegonow
ID: 17018559
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
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17019463
ok do u want to change points ?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 17022988
>>>>  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
 

Author Comment

by:notmegonow
ID: 17028709
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumDigits challenge 9 98
viewing source code from eclipse 13 74
Change to event 1 74
how to split multiple lines delimiter : 8 55
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now