C에서 비교적 간단한 라이브러리 프로젝트를하려고하는데, 막혔습니다. I 변수 설정 fscanf
사용하고 (숯불을 cote
[5]) 다음 fgets
다른 변수 세트에 (a 문자 인 titre
을 [50]). 이 두 변수는 모두 Ouvrage
구조에 속합니다.fgets 다른 변수에 문자열 추가하기
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char cote[5];
char titre[50];
} Ouvrage;
void main(void)
{
FILE *flot;
Ouvrage o;
// Opening the file
flot = fopen("ouvrages.don", "r");
if(flot == NULL)
{
printf("Error opening file.\n");
exit(1);
}
// Reading the cote
fscanf(flot, "%s%*c", o.cote);
// Printing the length of cote
printf("STRLEN: %d\t", strlen(o.cote));
// Reading the titre
fgets(o.titre, 50, flot);
o.titre[strlen(o.titre) - 1] = '\0';
// Printing the length of cote again.... Different.
printf("STRLEN: %d\n", strlen(o.cote));
}
: 5는 아직 나중에는 여기에 31
을 반환 strlen(o.cote)
반환 test.c
코드입니다 :
문제는 fgets
가 fgets
하기 전에, 그것은 cote
에 읽고 문자열을 추가 할 것이다
그리고 여기가 ouvrage.don
파일입니다
NJDUI
Charlie et la chocolaterie
ROM
Rhoal Doal
S o fgets
은 이전 변수에 어떻게 영향을 미치며 어떻게 중지합니까? 어떤 도움이라도 대단히 감사합니다.
따뜻한 환영과 위대한 답변에 감사드립니다. C의 문자열은 재미가 아닙니다. :) 도와 줘서 고마워! –