1
파일에서 데이터를 사용하여 구조체로 읽어야하는 문제에 대한 작업.strcpy 전에 문자 배열 끝에 null 문자를 추가하려고 시도했습니다.
이 파일은 #과 등급으로 끝나는 이름의 몇 줄의 ASCII 아트로 구성되어 있습니다. 이 라인에
multiple-character character constant
overflow in implicit constant conversion
: 내 소스를 컴파일 할 때
typedef struct
{
char* name;
char* art;
int rating;
}CASE;
, 나는 다음과 같은 경고가 계속 : 여기가 내 구조체는 다음과 같이 설정 한
Sample Name
(S)
(S)
# 5
예입니다 buffer[artCount] = '/0';
여기서 artCount는 버퍼 자체의 strlen입니다.
strcpy를 준비하기 위해 문자 배열 끝에 null 문자를 추가하기 만하면됩니다. 여기 내 논리에 문제가 있습니까?
기능 :
/*CASE* all is an empty array of CASE structs*/
void readFile(FILE* FPin, CASE* all)
{
CASE* walker = all;
int count = 0;
int artCount;
char buffer[160];
if((FPin = fopen("art.txt", "r")) == NULL)
{
printf("Error opening file.");
exit(100);
}
walker->name = (char*)malloc(sizeof(char)*100);
/*Reads in the name*/
while(fgets(walker->name, 100, FPin) != NULL)
{
/*Reads in the art*/
while(fscanf(FPin, "%c", buffer) != '#');
artCount = strlen(buffer);
buffer[artCount] = '/0';
walker->art = (char*)malloc(sizeof(char)*160);
strcpy(walker->art, buffer);
/*Reads in the rating*/
fscanf(FPin, "%d", &walker->rating);
count++;
walker++;
}
fclose(FPin);
return;
}
이스케이프 시퀀스는'\ 0' - ** 백 슬래시 ** 제로 – sidyll
'sizeof (char)'는 C 표준에 의해 1로 보장됩니다. –
6 년 동안 누구도 'artCount = strlen (buffer); buffer [artCount] = '\ 0 '' (오른쪽의'\\'와 함께) 아무것도하지 않습니까? – trentcl