473,404 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

reading a unformatted file

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
Nov 14 '05 #1
8 7429
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.

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
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #2
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.

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


Nov 14 '05 #3
nanolucifer wrote:
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
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.
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


--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #4
On Thu, 27 Jan 2005 04:20:08 -0800, nanolucifer wrote:
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
Nov 14 '05 #5
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

Nov 14 '05 #6
nanolucifer wrote:

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.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #7
Vijay wrote:
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
Nov 14 '05 #8
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.

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;
}
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mitch | last post by:
Hi all, I hope this is the right place for this post, apologies if it is not. I am trying to write a program that creates an input file for a dispersion model. The model is expecting a Fortran...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
2
by: Matt McGonigle | last post by:
Hi all, Please help me out with this. Perhaps it is a dumb question, but I can't seem to make it work. I am doing a file conversion using an unformatted binary file for input and outputting to...
1
by: Andrea Gavana | last post by:
Hello NG, that may sound a silly question, but I didn't find anything really clear about the issue of reading unformatted big endian files with Python. What I was doing till now, was using...
7
by: cmfvulcanius | last post by:
I am using a script with a single file containing all data in multiple sections. Each section begins with "#VS:CMD:command:START" and ends with "#VS:CMD:command:STOP". There is a blank line in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.