Linux下C编程-文件编程

1、linux系统调用其实也是和库函数调用类似的,只是有些地方需要注意,比如头文件的包含,系统调用执行错误时,返回错误的值。

2、错误的返回及显示可以用【perror】或【strerrno配合fprintf和stderr】具体可以man

3、在头文件stdlib.h头文件中定义了一些宏定义,例如  :#define EXIT_SUCCESS 0        #define EXIT_FAILURE 1

      可参考 http://baike.baidu.com/view/1347718.htm

4、综合实例:file_cp.c(文件复制)的注意事项

     1)linux系统调用和C库函数都有相应的文件操作函数,但两者还是有不同之处,

例如系统调用打开函数样用:open(argv[1],O_RDONLY)  ,C库函数这样用:fopen("argv[1]","r") ,虽然两者函数原型分别为

int open(const char *pathname, int flags, [mode_t mode]) 和 FILE *fopen(const char *filename, const char *mode),但为什么要加双引号,我也不知道,希望高手支招。要是不加的话会有警告(warning: passing arg 1 of `fopen' makes pointer from integer without a cast  ,实参和形参不一致),而且执行之后会错误。

     2)使用exit(宏或常数)结束执行,宏即上面提到的EXIT_SUCCESS,EXIT_FAILURE。exit是退出程序 return是返回函数值 在main里面return和exit作用一样 但是在其他函数里面就不是了,int main(int argc, char *agrv[ ]) 的情况下不用return返回一个值,System可能会当你的程序非法退出。

5、一些心得:每执行一个功能或者库函数后,记得编写几个反馈语句(反馈是否正确执行,如果错误,是怎么样的错误,用户应如何纠正),这样一来方便调试,二来可以使得界面更加友好。

6、源代码

     1)linux系统调用编写 file_cp.linux.c

#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<stdio.h>

#include<errno.h>

#include<string.h>

#define BUFFER_SIZE 1024

int main(int argc,char *argv[])

{

        int from_fd,to_fd;

        int byte_read,byte_write;

        char *ptr;

        char buffer[BUFFER_SIZE];

        if(argc!=3)

        {

                fprintf(stderr,"Usage, %s from_file to_file\n",argv[0]);

                exit(1);

        }

        if((from_fd=open(argv[1],O_RDONLY))<0)

        {

                fprintf(stderr,"Open %s failed,Error:%s\n",argv[1],strerror(errno));

                exit(1);

        }

        if((to_fd=open(argv[2],O_WRONLY|O_CREAT,0644))<0)

        {                fprintf(stderr,"Open %s failed,Error:%s\n",argv[2],strerror(errno));

                exit(1);

        }

        while(byte_read=read(from_fd,buffer,BUFFER_SIZE))

        {

                if((byte_read==-1)&&(errno!=EINTR)) break;

                else

                {

                        ptr=buffer;

                        while(byte_write=write(to_fd,ptr,byte_read))

                        {

                                if((byte_write==-1)&&(errno!=EINTR))break;

                                else if(byte_write==byte_read)break;

                                else

                                {

                                        ptr+=byte_write;

                                        byte_read-=byte_write;                                }

                        }

                        if(byte_write==-1)break;

                }

        }

        close(from_fd);        close(to_fd);

        exit(0);

}

     2)C库函数编写 file_cp.c

#include<stdio.h>

#include<stdlib.h>

#define BUFFER_SIZE 1024

int main(int argc,char *argv)

{

        FILE *from_fd;

        FILE *to_fd;

        int byte_read,byte_write;

        char buffer[BUFFER_SIZE];

        char *ptr;

        if(argc!=3)

        {

                printf("you haven't input the filename!\n");

                return 1;

        }

        if((from_fd=fopen("argv[1]","r"))<0)

        {

                printf("open %s failed!\n",argv[1]);

                return 1;

        }

        if((to_fd=fopen("argv[2]","w+"))<0)

        {

                printf("open %s failed!\n",argv[2]);

                return 1;

        }

        ptr=buffer;

        while(byte_read=fread(ptr,1,BUFFER_SIZE,from_fd))

        {

                if(byte_read<0)

                {

                        printf("read error\n");

                        return 1;

                }

                while(byte_write=fwrite(ptr,1,byte_write,to_fd))

                {

                        if(byte_write<0)

                        {

                                printf("write error\n");

                                return 1;

                        }

                        else if(byte_write<byte_read)

                        {

                                ptr+=byte_write;

                                byte_read-=byte_read;

                        }

                }

        }

        fclose(from_fd);

        fclose(to_fd);

        return 0;

}

你可能感兴趣的:(linux)