File IO #3

I normally have been opening files and reading them using the following 
code:

//-----------------------------------------------------------
int i;
unsigned char bytes[3000], tdat[50];
FILE *in;

CString fname="test.dat";
in = fopen(fname, "rb");
fread(bytes, 2500, 1, in);      // read the full file
fclose(in);

for(i=0; i<30; i++){
  tdat[i] = bytes[i+70];        // extract some data from within bytes
}
//-----------------------------------------------------------

I have heard that the use of fopen & fread are frowned upon, not sure why.

How do I use "correct" code to open a file that contains 8,000,000 bytes of 
binary data
and be able to extract any parts of the original file as needed??
I have been trying to apply some examples given in this newsgroup without 
much luck.
Thanks for helping.


0
eddie331 (70)
9/19/2006 9:23:22 PM
vc.mfc 33608 articles. 0 followers. Follow

3 Replies
314 Views

Similar Articles

[PageSpeed] 1

Why not just use CFile.  CFile is just a front end for CreateFile, ReadFile, 
and WriteFile from the Win32SDK and they will work fine for what you're 
doing regardless of whether or not you use Unicode.  I'm not sure what 
CreateFile, ReadFile, and WriteFile use under the covers, but I guess I 
don't have to know.

Tom

"Ed" <eddie@hvc.rr.com> wrote in message news:edZPg.2392$x11.183@trndny02...
>I normally have been opening files and reading them using the following 
>code:
>
> //-----------------------------------------------------------
> int i;
> unsigned char bytes[3000], tdat[50];
> FILE *in;
>
> CString fname="test.dat";
> in = fopen(fname, "rb");
> fread(bytes, 2500, 1, in);      // read the full file
> fclose(in);
>
> for(i=0; i<30; i++){
>  tdat[i] = bytes[i+70];        // extract some data from within bytes
> }
> //-----------------------------------------------------------
>
> I have heard that the use of fopen & fread are frowned upon, not sure why.
>
> How do I use "correct" code to open a file that contains 8,000,000 bytes 
> of binary data
> and be able to extract any parts of the original file as needed??
> I have been trying to apply some examples given in this newsgroup without 
> much luck.
> Thanks for helping.
>
> 


0
tserface (3861)
9/19/2006 9:36:12 PM
Ed wrote:
> I normally have been opening files and reading them using the following 
> code:
> 
> //-----------------------------------------------------------
> int i;
> unsigned char bytes[3000], tdat[50];
> FILE *in;
> 
> CString fname="test.dat";
> in = fopen(fname, "rb");
> fread(bytes, 2500, 1, in);      // read the full file
> fclose(in);
> 
> for(i=0; i<30; i++){
>   tdat[i] = bytes[i+70];        // extract some data from within bytes
> }
> //-----------------------------------------------------------
> 
> I have heard that the use of fopen & fread are frowned upon, not sure why.
> 
> How do I use "correct" code to open a file that contains 8,000,000 bytes of 
> binary data
> and be able to extract any parts of the original file as needed??
> I have been trying to apply some examples given in this newsgroup without 
> much luck.
> Thanks for helping.

New improved version with the correction posted by Tom:

BYTE* buf = new BYTE[8000000];
CFile f;
f.Open(....);
UINT nBytesRead = f.Read(buf, 8000000);
f.Close();

BYTE tdat[50];
  for(i=0; i<30; i++){
    tdat[i] = buf[i+70]; // extract some data from within bytes

  ...later
delete [] buf;

P.S. You can use fread and fopen if you want to.  Answers in this MFC 
newsgroup tend to use MFC.

-- 
Scott McPhillips [VC++ MVP]

0
Scott
9/19/2006 11:49:45 PM
Scott,

You wrote the whole program for him.... you should get a commission :o)

I did some tracing through CFile::Open today and it looks like it's making 
calls to GetVolumeInformation() and FindFirstFile() for every file I open. 
Normally, and in the case of an 8MB file, this is not a big deal, but in 
cases where  I am opening a lot of small files (in the test I was doing 
there were 12,000 2K files) it tends to slow it down over just using 
CreateFile(), ReadFile(), and WriteFile() because of the slight overhead of 
these two functions.  I don't think this is a show stopper for CFile since 
it is so convenient to use most of the time, but it certainly is curious why 
they would add this overhead...  I don't seen anything "extra" on closing 
the file at all.

Tom

"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message 
news:%238ZXHZE3GHA.2096@TK2MSFTNGP05.phx.gbl...
> Ed wrote:
>> I normally have been opening files and reading them using the following 
>> code:
>>
>> //-----------------------------------------------------------
>> int i;
>> unsigned char bytes[3000], tdat[50];
>> FILE *in;
>>
>> CString fname="test.dat";
>> in = fopen(fname, "rb");
>> fread(bytes, 2500, 1, in);      // read the full file
>> fclose(in);
>>
>> for(i=0; i<30; i++){
>>   tdat[i] = bytes[i+70];        // extract some data from within bytes
>> }
>> //-----------------------------------------------------------
>>
>> I have heard that the use of fopen & fread are frowned upon, not sure 
>> why.
>>
>> How do I use "correct" code to open a file that contains 8,000,000 bytes 
>> of binary data
>> and be able to extract any parts of the original file as needed??
>> I have been trying to apply some examples given in this newsgroup without 
>> much luck.
>> Thanks for helping.
>
> New improved version with the correction posted by Tom:
>
> BYTE* buf = new BYTE[8000000];
> CFile f;
> f.Open(....);
> UINT nBytesRead = f.Read(buf, 8000000);
> f.Close();
>
> BYTE tdat[50];
>  for(i=0; i<30; i++){
>    tdat[i] = buf[i+70]; // extract some data from within bytes
>
>  ...later
> delete [] buf;
>
> P.S. You can use fread and fopen if you want to.  Answers in this MFC 
> newsgroup tend to use MFC.
>
> -- 
> Scott McPhillips [VC++ MVP]
> 


0
tserface (3861)
9/20/2006 12:03:00 AM
Reply:

Similar Artilces: