가능한 중복 : 나는 어떻게 줄 끝에 단어를 읽을 수있는 문제가 여러 번 발생했습니다
Any cool function to replace readln from pascal in ansi c?읽기 단어
? 예를 들어
: 위험한 기능의
2
hello this is a word
hi five
so i want to output
case 1:
hello
this
is
word
case 2:
hi
five
가능한 중복 : 나는 어떻게 줄 끝에 단어를 읽을 수있는 문제가 여러 번 발생했습니다
Any cool function to replace readln from pascal in ansi c?읽기 단어
? 예를 들어
: 위험한 기능의
2
hello this is a word
hi five
so i want to output
case 1:
hello
this
is
word
case 2:
hi
five
하나는 당신에게 gets
로 명명 된 솔루션을 제공 할 것입니다. 그렇지 않으면
: -
char line[512];
int count=0;
char input=1;
while((input=getchar())!='\n')
line[count++]=input;
당신 수 문자열의 각 문자를 통해 루프 당신은 \n
또는 \r
문자가 발생할 때. 이 같은 게 어쩌면? :
이렇게하면 줄 바꿈시 수행 할 작업을 정확하게 선택할 수 있습니다. 파일을 파싱 할 때이 메서드를 많이 사용하는 경향이 있습니다. 각 줄을 버퍼에 쓰고 버퍼를 처리 한 다음 처리를 위해 다음 줄을 버퍼로 옮기기 시작합니다.
#include <stdio.h>
int main(){
int i, dataSize=0;
scanf("%d%*[\n]", &dataSize);
for(i = 1; i<=dataSize;++i){
char word[64];
char *p=word, ch=0;
printf("case %d:\n", i);
while(EOF!=ch && '\n'!=ch){
switch(ch=getchar()){
case ' '://need multi space char skip ?
case '\t':
case '\n':
case EOF:
*p = '\0';
printf("%s\n", p=word);
break;
default:
*p++ = ch;
}
}
if(ch == EOF)break;
}
return 0;
}
또는
#include <stdio.h>
#include <ctype.h>
int main(){
int i, dataSize=0;
scanf("%d%*[\n]", &dataSize);
for(i = 1; i<=dataSize;++i){
char word[64],ch = 0;
int stat = !EOF;
printf("case %d:\n", i);
while(EOF!=stat && '\n'!=ch){
ch = 0;
stat=scanf(" %s%c", word, &ch);
if(EOF!=stat || isspace(ch)){
printf("%s\n", word);
}
}
if(EOF==stat)break;
}
return 0;
}