LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1567|回复: 2

C中read的问题?

[复制链接]
发表于 2002-8-21 22:46:52 | 显示全部楼层 |阅读模式
用read读取文件内容后反回的是读取字节,但如何显示读取的内容呀,用printf?
 楼主| 发表于 2002-8-22 11:23:28 | 显示全部楼层
解决了,下面是个例子:
/*this a test to read a file content*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main (void)
{
        char name[20];
        char buf[60];
        int b;
        int c;
        int d;
        printf ("input the name of the file:");
        scanf ("%s",name);
        printf ("input the streamsize you want:");
        scanf ("%d",&c);
        open (name,O_RDONLY);
        b=open (name,O_RDONLY);
        if (b==-1)
        {
                printf ("Open Error!\n");
        }
        read (b,buf,c);
        d=read (b,buf,c);
        if (d==-1)
        {
                printf ("Read Error!\n");
        }
        printf ("the content you want is:\n");
        printf ("%s\n",buf);
        close (b);
}
发表于 2002-9-15 10:08:52 | 显示全部楼层
read的返回值是读取的字节数,read出来的内容放在一个你自己定义的BUFFER中。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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