나는 Csv 형식으로 다른 파일을 읽어야하는 C로 프로그램을 작성하고 있습니다. 모든 파일은 두 줄로 구성됩니다. 첫 번째 줄은 저장되는 항목을 설명하고 두 번째 줄은 항목의 데이터를 포함합니다. 모든 파일에는 개의 6 개의 열이 있습니다. 데이터는 날짜, 소스 및 카테고리와 같은 정보입니다. 실제로 경로를 얻는 프로그램을 작성하고 콘텐츠를 하나의 동적 문자 배열로 돌려 주지만 항상 디버그 어설 션 오류가 발생하여 항상 충돌합니다. 내 코드는 다음과 같습니다 어쨌든 끝에 첫 번째를 삭제할 원인 데이터가 저장되지 않기 때문에 C - csv 파일을 읽음으로써 디버그 어설 션 오류 오류가 발생했습니다.
char* readfile(char csvpath[]) {
FILE *csv;
int c;
int countcontent = 100; //counter for the length of the content array
int counter = 0; //counter of the amount of the inserted chars
char *temp; //temp = buffer
char *content = (char*)calloc(100,sizeof(char)); //content of the file
csv = fopen(csvpath,"r");
while(c = fgetc(csv) != EOF) { //while file isnt at the end
if(countcontent <= counter) {
realloc(content,100*sizeof(char));
countcontent += 100;
}
temp = (char*)calloc(20,sizeof(char));
fgets(temp,20,csv);
content = concat(content,temp); //concat is my own function and add the 2. string behind the 1.
counter+= 20;
}
fclose(csv);
return content;}
우선, [malloc (및 가족)의 반환을해서는 안됩니다.] (http://stackoverflow.com/questions/605845/do-i-cast-the-result-of- malloc). 둘째 * 디버거 사용 *. –
'c = fgetc (csv)! = EOF'는'c = (fgetc (csv)! = EOF) '와 동일합니다.' –
'stream! = NULL'은'fgetc'에서 실패했습니다. 열렸다. – ElderBug