C 如何使用写系统调用

C 如何使用写系统调用,c,system,system-calls,C,System,System Calls,我的问题是系统调用写入。arc文件包含多个文件包含。我知道每个文件的大小。我想创建不同的输出文件,我想把这些内容写入这些输出文件。我可以创建文件,但不能写入这些文件。我在下面的代码中标记了这个问题 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <unis

我的问题是系统调用写入。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);
}

}快速阅读您的代码后,我注意到:

ifextfile=creatf_name,0644==-1

应该是


如果extfile=creaf_name,0644==-1

为什么要使用open/write/read而不是fopen/fwrite/fread?我用了。但我现在尝试使用系统调用。尝试使用O_TRUNC标志集打开,除非您有特定的原因不这样做。我尝试了w/O_TRUNC,但仍然相同。因此,启动旧的良好GDB并观察变量的内容是否存在异常。此外,为什么要用malloc分配一组可以静态声明为f_name、tempsize等的局部变量呢?谢谢你的好意,但仍然是一样的。我可以打开所有文件,但里面是空的。我无法将数据写入文件。我还编辑了我的代码