Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

c - how to use write system call

My problem is system call write. arc file contains multiple file contains. I know each file size. and i want create different output files and I want write these contains into the these output files. I can create files but I can not write into the these file. I marked "PROBLEM HERE" below code.

#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
");
    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
",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
",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
");
                exit(1);
            }

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

    }
}
else if((strcmp(argv[1],"-x")==0))
{
    if(argc!=3)
    {
        fprintf(stderr,"Error : you must enter 3 argument for extract 
");
        exit(1);
    }
    else
    {
        if((arcfile=open(argv[2],O_RDONLY))==-1)
        {
            fprintf(stderr,"Error:File can't open %s
",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
",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
",file_len);
            printf("file name: %s
",f_name);
            printf("file size: %d
",sizeoffile[m]);
            //printf("file size : %d
",outfile[m].file_size);
        }
        for(m=0;m<n;m++)
        {

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

                if(xread==-1)
                {
                    fprintf(stderr,"Error occurred while reading.
");
                    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
");
    exit(1);
}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

On a quickly read on your code,I noticed:

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

should be

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...