QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 670|回复: 3

A problem about c .

[复制链接]
发表于 2003-4-18 15:35:15 | 显示全部楼层 |阅读模式
I writed a file after opening it.
But when i want to read from it, something is wrong.

I use the function read() to read the file.
This function  need the information about the size of the file,
which I can find two ways to get.

The first is to use the function fstat() to get the stat struct and
get the value of the st_size field.

The second is to use the function lseek() to get the length of the file.

When I use them at the same time,
these two methods turn out the same result.

But after  calling the read(), nothing is read from the file.
The buffers, dd and pp, remain empty.

It is still more strange that , when I just use the first one to read the file,
it is OK, that is ,file is read correctly.

I do know how this will happen .
The code goes like the next article.

Please explain the reason.

Thankls!
 楼主| 发表于 2003-4-18 15:35:40 | 显示全部楼层
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include &lt;stdio.h&gt;

int main()
{
        int rlt;
        extern int errno;
        char *zz="adasdfads11";
        char *dd;
        char *pp;
        int length=0;
       

        struct stat *status=(struct stat*)malloc(sizeof(struct stat));
       
        rlt=open("hehe",O_CREAT|O_RDWR);

        write(rlt,zz,strlen(zz));

        close(rlt);

        rlt=open("hehe",O_CREAT|O_RDONLY);
       
        fstat(rlt,status);
        length=lseek(rlt,0,SEEK_SET);
        length=lseek(rlt,0,SEEK_END)-length;

        dd=(char*)malloc(status-&gt;st_size+1);
        pp=(char*)malloc(length+1);
        memset((void*)dd,'\0',status-&gt;st_size);
        memset((void*)pp,'\0',length);

        fsync(rlt);
       
        if(read(rlt,dd,status-&gt;st_size)==-1)
        {
                printf("error read dd\n");
                free(status);
                free(dd);
                free(pp);
                exit(1);
        }

        fsync(rlt);
       
        if(read(rlt,pp,length)==-1)
        {
                printf("error read pp\n");
                free(dd);
                free(pp);
                free(status);
                exit(2);
        }
       
        printf("%d\n",status-&gt;st_size);
        printf("%d\n",length);
       
        printf("dd:%s\n",dd);
        printf("pp:%s\n",pp);
       
        free(status);
        free(pp);
        free(dd);
       
        close(rlt);
       
       
}
回复

使用道具 举报

发表于 2003-4-18 21:47:04 | 显示全部楼层

Re: A problem about c .

I writed a file after opening it.
But when i want to read from it, something is wrong.

I use the function read() to read the file.
This function  need the information about the size of the file,
~~read does not need the file length, pls read help about read for more info.

which I can find two ways to get.

The first is to use the function fstat() to get the stat struct and
get the value of the st_size field.
~~this is the correct way to get file length.

The second is to use the function lseek() to get the length of the file.
~~this is correct. but it has side effect that it move the file pointer to the end of file, so u can not read any more. u need call lseek again and set the file pointer to the start of file.

When I use them at the same time,
these two methods turn out the same result.

But after  calling the read(), nothing is read from the file.
The buffers, dd and pp, remain empty.
~~~u can use the return value of read to get more information. if return 0, nothing read

It is still more strange that , when I just use the first one to read the file,
it is OK, that is ,file is read correctly.

I do know how this will happen .
The code goes like the next article.

Please explain the reason.

Thankls!
回复

使用道具 举报

发表于 2003-4-18 21:52:51 | 显示全部楼层
length=lseek(rlt,0,SEEK_SET);
length=lseek(rlt,0,SEEK_END)-length;

after the second statement, the file pointer points to the end. so u need set it back again.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-16 04:32 , Processed in 0.049591 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表