niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C
how to rewind to file beginning immediately after writing...
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
Author Message
Rajorshi Biswas
*nix forums beginner


Joined: 24 Sep 2005
Posts: 5

PostPosted: Thu Jul 20, 2006 1:03 pm    Post subject: Re: how to rewind to file beginning immediately after writing... Reply with quote

Thanks for pointing me to the appropriate group, Gordon!

~Rajorshi

Flash Gordon wrote:
Quote:
Rajorshi Biswas wrote:
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);

open and friends are not part of standard C so you would do better
asking on comp.unix.programmer, however I would point out that if
O_WRONLY has the obvious meaning of opening the file for writing only
you will not be able to read from it.

snip

while((nread = read(fd, block, sizeof(block))) > 0) /* READ FAILS
HERE <<< */

snip

If you want further information on using open and friends, please ask in
comp.unix.programmer
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Back to top
Flash Gordon
*nix forums Guru


Joined: 28 Feb 2005
Posts: 1258

PostPosted: Thu Jul 20, 2006 7:00 am    Post subject: Re: how to rewind to file beginning immediately after writing... Reply with quote

Rajorshi Biswas wrote:
Quote:
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);

open and friends are not part of standard C so you would do better
asking on comp.unix.programmer, however I would point out that if
O_WRONLY has the obvious meaning of opening the file for writing only
you will not be able to read from it.

<snip>

Quote:
while((nread = read(fd, block, sizeof(block))) > 0) /* READ FAILS
HERE <<< */

<snip>

If you want further information on using open and friends, please ask in
comp.unix.programmer
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Back to top
Rajorshi Biswas
*nix forums beginner


Joined: 24 Sep 2005
Posts: 5

PostPosted: Thu Jul 20, 2006 6:17 am    Post subject: Re: how to rewind to file beginning immediately after writing... Reply with quote

Sorry folks, fixed it - a simple case of changing O_WRONLY to O_RDWR ..
duh Sad
Rajorshi Biswas wrote:
Quote:
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
Back to top
Rajorshi Biswas
*nix forums beginner


Joined: 24 Sep 2005
Posts: 5

PostPosted: Thu Jul 20, 2006 5:59 am    Post subject: how to rewind to file beginning immediately after writing... Reply with quote

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
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
The time now is Thu Dec 04, 2008 4:11 am | All times are GMT
navigation Forum index » Programming » C
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts FAQ 4.32 How do I strip blank space from the beginning/en... PerlFAQ Server Perl 0 Fri Jul 21, 2006 1:03 pm
No new posts Running php file everyday on scheduled time sachin PHP 1 Fri Jul 21, 2006 12:49 pm
No new posts Regarding thesaurus iso file Srikanth modules 0 Fri Jul 21, 2006 10:42 am
No new posts how can i get a file descriptor not used? mars system 0 Fri Jul 21, 2006 7:41 am
No new posts small GTK "Open file" dialog David Siroky Debian 0 Fri Jul 21, 2006 7:30 am

Loans | Teen Chat | Home Loan | Remortgages | Mortgages
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2256s ][ Queries: 20 (0.0889s) ][ GZIP on - Debug on ]