2013-05-05 75 views
-3

我的問題是系統調用寫入。 arc文件包含多個文件包含。我知道每個文件的大小。我想創建不同的輸出文件,我想將這些包含到這些輸出文件中。我可以創建文件,但我無法寫入這些文件。我在下面的代碼中標記了「PROBLEM HERE」。如何使用寫入系統調用

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <sys/stat.h> 
#include <sys/types.h> 
#include <unistd.h> 
#include <fcntl.h> 
#define BUFSIZE 4096 

struct stat st; 

struct inputfiles{ 
    unsigned char numchar; 
    char *filename; 
    unsigned int filesize; 
} file[255]; 

struct outputfiles{ 
    unsigned char num_char; 
    char *file_name; 
    unsigned int file_size; 
} outfile[255]; 


int main(int argc,char *argv[]) 
{ 
    int i,j,k,m,h; 
    int input_file,output_file; 
    unsigned char chinput_file; 
    int fd; 
    int infile,outfile,arcfile; 
    int extfile[255]; 
    char buffer[BUFSIZE]; 
    char outbuffer[BUFSIZE]; 
    char n; 
    char flength; 
    int file_len; 
    char *f_name; 

char *tempsize; 
int sizeoffile[255]; 

ssize_t nread,xread; 

input_file=argc-3; 
chinput_file=(unsigned char)input_file; 
if(argc<=2) 
{ 
    fprintf(stderr,"Error: you must enter least 3 argument\n"); 
    exit(1); 
} 
else if(strcmp(argv[1],"-c")==0) 
{ 
    if((outfile=open(argv[2],O_WRONLY | O_CREAT,0644))==-1) 
    { 
     fprintf(stderr,"Error: Archive file can't open %s\n",argv[2]); 
     remove(argv[2]); 
     exit(1); 
    } 

    /*** write number of files into the archive file ***/ 
    write(outfile,(char*)&chinput_file,sizeof(char)); 


    j=0; 
    for(i=3;i<argc;i++) 
    { 
     file[j].numchar=(unsigned char)strlen(argv[i]); 


     /**** write filename size into archive file ****/ 
     write(outfile,(char*)&file[j].numchar,sizeof(char)); 



     file[j].filename=malloc(file[j].numchar); 
     strcpy(file[j].filename,argv[i]); 

     /**** write filename into the archive file ****/ 
     write(outfile,file[j].filename,file[j].numchar); 

     stat(argv[i],&st); 
     file[j].filesize=st.st_size; 

     /**** write size of file into the archive file ****/ 
     write(outfile,&file[j].filesize,sizeof(int)); 

     j++; 
    } 

    for(m=3;m<argc;m++) 
    { 
     if((infile=open(argv[m],O_RDONLY))==-1) 
     { 
      fprintf(stderr,"Error: File can't open %s\n",argv[m]); 
      exit(1); 
     } 

     while((nread=read(infile,buffer,BUFSIZE))>0) 
     { 
      if(write(outfile,buffer,nread)<nread) 
      { 
       fprintf(stderr,"Error : input file size too much\n"); 
       exit(1); 
      } 

      if(nread==-1) 
      { 
       fprintf(stderr,"Error occurred while reading.\n"); 
       exit(1); 
      } 
     } 

    } 
} 
else if((strcmp(argv[1],"-x")==0)) 
{ 
    if(argc!=3) 
    { 
     fprintf(stderr,"Error : you must enter 3 argument for extract \n"); 
     exit(1); 
    } 
    else 
    { 
     if((arcfile=open(argv[2],O_RDONLY))==-1) 
     { 
      fprintf(stderr,"Error:File can't open %s\n",argv[2]); 
      remove(argv[2]); 
      exit(1); 
     } 

     read(arcfile,(char*)&n,sizeof(char)); 
     output_file =(int)n; 


     for(m=0;m<n;m++) 
     { 

      read(arcfile,&flength,sizeof(char)); 
      file_len=((int)flength); 
      f_name=malloc(file_len+1); 
      read(arcfile,f_name,file_len); 

      if((extfile[m]=open(f_name,O_WRONLY | O_CREAT ,0644))==-1) 
      { 
       fprintf(stderr,"Error :extract file is %s can't open\n",f_name); 
       remove(f_name); 
       exit(1); 
      } 


      tempsize=malloc(sizeof(int)); 
      read(arcfile,&tempsize,sizeof(int)); 

      sizeoffile[m]=(int)tempsize; 
      //outfile[m].file_size=sizeoffile; 

      //printf("file name length: %d\n",file_len); 
      printf("file name: %s\n",f_name); 
      printf("file size: %d\n",sizeoffile[m]); 
      //printf("file size : %d\n",outfile[m].file_size); 
     } 
     for(m=0;m<n;m++) 
     { 

      while((xread=read(arcfile,outbuffer,sizeoffile[m]))!=0) 
      { 
       //printf("xread is : %d\n",xread); 
       //if(write(extfile[m],outbuffer,xread)!=xread) 
       //{ 
        // fprintf(stderr,"Error : input file size too much\n"); 
        // exit(1); 
       //} 

       if(xread==-1) 
       { 
        fprintf(stderr,"Error occurred while reading.\n"); 
        exit(1); 
       } 

       write(extfile[m],outbuffer,xread); <-------- PROBLEM IS HERE 

      }//end of while 
     } //end of second for loop 
    }//end of else 

}//end of else if 


else { 
    fprintf(stderr,"invalid command line\n"); 
    exit(1); 
} 

}

+0

/讀取而不是fopen/fwrite/fread? – 2013-05-05 16:30:18

+0

我做到了。但我現在嘗試與系統調用。 – ccc 2013-05-05 16:31:56

+0

嘗試用'O_TRUNC'標誌設置打開,除非您有特殊原因不這樣做。 – 2013-05-05 16:35:55

回答

2

在一個對你的代碼快速閱讀,我注意到:你爲什麼要使用開/寫

if((extfile=creat(f_name,0644)==-1))

應該

if((extfile=creat(f_name,0644))==-1)

+0

謝謝你的好處,但仍然一樣。我可以打開所有文件,但裏面是空的。我無法將數據寫入文件。我也編輯我的代碼 – ccc 2013-05-05 19:35:25

+0

我編輯我的代碼我標記的問題在這裏 – ccc 2013-05-05 19:39:27

+1

@ccc:你準確得到什麼錯誤? – Jack 2013-05-07 01:45:23