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 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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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?
...
|
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...
|
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)...
|
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.
...
|
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...
|
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...
...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |