A question about file operation

W

windstorm

int main( void )
{
int fd,size,position;
char buf[20];
char lkl[] = "hello,world!";
fd = creat( "test",0644 );
size = write( fd,lkl,sizeof(lkl) );
close(fd);
fd = open( "test",O_RDWR );
size = read( fd,buf,sizeof(buf) );
printf( "fd is %s,the length is %d\n\n",buf,size );
position = lseek( fd,70000000,SEEK_END);
size = write( fd,lk,sizeof(lkl) );
close(fd);
return 1;
}

This is just test code, I wanna see what the file will be if I make
the file pointer point to the end and move it by API lseek. That is
what you can see in the code. After a lot of tests, I found that the
file's size which can be saw by the command "ls -ls" will change
according to the lseek's second parameter distance.

For example:

If the dist is 4000 or less than 4000, the block which is filled by
file "test" will be 4K, it's reasonable for the minimal size of block
is 4K.

If the dist is 5000-480000, the block which is filled by file "test"
will be 8K, it's reasonable for the position where the second string
lk be written is out of block 1, so maybe we need two block to save
file "test".

But the follow is something I can not understand. If the dist is more
than 480000, such as 700000, the block which is filled by file "test"
will be 8.5K.

I change the dist to 7000000 direct, the block which is filled by file
"test" will be 15K. And if it is changed to 70000000, the block will
be 75K.

In my opnion, there only have two string in the file, so although
length of file will increase as I increase the distance moved by
lseek, the ACTUAL block size filled by "test" file will keep to be 8K.
But actually I am wrong. So who can explain it for me ?

Thanks very much!
 
R

Richard Bos

windstorm said:
int main( void )
{
int fd,size,position;
char buf[20];
char lkl[] = "hello,world!";
fd = creat( "test",0644 );

This is not ISO C, which is the subject of this newsgroup. It's POSIX,
or something very like it, and your question is about the POSIX parts,
not about the C parts. You need to ask this question in
comp.unix.programmer.

Richard
 
K

Kenneth Brody

windstorm said:
int main( void )
{

[... POSIX code to write() a string at the start of a file, ...]
[... followed by an lseek() to 70000000 and another write(). ...]
}

This is just test code, I wanna see what the file will be if I make
the file pointer point to the end and move it by API lseek. That is
what you can see in the code. After a lot of tests, I found that the
file's size which can be saw by the command "ls -ls" will change
according to the lseek's second parameter distance. [...]
In my opnion, there only have two string in the file, so although
length of file will increase as I increase the distance moved by
lseek, the ACTUAL block size filled by "test" file will keep to be 8K.
But actually I am wrong. So who can explain it for me ?

This isn't strictly C, as your results are really O/S and filesystem
dependent. However, search for "sparse files" (with quotes) to find
out more about what's going on, and if you need more help you need to
go to a group such as comp.unix.programmer where this might be on-topic.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
W

windstorm

windstorm said:
int main( void )
{
int fd,size,position;
char buf[20];
char lkl[] = "hello,world!";
fd = creat( "test",0644 );

This is not ISO C, which is the subject of this newsgroup. It's POSIX,
or something very like it, and your question is about the POSIX parts,
not about the C parts. You need to ask this question in
comp.unix.programmer.

Richard

oh,I am sorry about it. I have post this to comp.unix.programmer,
thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top