LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-20-2010, 03:18 AM   #1
shree24985
LQ Newbie
 
Registered: Jan 2009
Location: Mumbai
Distribution: slackware
Posts: 15

Rep: Reputation: 0
Exclamation Copying File in C ,help needed


Hello ,
I needed some assistance in copying a file in C programme.

I am doing something like this but failing to get rid of the garbage at the end of newly copied file.

Code for copying

FILE *u_fp = fopen(...);
FILE *u_fp_new = fopen(...);
char *block_ptr =
(char*)malloc((sizeof(char)*BUF_LEN_CHUNK)+1);
memset(block_ptr, '\0', sizeof(block_ptr));
int how_much_read=0,items_read=0;
do {
items_read = fread(block_ptr, BUF_LEN_CHUNK, 1, u_fp);
fflush(u_fp);
how_much_read = strlen(block_ptr);
fwrite(block_ptr, how_much_read, 1, u_fp_new);
fflush(u_fp_new);
}while( items_read != 0 );


Any assistance would be much appreciated .
 
Old 04-20-2010, 04:00 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by shree24985 View Post
Hello ,
I needed some assistance in copying a file in C programme.

I am doing something like this but failing to get rid of the garbage at the end of newly copied file.

Code for copying

FILE *u_fp = fopen(...);
FILE *u_fp_new = fopen(...);
char *block_ptr =
(char*)malloc((sizeof(char)*BUF_LEN_CHUNK)+1);
memset(block_ptr, '\0', sizeof(block_ptr));
int how_much_read=0,items_read=0;
do {
items_read = fread(block_ptr, BUF_LEN_CHUNK, 1, u_fp);
fflush(u_fp);
how_much_read = strlen(block_ptr);
fwrite(block_ptr, how_much_read, 1, u_fp_new);
fflush(u_fp_new);
}while( items_read != 0 );


Any assistance would be much appreciated .
You are using a wrong loop type, i.e. you are checking condition after writing, though you should write only in case you've read something meaningful.
 
Old 04-20-2010, 10:48 AM   #3
shree24985
LQ Newbie
 
Registered: Jan 2009
Location: Mumbai
Distribution: slackware
Posts: 15

Original Poster
Rep: Reputation: 0
thankyou Sergei .

using while loop solved it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
strange (to me) behavior of file permissions when copying file warrob Linux - General 10 10-25-2009 05:13 AM
copying a file enrique_arong Linux - Newbie 5 07-29-2004 12:21 PM
Question on File System and File Copying hey_joe Linux - Newbie 2 11-05-2003 10:30 AM
Copying a File... Nefarious Linux - General 3 11-28-2002 12:23 AM
Copying a file tgonetwork Linux - Newbie 3 01-16-2002 10:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration