Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying one file to another

Status
Not open for further replies.

countdrak

Programmer
Jun 20, 2003
358
0
0
US
I am doing this just as an exercise coz I ran into this problem in something I was doing.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	FILE *from, *to;
	char file1[100], file2[100], buffer[8192];


	printf("Name of file to be copied: ");
	gets(file1);
	printf("\nName of file to be copied to: ");
	gets(file2);

	from = fopen(file1, "r");
	to = fopen(file2, "w");


	while (!feof(from))
	{
		fread(buffer, sizeof(char), 8192, from);
		fwrite(buffer, sizeof(char), 8192, to);
	}

	fclose(from);
	fclose(to);
	return 0;
}

Now this might look simple enough, but its not, coz im trying to copy a 600MB file bit by bit, but it only makes a 4kb or 5kb or 9kb file depending on the buffer size I give.

Why is this happening? And how to correct it?
 
Ok I guess you can write something like this.

Code:
#include <stdio.h>
#include <stdlib.h>

int main () {
  FILE * pFile, *wFile;
  long lSize;
  char * buffer;

  pFile = fopen ( "023LW.rf" , "rb" );
  wFile = fopen ( "copied.txt", "w");
  if (pFile==NULL) exit (1);

  // obtain file size.
  fseek (pFile , 0 , SEEK_END);
  lSize = ftell (pFile);
  rewind (pFile);

  // allocate memory to contain the whole file.
  buffer = (char*) malloc (lSize);
  if (buffer == NULL) exit (2);

  // copy the file into the buffer.
  fread (buffer,1,lSize,pFile);
  fwrite(buffer, 1, lSize, wFile);

  /*** the whole file is loaded in the buffer. ***/

  // terminate
  fclose (pFile);
  fclose (wFile);
  free (buffer);
  return 0;
}

But I want to pick up a chunk of values, process, dump them and repeat the process again until the end of file. But it seems !feof(FILE *) does not work as it ends the file early.

Why oh why!
 
Ha ha ha! Sorry everyone...had to open the file in "rb" mode. Everything is working now.

Great!
 
Bear in mind that in your second example, you are in effect malloc'ing a serious amount of memory (you mention 600 MB files) - and you run the risk of hogging system resources, and going into swap.

Much better IMO to read and write the files in chunks, and ease up on the RAM :)

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Some additions:
1. Open target file in "wb" mode.
2. Test if a target file was opened (need permissions to open an existent file for rewriting). Check fread() result too (i/o errors?).
3. See sedj's remark: it's a serious problem.
4. Press <Ctrl-Z> <Enter> instead of file name or type a long (long;) file name... Avoid gets where possible (always use fgets;).
 
> while (!feof(from))
Tell me, is your output file bigger than your input file?

Try this
Code:
size_t n;
while ( (n=fread(buffer, sizeof(char), sizeof(buffer), from)) != 0 ) {
  if ( fwrite(buffer, sizeof(char), n, to) != n ) {
    /* failed to write - disk full? */
  }
}


> gets(file1);
Oh the humanity!!!
Use fgets() and be safe.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top