| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
Reworld,下班在家制作游戏,1500万奖金等你拿以码会友 以友辅仁
共有 161 人关注过本帖
标题:有个地方没有看懂
只看楼主 加入收藏
chaw899
Rank: 2
等 级:禁止访问
帖 子:48
专家分:11
注 册:2018-11-29
结帖率:88.89%
  问题点数:0  回复次数:2   
有个地方没有看懂
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>

int main()
{
  char block[1024];
  int in,out;
  in = open("file1",O_RDONLY);
  out = open("file8",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
  char buf[1024];

  int bytes_to_read;
 
    for (;bytes_to_read;)     // 这里和 for(;;) 和 while(1) 没有区别吗?
    {
        int read_chunk = bytes_to_read > sizeof(buf) ? sizeof(buf) : bytes_to_read;
        int bytes_read = read(in, buf, read_chunk);
        printf("%d\n",read_chunk);
        printf("%d\n",bytes_read);

        if (bytes_to_read < 0)
        {
            printf("Error reading context.\n");
        }
        printf("Test%d\n",read_chunk);
        printf("Test%d\n",bytes_read);
        bytes_to_read -= read_chunk;
    }
  exit(0);
}
搜索更多相关主题的帖子: include int out open printf 
2019-07-17 21:25
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:209
帖 子:6709
专家分:29702
注 册:2014-5-20
  得分:0 
  int bytes_to_read;
  for (;bytes_to_read;) 这时bytes_to_read的值是什么?
2019-07-18 05:16
rjsp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:316
帖 子:6563
专家分:38038
注 册:2011-1-18
  得分:0 
程序代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

 

int main( void )
{
    int ret = 0;

    int in = open( "file1", O_BINARY|O_SEQUENTIAL|O_RDONLY );
    if( in != -1 )
    {
        int out = open( "file8", O_BINARY|O_SEQUENTIAL|O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR );
        if( out != -1 )
        {
            for( ; ; )
            {
                char buf[1024];

                int rc = read( in, buf, (unsigned)sizeof(buf) );
                if( rc < 0 )
                {
                    puts( "failed to read from file8." );
                    ret = 3;
                    break;
                }
                if( rc == 0 )
                    break;

                int wc = write( out, buf, rc );
                if( wc != rc )
                {
                    puts( "failed to write to file8." );
                    ret = 4;
                    break;
                }
            }
            if( ret == 0 )
                puts( "ok" );

            close( out );
        }
        else
        {
            puts( "failed to open file8." );
            ret = 2;
        }

        close( in );
    }
    else
    {
        puts( "failed to open file1." );
        ret = 1;
    }

    return ret;
}



[此贴子已经被作者于2019-7-18 09:23编辑过]

2019-07-18 09:21
快速回复:有个地方没有看懂
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.061631 second(s), 8 queries.
Copyright©2004-2019, BCCN.NET, All Rights Reserved