By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,378 Members | 3,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,378 IT Pros & Developers. It's quick & easy.

A question about file operation

P: n/a
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!

Jun 14 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
windstorm <li************@gmail.comwrote:
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
Jun 14 '07 #2

P: n/a
windstorm wrote:
>
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:Th*************@gmail.com>
Jun 14 '07 #3

P: n/a
On 6 14 , 8 54 , r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
windstorm <likunarmstr...@gmail.comwrote:
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!

Jun 14 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.