3758
- 收藏
- 点赞
- 分享
- 举报
关于callback function使用作用及其理解
callback function简单理解为调用函数。
C的例子來說:
#include
#include
#include
#include
#include
#include
#define DEFAULT_BLOCK_SIZE (4096)
typedef void (* CALLBACK) (int);//与定义一个callback函数
void ShowPercentage(int percentage)//定义一个Callback函数 类型与与定义一致
{
fprintf(stderr, "%dn%nn", percentage);
}
void CopyFile(const char *source, const char *target, CALLBACK callback)
{
char buf[DEFAULT_BLOCK_SIZE] ;
struct stat fs ;
int fdSrc, fdTrg ;
int readBytes = 0, totalReadBytes = 0, percentage = 0;
fdSrc = open(source, O_RDONLY);
fstat(fdSrc, &fs);
fdTrg = open(target,O_CREAT|O_TRUNC|O_RDWR);
while((readBytes=read(fdSrc, buf, DEFAULT_BLOCK_SIZE)) > 0)
{
write(fdTrg, buf, readBytes);
totalReadBytes += readBytes ;
callback( (totalReadBytes*100)/fs.st_size);
}
close(fdTrg);
close(fdSrc);
}
int main(void)
{
CopyFile("A.TXT", "B.TXT", ShowPercentage);
return 0 ;
}
C的例子來說:
#include
#include
#include
#include
#include
#include
#define DEFAULT_BLOCK_SIZE (4096)
typedef void (* CALLBACK) (int);//与定义一个callback函数
void ShowPercentage(int percentage)//定义一个Callback函数 类型与与定义一致
{
fprintf(stderr, "%dn%nn", percentage);
}
void CopyFile(const char *source, const char *target, CALLBACK callback)
{
char buf[DEFAULT_BLOCK_SIZE] ;
struct stat fs ;
int fdSrc, fdTrg ;
int readBytes = 0, totalReadBytes = 0, percentage = 0;
fdSrc = open(source, O_RDONLY);
fstat(fdSrc, &fs);
fdTrg = open(target,O_CREAT|O_TRUNC|O_RDWR);
while((readBytes=read(fdSrc, buf, DEFAULT_BLOCK_SIZE)) > 0)
{
write(fdTrg, buf, readBytes);
totalReadBytes += readBytes ;
callback( (totalReadBytes*100)/fs.st_size);
}
close(fdTrg);
close(fdSrc);
}
int main(void)
{
CopyFile("A.TXT", "B.TXT", ShowPercentage);
return 0 ;
}
我来回答
回答0个
时间排序
认可量排序

或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币
Markdown 语法
- 加粗**内容**
- 斜体*内容*
- 删除线~~内容~~
- 引用> 引用内容
- 代码`代码`
- 代码块```编程语言↵代码```
- 链接[链接标题](url)
- 无序列表- 内容
- 有序列表1. 内容
- 缩进内容
- 图片
相关问答
-
2020-12-30 14:44:10
-
2024-09-04 18:42:35
-
2016-11-17 17:05:42
-
2016-11-04 18:42:03
-
2017-06-20 18:15:15
-
2015-08-21 16:53:44
-
2008-08-01 18:54:34
-
2013-08-25 13:17:23
-
2017-11-30 21:09:41
-
2018-03-26 22:05:05
-
02016-10-08 14:13:06
-
2013-12-02 22:35:59
-
2017-03-13 21:22:00
-
2008-08-23 12:37:29
-
2020-08-24 19:03:42
-
2017-06-21 15:59:45
-
2008-08-07 19:23:29
-
2019-01-02 09:40:04
-
22013-11-21 21:50:42
无更多相似问答 去提问

点击登录
-- 积分
-- E币
提问
—
收益
—
被采纳
—
我要提问
切换马甲
上一页
下一页
悬赏问答
-
10海思3559的VGS模块在VI上画线问题
-
53519dv500接lvds的sensor mn34120,图像出现很多竖线,sensor板接以前的3519v101没问题
-
103403外接hdmi口1024*600显示屏报错
-
5SS928点DC camera的6946,全屏紫色
-
5hi3519 的 网络传输的MTU值可以修改到比1500大嘛?
-
10WS73V100星闪扫描不到设备sle
-
5SS928/SD3403 录像失败 venc stream time out, exit thread; venc 2 stream buffer is full
-
10Hi3516DV500无法运行PQTool软件
-
10君正T23+1084带TF卡插卡(给该主板适配TF卡驱动,电机驱动,适配GPIO)
-
5Hi3536A从vpss获取1080分辨率的yuv数据异常
举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
提醒
你的问题还没有最佳答案,是否结题,结题后将扣除20%的悬赏金
取消
确认
提醒
你的问题还没有最佳答案,是否结题,结题后将根据回答情况扣除相应悬赏金(1回答=1E币)
取消
确认