| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1285 人关注过本帖
标题:有个地方没有看懂
只看楼主 加入收藏
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: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10527
专家分:42899
注 册:2014-5-20
收藏
得分:0 
  int bytes_to_read;
  for (;bytes_to_read;) 这时bytes_to_read的值是什么?
2019-07-18 05:16
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册: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.014889 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved