• C

copy file into buffer

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.  
LVL 5
lwinkenbAsked:
Who is Participating?
 
ankuratvbConnect With a Mentor Commented:
You might need to open the file in binary mode using "rb" in fopen()
0
 
sunnycoderCommented:
Hi lwinkenb,

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

Sunnycoder
0
 
sunnycoderCommented:
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
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
lwinkenbAuthor Commented:
"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
 
sunnycoderConnect With a Mentor Commented:
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
 
lwinkenbAuthor Commented:
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
 
ankuratvbCommented:
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
 
sunnycoderCommented:
open the file in notepad and check the contents ... difference between 52 bytes and 5Kb should be easy to make out
0
 
lwinkenbAuthor Commented:
The file is 5KB.  It's a JPG.
0
 
sunnycoderCommented:
yep ... thats right ... binary files on windows need to be opened with b flag
0
 
lwinkenbAuthor Commented:
yep, that was it.  Thanks :)
0
 
PaulCaswellCommented:
>>     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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.