는 매우 편리한 수단 문자열을 파싱한다.
은 우리가 후있어 일정한 패턴이있는 int 및 문자열에 의해 followd 3 문자열 뒤에 INT는 것을이 예를 들면 가정합니다 입력 문자열
4273 Багров Д. С. 5454 знззз
을 감안할 때. 다른 방법이 있습니다. 저는 이것들을 다시 생각해 보겠습니다.
아주 기본적인 데모 :
#include <stdio.h>
int main(void) {
char * inputdata = "4273 Багров Д. С. 5454 знззз";
// variables to receive the scanned data
int firstint, secondint;
char firststring[32];
char secondstring[32];
char thirdstring[32];
char fourthstring[32];
// important, you should check whether the number of converted elements
// matches what you expect:
int scannedelements;
// let's scan the input
scannedelements = sscanf (inputdata,"%d %s %s %s %d %s",&firstint, &firststring, secondstring,
thirdstring,&secondint,fourthstring);
// and show what we found. Notice the similarity between scanf and printf
// but also note the subtle differences!!!
printf("We scanned %d %s %s %s %d %s\n",firstint, firststring, secondstring,
thirdstring,secondint,fourthstring);
printf("That's a total of %d elements %d\n",scannedelements);
return 0;
}
출력 : 내가 정수로 당신이 시험이라는 분야를 스캔
We scanned 4273 Багров Д. С. 5454 знззз
That's a total of 6 elements
공지 사항, 당신은 쉽게의 루프에 의해 그것의 숫자를 추출 할 수 있습니다 digit = data % 10; data = data/10;
이제 문자열의 첫 번째 그룹이 3 개의 다른 출력으로 잘리는 사실이 귀찮을 수 있습니다.
#include <stdio.h>
int main(void) {
char * inputdata = "4273 Багров Д. С. 5454 знззз";
// variables to receive the scanned data
int firstint, secondint;
char firststring[32];
char secondstring[32];
char thirdstring[32];
char fourthstring[32];
// important, you should check whether the number of converted elements
// matches what you expect:
int scannedelements;
// Alternatively, let's scan the group of 3 strings into 1 variable
scannedelements = sscanf (inputdata,"%d %[^0-9] %d %s",&firstint, firststring, &secondint,fourthstring);
// and show what we found.
printf("We scanned %d %s %d %s\n",firstint, firststring,secondint,fourthstring);
printf("That's a total of %d elements %d\n",scannedelements);
return 0;
}
출력 :
We scanned 4273 Багров Д. С. 5454 знззз
That's a total of 4 elements -1079150400
공지 후미 공간 Багров Д. С.
에서 문제가 될 수도 있고 그렇지 않을 수도 그것이 자리를 만날 때까지 상기 출력 데이터에 따라, 우리는 읽기 sscanf를 지시 할 수있다 , 그러나 그것은 쉽게 제거됩니다. 여러분의 편의를 위해
는,이 코드는 ideone로 볼 수 있습니다 : http://ideone.com/4gFlxf#sthash.KQfhcYxr.dpuf
이 예는 거의 scanf와 함께 무엇이 가능한지의 표면을 긁는 다, 나는 그것의 manpage은 더 많은 가능성을 발견하고 탐구하는 것이 좋습니다.
- 평균 점수를 계산하는 방법에
:
#include <stdio.h>
int main(void) {
int inputdata = 24680;
int average = 0;
int number_digits = 0;
int digit = 0;
int digits = 0;
while (inputdata > 0) {
digit = inputdata % 10; // modulo by 10 is the last digit
average += digit;
digits++;
inputdata = inputdata/10; // integer division by 10 = remove last digit
}
if (digits > 0) { // to avoid dividing by zero is some edge case
printf ("The average over %d scores is %.1f\n", digits, (double) average/digits);
} else {
printf ("As the input was 0, the average is 0");
}
return 0;
}
I 자동 분석 구조체를 사용하여 문자열 좋은 생각이고, 문제는의 직접적인 효과라고 생각하지 않습니다를 그 선택. 예를 들어 fgets를 사용하여 문자 배열로 행을 읽은 다음 sscanf를 사용하여 파싱합니다. – fvu
가장 긴 이름이 가능하도록 (메모리 비효율적이지만 매우 직관적 인)'name' 요소 크기를 늘리거나'char *'으로 만들고'malloc'을 사용하십시오. 또한 "문자열 파싱"('strtok'시도)을 재고해야합니다. – usr2564301
나는 @fvu 제안을 제안하지만'sscanf' 대신'strtok'에 관심이있을 것입니다. –