|
Home > Archive > Unix Programming > September 2005 > problem w/ function write(...)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
problem w/ function write(...)
|
|
| ferbar 2005-09-28, 2:50 am |
| Hello All,
I am writing a program that compares file access patterns.. and I'm
trying to do a write, w/ no success..The problem is in the open() for
writing function.. I work under cygwin.. Here's what I have..
Any help appreciated!!
Thanks,
FBM
______________________________________
int openWtest(char *pathnameX) {
int fd;
fd = open(pathnameX, O_RDWR|O_CREAT|S_IWGRP);
if (fd < 0) {
perror("desc:");
exit(fd);
}
return fd;
}
.... main()..
/* Write tests */
char *output="./tmp/output.txt";
fd_output = openWtest(output);
write1test(fdo1, fd_output, bufread, blocksize1, output);
| |
| Maxim Yegorushkin 2005-09-28, 7:58 am |
|
ferbar wrote:
> fd = open(pathnameX, O_RDWR|O_CREAT|S_IWGRP);
You messed up flags. It sould be:
open(pathnameX, O_RDWR | O_CREAT, S_IWGRP);
| |
| ferbar 2005-09-28, 7:58 am |
| Maxim Yegorushkin wrote:
> ferbar wrote:
>
>
>
> You messed up flags. It sould be:
>
> open(pathnameX, O_RDWR | O_CREAT, S_IWGRP);
Thanks.. that helped. However, I still having problems.. when the file
exist, now I get a permission denied message. I change the instruction
with this:
[vbcol=seagreen]
With user rights now works until de point it creates the file... but
the second time I execute it, I get an error. So I changed it for
this..
[vbcol=seagreen]
But now I get an error if the file exists.
Also, when I try to write with this function, does not work either..
let me show it..
void write1test(int fd, int fd_o, void *buffer, int block_size, char
*pathnameX) {
ssize_t bytesw, bytesr;
double t, t_t=0;
size_t nbytes;
while (bytesr != 0) {
bytesr = read(fd, buffer, block_size);
t = now();
bytesw = write(fd_o, buffer, nbytes);
t = now() - t;
t_t += t;
if (bytesw < 0) {
perror("write:");
exit(bytesw);
}
}
printf("time to write blocksizes %d to file %s: %g\n", block_size,
pathnameX, t_t);
}
fd is a file desc. of an open file, pointing to the beginning..
now() gets time of the day..
Thanks!
FBM
| |
| Fletcher Glenn 2005-09-28, 6:02 pm |
| ferbar wrote:
> Maxim Yegorushkin wrote:
>
>
>
> Thanks.. that helped. However, I still having problems.. when the file
> exist, now I get a permission denied message. I change the instruction
> with this:
>
>
>
>
> With user rights now works until de point it creates the file... but
> the second time I execute it, I get an error. So I changed it for
> this..
>
>
>
>
> But now I get an error if the file exists.
>
> Also, when I try to write with this function, does not work either..
> let me show it..
>
> void write1test(int fd, int fd_o, void *buffer, int block_size, char
> *pathnameX) {
>
> ssize_t bytesw, bytesr;
> double t, t_t=0;
> size_t nbytes;
> while (bytesr != 0) {
> bytesr = read(fd, buffer, block_size);
> t = now();
> bytesw = write(fd_o, buffer, nbytes);
> t = now() - t;
> t_t += t;
> if (bytesw < 0) {
> perror("write:");
> exit(bytesw);
> }
> }
> printf("time to write blocksizes %d to file %s: %g\n", block_size,
> pathnameX, t_t);
> }
>
> fd is a file desc. of an open file, pointing to the beginning..
> now() gets time of the day..
>
> Thanks!
>
> FBM
>
S_IWUSR and S_IWGRP both only set the write permission on the file
that is created. You also need read permission to access a file.
Try creating the file with S_IRUSR + S_IWUSR.
--
Fletcher Glenn
| |
| Floyd L. Davidson 2005-09-28, 6:02 pm |
| "ferbar" <fbarsoba@gmail.com> wrote:
>Maxim Yegorushkin wrote:
>
>Thanks.. that helped. However, I still having problems.. when the file
>exist, now I get a permission denied message. I change the instruction
>with this:
>
That gives the user write permission... but not read permission.
Before you were giving the group write permission only. But in
neither case does the user have read permission too.
[vbcol=seagreen]
>With user rights now works until de point it creates the file... but
>the second time I execute it, I get an error. So I changed it for
>this..
>
>
>But now I get an error if the file exists.
Try this:
fd = open(pathnameX, O_RDWR | O_APPEND | O_CREAT, (S_IWUSR | S_IRUSR);
With that you will creat the file if it does not exist, it will
be in append mode, and will have read and write permissions for
the owner only.
If you are going to write to one fd and read from another, you
should probably open the first as O_WRONLY and the second as
O_RDONLY, though using O_RDWR will not harm anything.
>Also, when I try to write with this function, does not work either..
>let me show it..
>
>void write1test(int fd, int fd_o, void *buffer, int block_size, char
>*pathnameX) {
>
> ssize_t bytesw, bytesr;
> double t, t_t=0;
> size_t nbytes;
nbytes is not initialized.
> while (bytesr != 0) {
> bytesr = read(fd, buffer, block_size);
> t = now();
> bytesw = write(fd_o, buffer, nbytes);
So how many bytes is this going to write? Undetermined...
> t = now() - t;
> t_t += t;
> if (bytesw < 0) {
> perror("write:");
> exit(bytesw);
The value of bytesw is guaranteed to be negative here, but
exit() needs an argument that is an unsigned char value, between
0 and 255.
It is the exit status returned to the shell, which *will* be a
value from 0 to 255, regardless of the argument you give to
exit(). In other words, a -1 will cause a return status of 255,
and a 256 will cause a return status of 0. There is no harm in
providing values outside the range of 0-255, but it will not be
of any benefit either, and merely confuses the potential value
in having a meaningful exit status for your program.
> }
> }
> printf("time to write blocksizes %d to file %s: %g\n", block_size,
>pathnameX, t_t);
>}
>
>fd is a file desc. of an open file, pointing to the beginning..
>now() gets time of the day..
You might be better off keeping track of time using a long int rather
than a double?
--
Floyd L. Davidson <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@apaflo.com
| |
| ferbar 2005-09-28, 6:02 pm |
| Floyd L. Davidson wrote:
> "ferbar" <fbarsoba@gmail.com> wrote:
>
> That gives the user write permission... but not read permission.
> Before you were giving the group write permission only. But in
> neither case does the user have read permission too.
>
>
> Try this:
>
> fd = open(pathnameX, O_RDWR | O_APPEND | O_CREAT, (S_IWUSR | S_IRUSR);
>
> With that you will creat the file if it does not exist, it will
> be in append mode, and will have read and write permissions for
> the owner only.
>
> If you are going to write to one fd and read from another, you
> should probably open the first as O_WRONLY and the second as
> O_RDONLY, though using O_RDWR will not harm anything.
>
>
> nbytes is not initialized.
>
Right! That was the problem, thanks.
>
> So how many bytes is this going to write? Undetermined...
>
>
> The value of bytesw is guaranteed to be negative here, but
> exit() needs an argument that is an unsigned char value, between
> 0 and 255.
>
> It is the exit status returned to the shell, which *will* be a
> value from 0 to 255, regardless of the argument you give to
> exit(). In other words, a -1 will cause a return status of 255,
> and a 256 will cause a return status of 0. There is no harm in
> providing values outside the range of 0-255, but it will not be
> of any benefit either, and merely confuses the potential value
> in having a meaningful exit status for your program.
>
Ok, I understand..
}
>
> You might be better off keeping track of time using a long int rather
> than a double?
>
I needed double for the precision. But I may be wrong about which is
better...
>
> --
> Floyd L. Davidson <http://www.apaflo.com/floyd_davidson>
> Ukpeagvik (Barrow, Alaska) floyd@apaflo.com
|
|
|
|
|