Jump to content

copying binary file in C

- - - - -

  • Please log in to reply
4 replies to this topic

#1
nerio

nerio

    Newbie

  • Members
  • PipPip
  • 14 posts
Hi. I am working on a program that copies file, that user enters its name, to an identical file with name output.out .

It is hard for me, because I am used to write(b,f); function from Pascal and here is fread() and there are four arguments there.

Here is my attempt:


/* not finished */

#include<stdio.h>

#include<math.h> 

main()

{

FILE *f1;

FILE *f2;


char s[20];

unsigned char b;


 printf("This program copies a given file.\n\n");

 printf("Enter file name:\n\n");

 scanf("%s",&s);

 printf("Processing..\n\n");

 f1=fopen(s,"rb"); /*Open the file Input*/

 f2=fopen("output.out","wb"); /*Open the file Output*/


 b=5;


 while( b != EOF ){

   fread(b,1,1,f1);

   fwrite(b,1,1,f2);

 }


 fclose(f1); /*close the file input*/

 fclose(f2); /*close the file output*/

 printf("Done. File output.out created.");

 getch();

}




Thank you for help.

Edited by nerio, 26 November 2010 - 03:35 PM.

I am a student of C language.

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
If you're reading strings with scanf, then you don't pass them by reference (no & sign). Would be better if you use fgets, or gets.
char s[20];
fgets(s, 20, stdin);
// or
gets(s);

Your while loop condition should be
while (f1 != EOF)
Other than that I think it ok.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
nerio

nerio

    Newbie

  • Members
  • PipPip
  • 14 posts
Thank you Flying Dutchman

this is the actual version now:

/* not finished */
#include<stdio.h>
#include<math.h> 
main()
{
FILE *f1;
FILE *f2;

char s[20];
unsigned char b;

 printf("This program copies a given file.\n\n");
 printf("Enter file name:\n\n");
 gets(s);
 printf("Processing..\n\n");
 f1=fopen(s,"rb"); /*Open the file Input*/
 f2=fopen("output.out","wb"); /*Open the file Output*/

 b=5;

while (f1 != EOF){
   fread(b,1,1,f1);
   fwrite(b,1,1,f2);
 }

 fclose(f1); /*close the file input*/
 fclose(f2); /*close the file output*/
 printf("Done. File output.out created.");
 getch();
}

but still doesn't work now..
I am a student of C language.

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Try this
rewind(f1);
fseek(f1, 0, SEEK_END);
long size = ftell(f1);
rewind(f1);

char* buffer = (char*) malloc(sizeof(char) * size);

fread(buffer, 1, size, f1);
fwrite(buffer, 1, size, f2);

free(buffer);
What this piece of code does is, first it calculates size of f1. rewind places file pointer to start of file and with fseek we move file pointer to end of file. Then with ftell we get file size so we know how many bytes we'll need to reserve. It then creates buffer of that size and reads to/writes from buffer.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

Try the following code


/* not finished */

#include<stdio.h>

#include<math.h> 

main()

{

FILE *f1;

FILE *f2;


char s[20];

unsigned char b;


 printf("This program copies a given file.\n\n");

 printf("Enter file name:\n\n");

 gets(s);

 printf("Processing..\n\n");

 f1=fopen(s,"rb"); /*Open the file Input*/

 f2=fopen("output.out","wb"); /*Open the file Output*/


 b=5;


[COLOR="Red"]while (!feof(f1))[/COLOR]{

   fread(b,1,1,f1);

   fwrite(b,1,1,f2);

 }


 fclose(f1); /*close the file input*/

 fclose(f2); /*close the file output*/

 printf("Done. File output.out created.");

 getch();

}


Hope that helps!

Munir




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users