Re: reading a unformatted file

From: Thomas Matthews (Thomas_MatthewsSpamBotsSuck_at_sbcglobal.net)
Date: 01/27/05


Date: Thu, 27 Jan 2005 15:21:01 GMT

Vijay wrote:
> Hi.
> I have a binary file which is on a CD it has filesize around
> 300 MB. Its not a text file. I want to read it and copy its
> content to a new file on hard disk. I dont want to use
> filecopy. I want to open it and read its content and copy to
> the file which is created on the harddisk. How can I do this.
> fgets will require specifica no allocated to buffer.
> Please check my logic . please dont mind syntax or improper function names.

In general, file copying is best handled by the operating system.
If you need to copy a file, you can use the "system" function
to pass commands to the operating system. Most operating systems
are designed to be efficient with file copying. Running a program
to copy a file may add another layer of overhead to the process.

> I tried with.
> fp1 = fopen (file on cd in 'r')
> fp2 = fopen (create a file on HDD in 'w' mode)
> while(!feof(fp1))
> {
> int ch = fgetch(fp1);
> write to fp2;
> }
> fclose(fp1) fclose(fp2)
>
> but i found that in the middle it gets out. it doesnt read the file fully.
> is there any other way which is more convenient.
> I dont know the format in which data is stored.
> thanks
> vijay

To copy a file, use fread, fwrite and open the
files in binary mode.

In general, the algorithm is:
   while not end of input file do
     read a chunk of input file.
     write the chunk to the output file.
     End-While

Generally speaking, the larger the chunk size,
the faster the program. However, disk drives
may have caches on them. Also, if you ask for
too much memory, the operating system may swap
the memory to a harddrive, which kind of defeats
the efficieny. Also, if your program is swapped
out during the copy operation, your program will
operate more slowly than using an operating system
function.

See also DMA Controllers.

-- 
Thomas Matthews
C++ newsgroup welcome message:
          http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq:   http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
          http://www.comeaucomputing.com/learn/faq/
Other sites:
     http://www.josuttis.com  -- C++ STL Library book
     http://www.sgi.com/tech/stl -- Standard Template Library


Relevant Pages

  • Re: Interview Questions
    ... This depends on the platform and maybe the Operating System. ... implementation to have a stack or heap. ... C++ Faq: http://www.parashift.com/c++-faq-lite C Faq: http://www.eskimo.com/~scs/c-faq/top.html alt.comp.lang.learn.c-c++ faq: ...
    (comp.lang.cpp)
  • Re: Beginner needs help fast
    ... define a class 'std::moneypunct' for parsing and generating character ... should be asked in groups dedicated to the relevant compiler. ... Likewise for development environment, operating system, libraries and so on. ... The FAQ has much to say on this and other things. ...
    (comp.lang.cpp)
  • Re: how to determine a file is ASCII or binary?
    ... There's no "ASCII" in C. ... the operating system might do something to the data as it is written to the ... In that case, the encoding needs to ... > Since the OS look both ASCII and binary file as a sequence of bytes, ...
    (comp.lang.c)
  • Re: How to trigger an event every 5 minutes
    ... Jon Zero wrote: ... tell the operating system to execute it every X minutes. ... minutes to the time and save as the update time. ... C++ Faq: http://www.parashift.com/c++-faq-lite ...
    (comp.lang.c)
  • Re: A basic question about running a program
    ... programs turns on the light then enters a forever wait loop. ... Is this program an operating system or a user program? ... The system has an Interrupt Service Routine that inverts ... C++ Faq: http://www.parashift.com/c++-faq-lite C Faq: http://www.eskimo.com/~scs/c-faq/top.html alt.comp.lang.learn.c-c++ faq: ...
    (alt.comp.lang.learn.c-cpp)