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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-21-2005, 10:13 AM
|
#1
|
Member
Registered: Dec 2004
Location: India
Distribution: Fedora Core 4
Posts: 145
Rep:
|
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);
}
|
|
|
08-21-2005, 10:41 AM
|
#2
|
Member
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215
Rep:
|
The code is doing ONLY what you tell it to do.
Make a flow chart of what is suppose to happen.
|
|
|
08-21-2005, 10:44 AM
|
#3
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,797
|
Also, you should close() your opened files at the end of the operation
|
|
|
08-28-2005, 04:53 AM
|
#4
|
Member
Registered: Apr 2003
Location: Bangalore,India
Distribution: FC2, RHES, RH9, FC3, FC1, Slackware 3.0
Posts: 208
Rep:
|
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.
|
|
|
08-28-2005, 02:13 PM
|
#5
|
Member
Registered: May 2005
Posts: 378
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 02:20 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|