Re: reading a unformatted file
From: Thomas Matthews (Thomas_MatthewsSpamBotsSuck_at_sbcglobal.net)
Date: 01/27/05
- Next message: Ben Pfaff: "Re: substitute for string 0 termination"
- Previous message: Greg Comeau: "Re: c99 on Microsoft Visual Studio"
- In reply to: Vijay: "reading a unformatted file"
- Next in thread: Martin Ambuhl: "Re: reading a unformatted file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Ben Pfaff: "Re: substitute for string 0 termination"
- Previous message: Greg Comeau: "Re: c99 on Microsoft Visual Studio"
- In reply to: Vijay: "reading a unformatted file"
- Next in thread: Martin Ambuhl: "Re: reading a unformatted file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|