[Resolution] I have a question about file input/output. An unknown cause error appears.

Asked 2 years ago, Updated 2 years ago, 37 views

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#define MAXCHAR 256


typedef enum {false, true} bool; 

int main(int argc, char ** argv){
  bool is_n=false;
  bool is_o=false;
  bool is_s=false;
    int param_opt;
    int srcfd, dstfd;
    int i, j;
    int filesize = 512;
    int filecount;
    int invalid = 0;
    char buf[3];
    char * srcname;
    char * dstname;
    char * directory="./";
    char * temp;
    char * data;
    char name[MAXCHAR+1];
    ssize_t nread;
    struct stat srcstat;


    if(argc > 1000){
        printf("too many predicates.\n");
        exit(0);
    }
    if(argc < 2){
        printf("need predicate.\n");
        exit(0);
    }

while( -1 !=( param_opt = getopt( argc, argv, "o:n:x:d:")))
   {
      switch( param_opt)
      {
      case  'n'   :  is_n  = true;
                 temp   = optarg; 
                 break;
      case  's'   :  is_s  = true;
                 filesize=atoi(optarg);
                 break;
      case  'o'   :  is_o  = true;
                 srcname = optarg;
                 dstname = optarg;
                 break;
      case  'd'   :  strcat(directory, optarg);
                 break;
      case  '?'   :  printf( "ERROR: %cn", optopt); 
                 break;

      }
   }


   if(is_o==false){
     exit(1);
     }

   if(is_n==true){
     strcpy(dstname,temp);
     }




    printf("file size            = %dkb\n", filesize);
    printf("source filename      = %s\n", srcname);
    printf("destination filename = %s\n", dstname);
    printf("directory            = %s\n", directory);


    if((srcfd = open(srcname, O_RDONLY)) < 0){
        printf("file open error!\n");
        exit(1);
    }


    if(fstat(srcfd, &srcstat) == -1){
        printf("fstat error!\n");
        close(srcfd);
        exit(1);
    }

//  //  printf("file size : %d", srcstat.st_size);



    if(srcstat.st_size < filesize*1024){
        printf("file size is small than %d Kb\n", filesize);
//      //      close(srcfd);
//      //      exit(1);
    }


    filecount = (srcstat.st_size / (filesize*1024)) + 1;
    printf("%ld / %d \n", srcstat.st_size, filesize);


    if(filecount > 999){
        printf("too many file create.(more then 999)\n");
        printf("input size more than %d\n", filesize);
        close(srcfd);
        exit(1);
    }

    if(opendir(directory) == NULL){
        mkdir(directory, 0777);
    }

    data = (char *)malloc(sizeof(char)*filesize+1);
    strcpy(data,"");
    for(i = 0 ; i < filecount ; i++){


        sprintf(name,"%s%s%d",directory,dstname,i);

        printf("file name : %s\n",name);

        if(dstfd = open(name, O_WRONLY | O_CREAT | O_TRUNC)<0){//, srcstat.st_mode
        printf("OPEN_ERROR\n");
        }
        else{

            if((nread = read(srcfd, data, filesize)) < 0){
                printf("read_ERROR\n");
                break;
                }
            else{


            write(dstfd, data, nread);

            printf("%s\n", name);
            printf("...OK\n");
            }




        close(dstfd);
        }

    }
free(data);
    close(nread);
    close(srcfd);
    return 0;
}

The top is the full code. No errors occur when compiling.

The part of the code where the error occurs.

        if(dstfd = open(name, O_WRONLY | O_CREAT | O_TRUNC)<0){//, srcstat.st_mode
        printf("OPEN_ERROR\n");
        }
        else{

            if((nread = read(srcfd, data, filesize)) < 0){
                printf("read_ERROR\n");
                break;
                }
            else{


            write(dstfd, data, nread);

            printf("%s\n", name);
            printf("...OK\n");
            }




        close(dstfd);
        }

The aaaaa part is like the bottom part ./temp0 ....OK. It should be printed like this. However, the loaded portion of the file is printed before the ./temp0 portion. After taking a printf, the data portion is not saved when the write() function is used for the first time. What's the problem? Only file number 0 is abnormal, and there is no abnormality after that. I'm asking you a question because I don't know why. I beg you.

Below is an image to help you understand. There is a write function between 111111 and 22222, and you can see that the text is taken by data before 22222.

-n : Option to give the name of the file generated by cutting. If this option is not available, use the name of the file you want to cut to default

-s : Option to give the size of the file being created. If this option is not present, a 512KB file is created by default.

-o : Option to give the name of the file to be cut (option must be included)

-d : If you want to save the cut files to the desired directory instead of the current directory. If the directory does not exist, create it and save it. The location of the truncated file is the directory where default is the executable.

○ The file names of the truncated files should go to "abc001", "abc002", .... ○ If the number of truncated files exceeds 1000, it will be cut too finely, so give the command again.

c linux

2022-09-22 20:11

1 Answers

First of all, I used the write() function, and it was captured on the console, which means that the file descriptor is 0 Which means that the value of the file descriptor generated by the open was not delivered properly.

if(dstfd = open(name, O_WRONLY | O_CREAT | O_TRUNC)<0){//, srcstat.st_mode printf("OPEN_ERROR\n"); }

This code is incorrectly bracketed.

When modified

if((dstfd = open(name, O_WRONLY | O_CREAT | O_TRUNC))<0){//, srcstat.st_mode printf("OPEN_ERROR\n"); }

It's going to be like that's it. An error occurred because < was processed before = by operator priority.


2022-09-22 20:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.