473,385 Members | 1,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to rewind to file beginning immediately after writing...

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

Jul 20 '06 #1
3 4036
Sorry folks, fixed it - a simple case of changing O_WRONLY to O_RDWR ..
duh :(
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);
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
Jul 20 '06 #2
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
Jul 20 '06 #3
Thanks for pointing me to the appropriate group, Gordon!

~Rajorshi

Flash Gordon wrote:
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
Jul 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Thierry Lam | last post by:
Let's say I already wrote a file and have the following: testing testing testing testing testing testing Is there an easy way to write something of variable length at the top of the file? ...
5
by: anas.hashmi | last post by:
I am trying to write to the beginning of a file. The reason: I want to make a form where board webmasters can use it to insert in updates to a webpage without having to go directly into the web...
4
by: **Developer** | last post by:
Assume that the first line of a file is read with: InputLine = LineInput(FileNum) I believe a file can be rewound as follows: (So that the first line can be reread) FileClose(FileNum)...
4
by: Neo | last post by:
I am writing an encryption program. I am using fopen , fgetc and fput c to open, read a block of data and write a block of encrypted. Thus all the blocks are overwritten with the encrypted. ...
0
by: Charles D Hixson | last post by:
I was reading through old messages in the list and came up against an idea that I thought might be of some value: "Wouldn't it be a good idea if one could "rewind" an iterator?" Not stated in...
24
by: Olaf \El Blanco\ | last post by:
Where is exactly neccesary to put "rewind(stdin)"? After a printf? Before a scanf? I have a lot of problems with strings... If somebody know any good document, please let me know where is... ...
13
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them...
5
by: gp | last post by:
i am implementing Iterator in a class, I have pretty much copied the code from php.net on Object Iteration. Adding all the normal methods for the task...rewind, current, next, etc. I was...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.