Solved

copy file into buffer

Posted on 2004-08-09
12
832 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
undefined reference to `bswap_128' 9 191
Inorder binary search tree 5 179
c++ substatte a varabe for a string in a LPCTSTR statment 8 95
smtp c source code 7 109
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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

730 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