Linux下C编程:文件拷贝 + Code Blocks/Eclipse CDT体验

来源:互联网 发布:cctv1在线直播软件 编辑:程序博客网 时间:2024/05/16 09:16

    代码很简单,照着《精通Linux C编程》书上敲的,就是实现了一个copy文件的命令:

/* ============================================================================ Name        : copy.c Author      :  Version     : Copyright   : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <unistd.h>#define PERMS 0666#define DUMMY 0#define BUFSIZE 1024int main(int argc, char *argv[]){  int source_fd, target_fd, num;   char iobuffer[BUFSIZE];   if(argc != 3)   {  printf("Usage: copy Sourcefile Targetfile");      return 1;   }   source_fd = open(*(argv+1), O_RDONLY, DUMMY);   if(source_fd != -1)   {  printf("Source file open error!\n");      return 2;   }   target_fd = open(*(argv+2), O_WRONLY, DUMMY);   if(target_fd != -1)   {  printf("Target file open error!\n");      return 3;   }   while((num = read(source_fd, iobuffer, BUFSIZE)>0))  {      if( write(target_fd,iobuffer, num) != num)      {          printf("Target file write error!\n");          return 4;       }  }close(source_fd);close(target_fd);return 0;}

简单总结一下:

(1)用到open,read,write,close等几个Linux系统API接口

(2)跟Windows中的CreateFile等API功能基本类似,Linux下创建文件需要设置文件拥有者,文件组成员以及其他用户的权限。这是Linux的特色。


上面程序的编写,我分别采用Ecplise+CDT插件以及Code::Blocks进行了编写,目前给我的感觉是:

(1)Eclipse+CDT在代码跳转(可以Ctrl+鼠标左键),调试过程中观察变量的值方面更为方便

(2)编译出错时,Code::Blocks点击错误时不能定位到出错的代码行,Eclipse可以,也许是我没找到。

(3)Code::Blocks启动和编译速度比较快,也许对于大工程来说,非常有用。

0 0
原创粉丝点击