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

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.  
0
lwinkenb
Asked:
lwinkenb
  • 5
  • 4
  • 2
  • +1
2 Solutions
 
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
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
sunnycoderCommented:
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
 
ankuratvbCommented:
You might need to open the file in binary mode using "rb" in fopen()
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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