ok Philip, i tried writing from a different function. and the code is like this. [ this is from my program ]
void write_file(char *src, char *dest){ char buff[4096]; uint16_t n,fd,ft; fd = open(src,O_RDONLY); ft = open(dest,O_CREAT | O_WRONLY,RW_EVERYONE); while(n = read(fd,buff,sizeof(buff)) > 0){ printf("\n n = %u",n); if(write(ft,buff,n) != n){ printf("\n write error"); getchar(); } getchar(); } }
-------------- RW_EVERYONE is #defined getchar() has been used to step through.
n is still 1.
i am very frustrated ( n = 1 ); can u figure out this problem... can anybody please help me out with this. also please tell me why does this happen.
__________________________________ Do you Yahoo!? Yahoo! Finance Tax Center - File online. File on time. http://taxes.yahoo.com/filing.html
On Tue, 23 Mar 2004, Linux User wrote:
while(n = read(fd,buff,sizeof(buff)) > 0){
should be:
while( (n = read(fd,buff,sizeof(buff))) > 0){
notice the extra parentheses. You were assigning the result of read > 0 to n. As long as read > 0, (read > 0) evaluates to 1.
On 23/03/04 03:44 -0800, Linux User wrote: <snip>
while(n = read(fd,buff,sizeof(buff)) > 0){
This will be parsed as while (n = (read(fd, buff, sizeof(buff)) > 0)) { The result of the bracket around read is 1, since the return value of read is > 0 and hence the condition evaluates to true.
/* Bad code follows */
/* * The OP should tell us why he/she is using read and write instead of * fread and fwrite, or perhaps rename to move the file instead */ while (fd) { n = read(fd, buff, sizeof(buff)); if (n = -1) { perror("Read failed: "); exit(n); }
if (n < sizeof(buff)) { /* Short read, eof? */ close_check = close(fd); /* Close file */ /* * We had an error while closing the file * Alert to see if the disk is full, or corrupted. */ if (close_check = -1) { perror("File close failed, disk full?: "); } } /* Insert code for writes here */ }
Devdas Bhagat
check man read. On success, the number of bytes read is returned. abhijeet
On Tue, 2004-03-23 at 18:59, Devdas Bhagat wrote:
On 23/03/04 03:44 -0800, Linux User wrote:
<snip> > while(n = read(fd,buff,sizeof(buff)) > 0){ This will be parsed as while (n = (read(fd, buff, sizeof(buff)) > 0)) { The result of the bracket around read is 1, since the return value of read is > 0 and hence the condition evaluates to true.
/* Bad code follows */
/*
- The OP should tell us why he/she is using read and write instead of
- fread and fwrite, or perhaps rename to move the file instead
*/ while (fd) { n = read(fd, buff, sizeof(buff)); if (n = -1) { perror("Read failed: "); exit(n); }
if (n < sizeof(buff)) { /* Short read, eof? */ close_check = close(fd); /* Close file */ /* * We had an error while closing the file * Alert to see if the disk is full, or corrupted. */ if (close_check = -1) { perror("File close failed, disk full?: "); } }
/* Insert code for writes here */ }
Devdas Bhagat
Sometime Today, Devdas Bhagat assembled some asciibets to say:
/*
- The OP should tell us why he/she is using read and write instead of
- fread and fwrite, or perhaps rename to move the file instead
*/
irrelevant. fread/fwrite won't work on file descriptors. They require FILE pointers. open/creat/socket return file descriptors.
while (fd) {
and who sets fd to 0?
if (n < sizeof(buff)) { /* Short read, eof? */
this is a short read. it does not mean eof. eof is signalled by a return value of 0. n < requested size just means that fewer bytes than requested were available at the time.
close_check = close(fd); /* Close file */
don't close here. close only if n == 0. If n < 0, check for errno of EINTR or EAGAIN, and redo the read.
Philip
On 23/03/04 21:01 +0530, Philip S Tellis wrote:
Sometime Today, Devdas Bhagat assembled some asciibets to say:
/*
- The OP should tell us why he/she is using read and write instead of
- fread and fwrite, or perhaps rename to move the file instead
*/
irrelevant. fread/fwrite won't work on file descriptors. They require FILE pointers. open/creat/socket return file descriptors.
The whole point is that the OP is copying a file from one place on local disk to another.
while (fd) {
and who sets fd to 0?
if (n < sizeof(buff)) { /* Short read, eof? */
this is a short read. it does not mean eof. eof is signalled by a return value of 0. n < requested size just means that fewer bytes than requested were available at the time.
Yup. In the current scenario though, this should not happen. Like I said, bad code sample.
close_check = close(fd); /* Close file */
don't close here. close only if n == 0. If n < 0, check for errno of EINTR or EAGAIN, and redo the read.
Lots more work to fix that piece of code.
Devdas Bhagat
Sometime Today, Devdas Bhagat assembled some asciibets to say:
The whole point is that the OP is copying a file from one place on local disk to another.
he was probably trying to learn how read(2) works. IAC, the original problem had to do with reading from a network and some other blah.
On 23/03/04 23:44 +0530, Philip S Tellis wrote:
Sometime Today, Devdas Bhagat assembled some asciibets to say:
The whole point is that the OP is copying a file from one place on local disk to another.
he was probably trying to learn how read(2) works. IAC, the original problem had to do with reading from a network and some other blah.
The OP was downloading a file, saving it to disk and then copying it.
Devdas Bhagat