Solved

copy file into buffer

Posted on 2004-08-09
12
826 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
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 nested-loops 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.

840 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