-3

我的问题是系统调用写入。arc 文件包含多个文件包含。我知道每个文件的大小。我想创建不同的输出文件,我想将这些包含写入这些输出文件中。我可以创建文件,但我不能写入这些文件。我在代码下面标记了“这里的问题”。

#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);
}

}

4

1 回答 1

2

在快速阅读您的代码时,我注意到:

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

应该

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

于 2013-05-05T18:18:04.823 回答