LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-21-2005, 10:13 AM   #1
skie_knite007
Member
 
Registered: Dec 2004
Location: India
Distribution: Fedora Core 4
Posts: 145

Rep: Reputation: 15
Copy program not working


Something is wrong with this program,,,I don't kno wat.............

When i run it,,,I'm getting a file of only 0 bytes.
Pls help out.

Code:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>

char buffer[2048];



int main(int argc,char *argv[])
{
int fdnew,fdold;
if(argc!=3)
{
printf("\nNeed two arguments for the copy program.");
exit(0);
}
fdold=open(argv[1],O_RDONLY);
if(fdold<0)
{
printf("\nCanoot open the designated file.Check permissions.");
exit(0);
}
fdnew=creat(argv[2],0666);
if(fdnew<0)
{
printf("\nCannot create the designated file.There might not be enough disk space.");

}

int count;
while((count==read(fdold,buffer,sizeof(buffer)))>0)
{
write(fdnew,buffer,count);
}


exit(0);
}
 
Old 08-21-2005, 10:41 AM   #2
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
The code is doing ONLY what you tell it to do.
Make a flow chart of what is suppose to happen.
 
Old 08-21-2005, 10:44 AM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,797

Rep: Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943
Also, you should close() your opened files at the end of the operation
 
Old 08-28-2005, 04:53 AM   #4
zeropash
Member
 
Registered: Apr 2003
Location: Bangalore,India
Distribution: FC2, RHES, RH9, FC3, FC1, Slackware 3.0
Posts: 208

Rep: Reputation: 30
as keefaz told its a good practice to do the corresponding cleanup operation always.
btw are you sure you want == here?

Code:
while((count==read(fdold,buffer,sizeof(buffer)))>0)
though writing multiple statements in a single line looks cool try to avoid it or run into problems like these. Also while posting indend the stuff. make it little easier for the person trying to go trhough your posted code.

Last edited by zeropash; 08-28-2005 at 04:56 AM.
 
Old 08-28-2005, 02:13 PM   #5
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
Originally posted by zeropash
as keefaz told its a good practice to do the corresponding cleanup operation always.
btw are you sure you want == here?

Code:
while((count==read(fdold,buffer,sizeof(buffer)))>0)
though writing multiple statements in a single line looks cool try to avoid it or run into problems like these.
Well spotted zeropash, straight to the heart of the problem. count is undefined, so the likelihood of read() returning the same value to yield an expression value of true (that being >0) is less than your chance of winning the Lotto. An assignment would, of course, do the trick nicely.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How can we copy the program code wriiten in notepad to linux vi editor? mayank027 Linux - General 2 10-25-2005 06:14 AM
Looking for a program that will copy BDG CDs Tux_Phoenix Linux - Software 0 02-03-2005 10:51 PM
Exercise 1-9. Write a program to copy its input to its output, replacing each string zombi3 Programming 3 12-21-2003 02:28 AM
Copy and Paste Not Working. rvijay Linux - Newbie 3 09-16-2003 09:49 AM
Copy program CropA Programming 2 03-20-2001 10:27 PM

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

All times are GMT -5. The time now is 02:20 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