reading a unformatted file

V

Vijay

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.

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
 
M

Michael Mair

Vijay said:
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.

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.

You say it is a binary file, so open it as such ("rb"/"wb") and use
fread() and fwrite().
If you do not know the format in which data is stored, you can use
a large buffer.

Withouth seeing your actual code, I cannot spot possible errors
on your part, so provide it or the boiled-down part which makes
problems.


Cheers
Michael
 
N

nanolucifer

1. If you are trying to read a binary file, use "rb" and "wb" modes.
2. From your code, it seems you are using fread and frwite (File
Streams) to do a single byte read and writes. 'fread' and 'fwrite' are
preferable if you want to read or write a block of data at once. For
better performance, I would suggest you use 'read' and 'write' for your
byte copying.

EnTee.
 
M

Michael Mair

nanolucifer said:
1. If you are trying to read a binary file, use "rb" and "wb" modes.
2. From your code, it seems you are using fread and frwite (File
Streams) to do a single byte read and writes. 'fread' and 'fwrite' are
preferable if you want to read or write a block of data at once. For
better performance, I would suggest you use 'read' and 'write' for your
byte copying.

It seems that the OP is using text streams and fgetc();
as the only thing required is copying the file, one can of
course use fread() and fwrite() with a large buffer.

read() and write(), common though they are, are not standard C
functions.


Please stop top-posting.


Cheers
Michael
 
L

Lawrence Kirby

1. If you are trying to read a binary file, use "rb" and "wb" modes.

That is what is likely to resolve the OP's problem.
2. From your code, it seems you are using fread and frwite (File
Streams) to do a single byte read and writes.

I don't see that anywhere.
'fread' and 'fwrite' are
preferable if you want to read or write a block of data at once.

That may be a good approach for this problem.
For
better performance, I would suggest you use 'read' and 'write' for your
byte copying.

The C language doesn't define any functions called read or write. POSIX
does but that isn't relevant here. It is unlikely fread and fwrite would
be noticably slower, indeed the buffering that C's streams provide can
sometimes result in significant performance gains.

Lawrence
 
T

Thomas Matthews

Vijay said:
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
 
C

CBFalconer

nanolucifer said:
1. If you are trying to read a binary file, use "rb" and "wb" modes.
2. From your code, it seems you are using fread and frwite (File
Streams) to do a single byte read and writes. 'fread' and 'fwrite'
are preferable if you want to read or write a block of data at once.
For better performance, I would suggest you use 'read' and 'write'
for your byte copying.

Please stop the top-posting. It is rude, especially in c.l.c.
Your answer belongs after, or intermixed with, the _snipped_
material to which you reply.

Don't suggest read and write. They are not a part of standard C,
thus are off-topic, and even if present may be inefficient due to
their lack of buffering. On many systems the best way to handle a
stream may well be with putc and getc, which can be macros
operating directly on the system buffers.
 
M

Martin Ambuhl

Vijay said:
while(!feof(fp1))
{

The above is a very common error, made mainly by Pascal programmers that
haven't yet figured out that C is not Pascal.

Please check the FAQ before posting. The key question is
http://www.eskimo.com/~scs/C-faq/q12.2.html

Question 12.2

Why does the code while(!feof(infp)) { fgets(buf, MAXLINE, infp);
fputs(buf, outfp); } copy the last line twice?

In C, EOF is only indicated after an input routine has tried to read,
and has reached end-of-file. (In other words, C's I/O is not like
Pascal's.) Usually, you should just check the return value of the input
routine (fgets in this case); often, you don't need to use feof at all.

References: K&R2 Sec. 7.6 p. 164
ANSI Sec. 4.9.3, Sec. 4.9.7.1, Sec. 4.9.10.2
ISO Sec. 7.9.3, Sec. 7.9.7.1, Sec. 7.9.10.2
H&S Sec. 15.14 p. 382

Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995 // mail feedback
 
J

Joe Wright

Vijay said:
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.

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

See how much of this you can use.

/* Binary file copy */
#include <stdio.h>
#include <stdlib.h>

void usage(void) {
puts("Usage: bcp <infile> <outfile>"), exit(EXIT_FAILURE);
}

int main(int argc, char *argv[]) {
FILE *in, *out;
size_t bytes;
unsigned char *buff;

if (argc < 3) usage();

if ((in = fopen(argv[1], "rb")) == NULL)
fprintf(stderr, "Can't open %s\n", argv[1]), exit(EXIT_FAILURE);

if ((out = fopen(argv[2], "wb")) == NULL)
fprintf(stderr, "Can't make %s\n", argv[2]), exit(EXIT_FAILURE);

if ((buff = malloc(sizeof *buff * BUFSIZ)) == NULL)
fprintf(stderr, "Can't allocate memory!"), exit(EXIT_FAILURE);

while ((bytes = fread(buff, 1, BUFSIZ, in)))
fwrite(buff, 1, bytes, out);

fclose(out);
fclose(in);
free(buff);
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top