[해결]파일 입출력 관련 질문드립니다. 원인모를오류가 뜹니다.
조회수 1816회
include
#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;
}
위에가 전체코드입니다. 컴파일할때 오류가 발생하지는 않습니다.
오류가 발생하는 코드부분입니다.
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);
}
저 aaaaaa 부분이 아래처럼 ./temp0 ....OK 이렇게 출력되야합니다. 그런데 ./temp0부분앞에 파일에서 로드된 부분이 출력됩니다. printf를 찍어본 결과 처음 write()함수가 사용될때 data부분이 저장되지 않습니다. 어떤것이 문제일까요. 0번 파일만 이상이 생기고 그 후부터는 이상이 생기지 않습니다. 도저히 이유를 몰라 질문드립니다. 부탁드립니다.
아래는 이해를 돕기위한 이미지입니다. 111111과 22222 사이에 write함수가 존재하고, 22222가 찍히기전에 문자가 data가 찍는것을 볼수있습니다.
- 파일의 절단 옵션은 기본적으로 4개가 사용가능함. (선택적으로 순서에 상관없이 적용 가능해야 함)
-n : 절단해서 생성되는 파일의 이름을 주기 위한 옵션. 이 옵션이 없는 경우는 default 로 절단 하는 파일의 이름을 사용
-s : 생성되는 파일의 크기를 주기 위한 옵션. 이 옵션이 없는 경우 default 로 512KB 크기의 파일들이 생성
-o : 절단할 파일의 이름을 주기 위한 옵션 (반드시 포함되어야할 옵션)
-d : 절단한 파일들을 현재 directory가 아닌 원하는 directory에 저장하고자 할 경우. directory가 없는 경우 생성하여 저장하도록 함. 절단된 파일의 위치는 default 가 실행 프로그램이 있는 directory
○ 절단된 파일들의 파일명은 "abc001", "abc002", .... 로 진행되도록 함. ○ 절단된 파일의 개수가 1000개는 넘는 경우는 너무 잘게 잘리므로 다시 명령을 주도록 함.
1 답변
-
일단..write()함수를 썼는데 콘솔에 찍혔다는건 파일디스크립터가 0번이라는 뜻이고, 그 말은 open을 통해 생성된 파일디스크립터 값이 제대로 전달이 안됐다는 거겠죠..
if(dstfd = open(name, O_WRONLY | O_CREAT | O_TRUNC)<0){//, srcstat.st_mode printf("OPEN_ERROR\n"); }
이 코드에서 괄호가 잘못 쳐져있습니다.
수정하면
if((dstfd = open(name, O_WRONLY | O_CREAT | O_TRUNC))<0){//, srcstat.st_mode printf("OPEN_ERROR\n"); }
가 되겠네요. 연산자 우선순위에 의해 =보다 <가 먼저 처리돼서 해당 오류가 발생하였습니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
-
댓글 입력