linux read函数的一些使用问题.

jennis19118 2013-08-23 01:56:36
我用write函数向一个文件file.c写入了四行字.
[第一行]hello,linux.
[第二行]I've learned linux for two weeks.
[第三行(是一个\n)]
[第四行]hi,this is my second time to write.
第三第四行是我第二次用lseek方法移动到之前file.c的末尾写入的.在读取的时候我却只能读到第一第二行.但是显示的字节数是85.
于是我试试移动到偏移量48的位置开始读,也就是第一次写入的前两行的字节数.这时候读取正常.

user@ubuntu:~/linuxfile$ ./read
file opened.
file closed.
the string read from that file is:
hi,this is my second time to write.the size is :37


空行没有显示,也就是说空行会被忽略掉?然后我再重新从文件头部读.

user@ubuntu:~/linuxfile$ ./read
file opened.
file closed.
the string read from that file is:hello,linux.
I've learned linux for two weeks.
the size is :85

第四行的内容没有.
这是为什么呢.求解.
...全文
255 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
AnYidan 2013-08-23
  • 打赏
  • 举报
回复
write 进程用O_APPEND
AnYidan 2013-08-23
  • 打赏
  • 举报
回复
使用了文件共享,多进程?
艾薇儿More 2013-08-23
  • 打赏
  • 举报
回复
嗨,这问题google一下就知道
jennis19118 2013-08-23
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。 不要把 fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fread,fwrite,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了
学习了
赵4老师 2013-08-23
  • 打赏
  • 举报
回复
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。 不要把 fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fread,fwrite,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了
jennis19118 2013-08-23
  • 打赏
  • 举报
回复
我的代码readfile.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main()
{
        char *path="../linuxtest/file.c";
        char s[1024];
        int fd;
        extern int errno;
        int size;

        fd=open(path,O_RDWR,0766);
        if(fd!=-1)
        {
        printf("file opened.\n");
        }
        else
        {
        printf("failed to open this file.  PATH:%s\n",path);
        printf("Error number:%d\n",errno);
        printf("Error message:%s\n",strerror(errno));
        }
        lseek(fd,0,SEEK_SET);
        size = read(fd,s,sizeof(s));

        if(close(fd)==0)
        {
        printf("file closed.\n");
        }
        else
        {
        printf("cannot close the file.  PATH:%s\n",path);
        printf("Error number:%d\n",errno);
        printf("Error message:%s\n",strerror(errno));
        }

        printf("the string read from that file is:%s",s);
        printf("the size is :%d\n",size);

}
fileopen.c(之前是测试open()函数的,后来用来写数据到文件)
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main()
{
        int fd,fd1;
        char path[]="../linuxtest/file.c";
        extern int errno;
        char buf[20];
        char s[]="hello,linux.\nI've learned linux for two weeks.\n";
        char s1[]="\nhi,this is my second time to write.";
        fd = open(path,O_WRONLY,0766);
        if(fd!=-1)
        {
        printf("file sucessfully opened. path:%s\n",path);
        }
        else
        {
        printf("cannot open the file. path:%s\n",path);
        printf("errno:%d\n",errno);
        printf("ERR:%d\n",strerror_r(errno,buf,20));
        }

        fd1 = open(path,O_WRONLY|O_CREAT,0766);
        if(fd1!=-1)
        {
        printf("file successfully opened. path:%s\n",path);
        }
        else
        {
        printf("cannot open the file. path:%s\n",path);
        printf("errno:%d\n",errno);
        printf("ERR:%d\n",strerror_r(errno,buf,20));
        }

        write(fd,s,sizeof(s));
        lseek(fd,0,SEEK_END);
        write(fd,s1,sizeof(s1));
        sync();
 if(close(fd)==0)
        {
        printf("file closed. path:%s\n",path);
        }
        else
        {
        printf("cannot close the file. path:%s\n",path);
        printf("errno:%d\n",errno);
        printf("error message:%s\n",strerror(errno));
        }

        if(close(fd1)==0)
        {
        printf("file closed.  path:%s\n",path);
        }
        else
        {
        printf("cannot close the file. path:%s\n",path);
        printf("errno:%d\n",errno);
        printf("error message:%s\n",strerror(errno));
        }

        printf("Done.\n");

}

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧