R
Rajorshi Biswas
Hi all,
I'm aware that this might not be a "C-specific" question, if so,
please let me know which group is the most appropriate for this kind of
question.
This is a question on unix filehandling in C, all I have to do is
rewind to the beginning of the file immediately after "write"ing some
bytes. My code looks something like this -
/* create a file and write something into it */
int fd = open("test.txt", O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
char b[100] = "this is some stupid text!\n";
write(fd, b, sizeof(b));
p = lseek(fd, 0, SEEK_SET);
printf("lseek returned = %d\n",p); /* returns 0 */
/* char block[BUFSIZ]; */
/* char tmp[] = "/tmp/fileXXXXXX"; */
newfd = mkstemp(tmp);
//copy contents of fd to newfd
while((nread = read(fd, block, sizeof(block))) > 0) /* READ FAILS
HERE <<< */
write(newfd, block, nread);
I am clueless as to why after a write to a new file, and an lseek to
position zero, a subsequent read fails. Even if I do an fsync() after
the write, it fails. I'm on RHEL3.
Thanks in advance.
Raj
I'm aware that this might not be a "C-specific" question, if so,
please let me know which group is the most appropriate for this kind of
question.
This is a question on unix filehandling in C, all I have to do is
rewind to the beginning of the file immediately after "write"ing some
bytes. My code looks something like this -
/* create a file and write something into it */
int fd = open("test.txt", O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
char b[100] = "this is some stupid text!\n";
write(fd, b, sizeof(b));
p = lseek(fd, 0, SEEK_SET);
printf("lseek returned = %d\n",p); /* returns 0 */
/* char block[BUFSIZ]; */
/* char tmp[] = "/tmp/fileXXXXXX"; */
newfd = mkstemp(tmp);
//copy contents of fd to newfd
while((nread = read(fd, block, sizeof(block))) > 0) /* READ FAILS
HERE <<< */
write(newfd, block, nread);
I am clueless as to why after a write to a new file, and an lseek to
position zero, a subsequent read fails. Even if I do an fsync() after
the write, it fails. I'm on RHEL3.
Thanks in advance.
Raj