?? datatransmitmodule.c
字號:
#include"global.h"#include"datatransmitmodule.h"int readlist(int fd , struct linebuf *lbuf ){ int hasreadinoneread; char *buf=lbuf->buf; int size=lbuf->size; int totalread=0; while( hasreadinoneread!=0 ){ if(size<=1){ char *tmp=lbuf->buf; int t=lbuf->size; lbuf->buf=xrealloc(buf,t*2); printf("realloc happened!\n"); lbuf->size=t*2; buf=(lbuf->buf-tmp)+buf; size=size+t; } hasreadinoneread=read( fd , buf , size ); printf("hasreadinoneread:%d\n",hasreadinoneread); //insert some protection for badread later here! size-=hasreadinoneread; buf+=hasreadinoneread; totalread+=hasreadinoneread; } *( (char*)buf+1 )='\0'; printf("totalread:%d lbuf->size:%d\n",totalread,lbuf->size); return totalread;}int readdata(int fd ,char *pathname){ int flag; int hasreadinoneread;//initialization just make we can enter the while(){} char *buf=malloc( sizeof(char)*4096 ); int w; if(buf==NULL){ perror("readdata()->malloc for buf error\n"); } int totalread=0; int filefd=open( pathname , O_WRONLY|O_CREAT , S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); if(filefd==-1){ perror("open file error!\n"); } while( hasreadinoneread ){ hasreadinoneread=read( fd , (void *)buf , 4096 ); if(hasreadinoneread==-1){ perror("readdata()->read from network\n"); } //insert some protection for badread later here! if(hasreadinoneread!=0){ if((w= write( filefd , (void *)buf , hasreadinoneread )) ==-1){ perror("readdata()->write to file\n"); } }else{ //I think a EOF should be inserted here! or maybe the close operation do this automatically close(filefd); printf("readdata()->after close(filefd)\n"); } printf("read:%d write:%d\n",hasreadinoneread,w); totalread+=hasreadinoneread; } *( (char*)buf+1 )='\0'; printf("file:%s has been read, %d bytes finished!\n",pathname,totalread); free(buf); printf("readdata()->after free(buf)\n"); return totalread;}int writedata( int fd , char * pathname){ int flag; int hasreadinoneread=1;//initialization just make we can enter while(){} char *buf=malloc( sizeof(char)*4096 ); int totalwrite=0; if(buf==NULL){ perror("writedata()->malloc for buf error"); } int filefd=open( pathname , O_RDONLY ); if(filefd==-1){ printf( "can't open %s." , pathname ); perror( "\n" ); } while( hasreadinoneread ){ hasreadinoneread=read( filefd , (void *)buf , 4096 ); if( hasreadinoneread==-1 ){ perror( "writedata()->read() from file" ); } if( hasreadinoneread!=0 ){ if( write( datafd , (void *)buf , hasreadinoneread )==-1 ){ perror( "writedata()->write() to network" ); } }else{ close(datafd); close(filefd); } totalwrite+=hasreadinoneread; } printf( "file:%s has been writen, %d bytes has finished!\n" , pathname , totalwrite ); return totalwrite;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -