2014-07-17 3 views
-2

파일에서 sscanf를 사용하려고합니다. 내가 일치하도록 노력하고 패턴은 다음과 이다 "%의 \ t %의 \ t %의 \ t % f를"C에서 탭 읽기

것은 내가 때문에 다음과 같은 입력에 대한 놀랜다 것입니다 : 안녕 안녕 안녕 5.344434

모든 데이터를 올바르게 읽는 중입니다 ...

이유를 알고 계십니까?

나는 다음과 같은 탭을 찾을 것으로 기대하고있었습니다. --- --- --- --- --- --- | 단 하나의 공간 만 일치하는 것은 아닙니다.

감사

답변

2

안돼요 - scanf 모두 공백으로 취급합니다 동일하게는 - 구분 기호로 사용되며 무시됩니다. 따라서 탭 공간으로 무엇인가를하고 싶다면 직접 해석해야합니다.

구문 분석하려면 scanf과 달리 구문 분석을하지 않고 전체 행을 읽어야합니다. 따라서 fgets을 사용해야합니다.

FILE *fp = /* init.. */; 
char buf[1024]; 
fgets(buf, 1024, fp); 
// parse yourself! 
+0

'는 –

+0

@BasileStarynkevitch 예 (자원 고갈, 즉 메모리 부족에 대한 물론 제외) 현재의 행의 길이를 제한하지 않기 때문에 getline'는 fgets' '보다 더 나은,하지만 그건 **하지 ** 표준 C> o < – ikh

3

standard

공백 문자 (들)로 구성된 지시자 을 남아 인터넷 RST 비 공백 문자 (최대 판독 입력하여 실행 읽지 않을 때까지). 환언

은, 형식 문자열 (isspace()에 의해 정의되는 공간, 탭, 개행 등;) 공백 문자의 시퀀스는 입력에 공백의 모든 양 일치한다.

0

신중하게scanf(3) 설명서를 읽었습니까? getline(3)을 사용하여 전체 줄을 읽은 다음 해당 줄을 "수동으로"구문 분석해야합니다!

2

당신이 scanf에 대한 documentation을 살펴 경우

C string that contains a sequence of characters that control how characters extracted from the stream are treated: 
Whitespace character: the function will read and ignore any whitespace characters 
encountered before the next non-whitespace character (whitespace characters include 
spaces, newline and tab characters -- see isspace). A single whitespace in the format 
string validates any quantity of whitespace characters extracted from the stream 
(including none). 
Non-whitespace character, except format specifier (%): Any character that is not 
either a whitespace character (blank, newline or tab) or part of a format specifier 
(which begin with a % character) causes the function to read the next character 
from the stream, compare it to this non-whitespace character and if it matches, 
it is discarded and the function continues with the next character of format. If the 
character does not match, the function fails, returning and leaving subsequent 
characters of the stream unread. 
Format specifiers: A sequence formed by an initial percentage sign (%) indicates a 
format specifier, which is used to specify the type and format of the data to be 
retrieved from the stream and stored into the locations pointed by the additional 
arguments. 

당신은 공백 문자는 무시받을 것을 알 수 있습니다.