Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

problem with files

13 views
Skip to first unread message

Jean-Louis Noel

unread,
Jan 8, 2012, 11:19:36 AM1/8/12
to
Hi,

I try to copy a designed amount of data from one file to another.

==========
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define UI32 unsigned __int32
#define FN_MAX_SIZE 64 // max file path/name

void
main(int argc, char *argv[])
{
UI32 i;
char inpname[FN_MAX_SIZE], outname[FN_MAX_SIZE];
FILE *finp, *fout;
if (argc != 3)
{
printf("%s\n", "Usage: ");
exit (1);
}

/* Store arguments */
strncpy(inpname, argv[1], FN_MAX_SIZE);
strncpy(outname, argv[2], FN_MAX_SIZE);

/* Open input file */
finp = fopen(inpname,"r");
if (finp == NULL) exit(2);

/* Open output file */
fout = fopen(outname,"w");
if (fout == NULL) exit(3);

i = 2934129;

while(i)
{
fputc(getc(finp),fout);
i--;
}

fclose(finp);
fclose(fout);
}
==========

The program wrote 2939145 bytes of data.
I don't understand where I made a mistake.

Thanks for your help.

Bye,
Jean-Louis

Johann Klammer

unread,
Jan 8, 2012, 4:19:23 PM1/8/12
to
Jean-Louis Noel wrote:
> The program wrote 2939145 bytes of data.
> I don't understand where I made a mistake.
>
> Thanks for your help.
>

Some suggestions...
Did you check for EOF?
What will fputc do in that case?
Some platforms treat binary and text files differently.
Not sure if this is one of those.
Try to fopen "rb" and "wb". Perhaps it helps.
Where is the 2939145 from? Does it include some kind of overhead?
...

Peter C. Chapin

unread,
Jan 9, 2012, 6:54:58 AM1/9/12
to
On 2012-01-08 11:19, Jean-Louis Noel wrote:

> i = 2934129;
>
> ...
>
> The program wrote 2939145 bytes of data.
> I don't understand where I made a mistake.

Probably because you have the files open in text mode. Thus any 0x0A
bytes in the input are being converted to 0x0D 0x0A combinations in the
output. (That is, '\n' is being converted to "\r\n" pairs). Notice that
your output size is slightly larger.

Try opening the files in binary mode using "rb" and "wb."

Peter

Jean-Louis Noel

unread,
Jan 9, 2012, 7:44:37 AM1/9/12
to
Hi,

"Peter C. Chapin" <PCh...@vtc.vsc.edu> wrote in message news:jeekj5$dt9$1...@www.openwatcom.org...

> Try opening the files in binary mode using "rb" and "wb."

Thanks guys it worked!

Bye,
Jean-Louis
0 new messages