Solved

copy file into buffer

Posted on 2004-08-09
12
801 Views
Last Modified: 2010-04-15
I want to copy a file into a buffer.  Here is my code:

FILE *f = fopen(path,"r");
int size = GetFileSize(path);

byte *buffer = new byte[size];

int bytesCopied = 0;

while(bytesCopied != size)
    bytesCopied += fread(buffer + bytesCopied,1,size,f);

The file I am testing this on is 4.25KB.  The first called to fread reads in 52 bytes.  But then it is just stuck in an endless loop where fread returns 0.  
0
Comment
Question by:lwinkenb
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11759462
Hi lwinkenb,

try
>     bytesCopied += fread(buffer + bytesCopied,1,size,f);
bytesCopied += fread(buffer + bytesCopied,size,1,f);

Sunnycoder
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11759547
ooops my bad .. previous line was just as good, or rather better

The code looks good and I guess the error is due to something else

Expand the loop a bit

while(bytesCopied != size)
{
    temp=fread(buffer + bytesCopied,1,size,f);
    bytesCopied += temp;
   if ( temp == 0 )
   {
            if (feof(f))
                   printf("end of file\n");
            else
                   perror("fread:");
    }
}

how do you get the size of the file? amount of contents in the file is different from size on disk (may not fill the whole block on disk)
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 11759872
"how do you get the size of the file? amount of contents in the file is different from size on disk"
Oh, I didn't know that.  Well I actually am using the MFC function CFile::GetStatus to get the file size.  Will that work for me?  

I expanded the loop like your example, and it is reaching the end of the file after the first read.  How is this possible since windows says the file is 5k, and my size variable  gets set to 5040?
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 50 total points
ID: 11759917
Files are allocated space on disk in blocks. Block size varies from platform to platform.
If a file has say 100 bytes of content, it will be allocated one block. Assuming that block size is 4K, the remaining 3K and 924 bytes will go unused.

sorry not too familiar with MFC but I think you should restructure your loop like this

while(1)
{
    temp=fread(buffer + bytesCopied,1,size,f);
    bytesCopied += temp;
   if ( temp == 0 )
   {
            if (feof(f))
            {
                   printf("read complete\n");
                   break;
            }
            else
            {
                   perror("fread:");
                   //you can choose to break ot continue here or you can use a flag to permit only a certain number of retries.
            }
    }
}

Since you can get out of loop in case of errors and you have errors notified to you, this will be more robust.
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 11759959
That is very close to what I have:

      int temp;
      while(bytesCopied != imageSize)
      {
            temp = fread(buffer + bytesCopied,1,imageSize,f);
            bytesCopied += temp;
            if(!temp)
            {
                  if(feof(f))
                  {
                        AfxMessageBox("End of file");
                        break;
                  }
                  else
                  {
                        AfxMessageBox("Read error");
                        break;
                  }
            }
      }

I am reaching AfxMessageBox("End of file") after reading in 52 bytes.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11759977
Hi,

To quickly retrieve the number of bytes in a file,use this:
   FILE *in;
   long size;
   in = fopen("test.txt", "r");
   fseek(in,0,SEEK_END);
   size=ftell(in);
   fclose(in);
   printf("%ld",size);
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 45

Expert Comment

by:sunnycoder
ID: 11759980
open the file in notepad and check the contents ... difference between 52 bytes and 5Kb should be easy to make out
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 11760028
The file is 5KB.  It's a JPG.
0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 200 total points
ID: 11760050
You might need to open the file in binary mode using "rb" in fopen()
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11760090
yep ... thats right ... binary files on windows need to be opened with b flag
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 11760192
yep, that was it.  Thanks :)
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11760352
>>     bytesCopied += fread(buffer + bytesCopied,1,size,f);
>bytesCopied += fread(buffer + bytesCopied,size,1,f);

int bytesRead;
do{
  bytesCopied += bytesRead = fread(buffer + bytesCopied,size - bytesCopied,1,f);
} while ( bytesCopied < size && bytesRead > 0 );

Note two adjustments from sunnys suggestion:

fread(...,size - bytesCopied,...);

and the loop construct.

Paul
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

746 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