Hi,
Right now I experience some difficulties using int write( int, char*, int );
In the simple program that I have attached I first move to a location within
a file using long lseek(int, long, int) and then I write some dummy data
using the int write( int, char*, int ) function.
I am doing that under Solaris, and for unknown reason the dummy string
that I am trying to write goes not at the pre-set location, but instead
goes to the very beginning of the file. The file size though is updatedA
correctly, if I move to a location that is currently outside the file
range.
Thanks,
mich
On Thu, 19 Nov 1998, Glynn Clements wrote:
>
> Mihail Mihaylov wrote:
>
> > Is there a way in C++ ( using the iostream.h ) or in C ( using the
> > stdio.h and the like ) to control precisely how
> > data gets saved to a file. What I mean by this is, can I control exactly
> > at which offset within the file my data blocks will go.
>
> Sure. Use fseek() to set the offset before writing the data.
>
> If you're not writing any data sequentially, it's more efficient to
> either use the Unix functions (open, lseek, write, close), or to
> disable buffering with e.g. setbuf(fp, NULL).
>
> --
> Glynn Clements <[EMAIL PROTECTED]>
>
#include <sys/file.h>
#include <sys/fcntl.h>
#include <fcntl.h>
#include <iostream.h>
#include <unistd.h>
int main( void )
{
int fd ;
long location ;
long offset = 512;
if ( ( fd = open( "save.txt", O_WRONLY | O_CREAT,
0644 ) ) < 0 ) {
cerr<<"ClientGlobals; Error openening file: " << "save.txt" << endl;
exit( 1 );
}
char buf[17];
lseek( fd, offset, L_SET);
write( fd, "string1", 7 );
close( fd );
return 0;
}