ProgrammingThis 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.
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.
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);
}
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.