这个错在哪里啊?

[复制链接]
1139|6
 楼主| limee 发表于 2019-7-17 19:57 | 显示全部楼层 |阅读模式
#include <stdio.h>
#include <unistd.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

int main()
{
   char c;
   int in,out;
   int rc;
   in = open("file1",O_RDONLY);
   out = open("file2",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
   while( ( rc = read(in,&c,1)) == 1)
   {
      printf("%d.\n",rc);
      write(out,&c,1);
   }
   exit(0);
}

想把file1复制到file2。源文件46k,实际只复制了4k。
mingw编译的。

网上查的资料说,文件很快读完了,所以读到字节不正确了。


dirtwillfly 发表于 2019-7-17 22:13 | 显示全部楼层
本帖最后由 dirtwillfly 于 2019-7-17 22:14 编辑

文件里是不是有特殊字符,导致了while里变为假?
gy168 发表于 2019-7-18 09:42 | 显示全部楼层
二进制打开试试
 楼主| limee 发表于 2019-7-18 11:44 | 显示全部楼层
dirtwillfly 发表于 2019-7-17 22:13
文件里是不是有特殊字符,导致了while里变为假?

的确,是^Z 字符引起的,不知道如何解决?
dirtwillfly 发表于 2019-7-19 08:28 | 显示全部楼层
limee 发表于 2019-7-18 11:44
的确,是^Z 字符引起的,不知道如何解决?

那个地方应该用判断是否到文件结尾,而不是看读到的字符
qt8867 发表于 2019-7-19 09:15 | 显示全部楼层
文件结束符是EOF,(实际上是-1),判断条件有问题;在实际编程的时候可以引用EOF特殊字符,类似这种:Read(in,&c,1))!= EOF
R2D2 发表于 2019-7-19 14:45 | 显示全部楼层
in = open("file1",O_RDONLY);这句要加上BINARY属性。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

301

主题

475

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部