fgets와 gets의 차이점
콩알녀
질문 제목 : fgets와 gets의 차이점fgets로 해서 안되던것이 gets로 하니깐 되는이유질문 내용 :
아래 소스를 보시면 빨갛게 표시된 fgets부분을 gets로 고치게 되면 remove함수가 제대로 실행이 됩니다
근데 왜 fgets로 하게 되면 잘 안되었을까요?? -_-a
#includestdio.h
#includestdlib.h
#includestring.h
typedef struct data
{
char name[20];
char number[20];
struct data *next;
}phone;
phone *head, *tail;
void init(void)
{
head=(phone*)malloc(sizeof(phone));
tail=(phone*)malloc(sizeof(phone));
head-next=tail;
tail-next=tail;
}
void insert()
{
phone *t;
t=(phone*)malloc(sizeof(phone));
printf(input name: );
scanf(%s,t-name);
fflush(stdin);
printf(input tel number: );
scanf(%s,t-number);
fflush(stdin);
printf(------- data inserted\n);
t-next=head-next;
head-next=t;
}
int remove(char *s)
{
phone *t,*p;
p=head;
t=p-next;
while(strcmp(s,t-name) !=0 && t!= tail)
{
p=p-next;
t=p-next;
}
if(t==tail)
return 0;
p-next=t-next;
free(t);
return 1;
}
void print()
{
phone *p;
p=head-next;
while(p!=tail)
{
printf(%s\t%s\n, p-name,p-number);
p=p-next;
}
}
int main(int argc,char *argv[])
{
init();
int a;
char buf[20];
while(1)
{
printf(-----menu-----\n);
printf( 1. insert\n);
printf( 2. remove\n);
printf( 3. search\n);
printf( 4. print all\n);
printf( 5. exit\n);
printf(choose the item: );
scanf(%d,&a);
getchar();
switch(a)
{
case 1:
insert();
break;
case 2:
printf(삭제할 이름: );
fgets(buf,20,stdin); //gets(buf)로 고치면 밑의 remove함수가 제대로 실행됨
remove(buf);
break;
case 4:
print();
&nbnbsp;break;
}
}
return 0;
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2692424 | 2차원배열 자료입력질문이요! (1) | 똘끼 | 2025-04-22 |
2692401 | 유닉스안에서 C언어를 이용한 명함 만들기 입니다; 이해안가는 부분이있네요 | 2gether | 2025-04-22 |
2692374 | 고수님들 댓글 마니부탁해요!!! (2) | 엄지 | 2025-04-22 |
2692343 | scnaf에 자꾸 선언을 참조하라는데;; (8) | 도래 | 2025-04-22 |
2692282 | 도스상에서 생성된 exe파일에 press~ 뜨게 하기 (4) | 회사원 | 2025-04-21 |
2692256 | scanf("%*c"); ㅠㅠ 고수님들 | 거북이 | 2025-04-21 |
2692230 | 하노이탑 질문입니다. (1) | 미쁘다 | 2025-04-21 |
2692210 | 정보 올림피아드 문제인데.. 풀이 과정이 궁금합니다.(재귀함수) (5) | 물티슈 | 2025-04-20 |
2692144 | C언어와 리눅스에 대한 질문입니다. | 싴흐한세여니 | 2025-04-20 |
2692114 | 컨텍스트 스위칭하는데 걸리는 시간 측정.. | YourWay | 2025-04-19 |
2692086 | 간접참조 연산자, 증감연산자 질문이용! (2) | 블랙캣 | 2025-04-19 |
2692056 | 주석좀 달아주세요. 몇개적엇는데 몇개만달아주세요. (2) | DevilsTears | 2025-04-19 |
2691978 | 진수 쉽게 이해하는법... (3) | 지지않는 | 2025-04-18 |
2691949 | getchar() 한 문자를 입력받는 함수 질문 | 채꽃 | 2025-04-18 |
2691919 | 배열 정렬 및 합치기 질문입니다. | 사과 | 2025-04-18 |
2691845 | c언어왕초보 질문이 있습니다........ | 루나 | 2025-04-17 |
2691815 | void add(int num); 함수... (4) | 살랑살랑 | 2025-04-17 |
2691756 | 명령 프롬프트 스크롤바가 없어요 | 두메꽃 | 2025-04-16 |
2691725 | 자료구조에 관련해서 질문이 있어 글을 올립니다. | 누리알찬 | 2025-04-16 |
2691697 | if 문에서 구조체 배열에 저장되있던 문자열 검사하는 법 ? (2) | 민트맛사탕 | 2025-04-16 |