strcmp(list[middle].name,list[search].name)이거 왜안되죠?
갤원
/*
Filename : 4.c
Written by : 2006920017 KimJinSoo
Date : 2008.03.13
*/
#includestdio.h
#includestdlib.h
#includestring.h
//Global Declarations
typedef struct
{
char name[10];
int id;
int math;
int eng;
}STUDENT;
int binsearch(STUDENT *list, int search,int left, int right);
//main start
void main()
{
//Local Definitions
int n=10;//the numbers of all student loading from the data.txt
int i;
int search;//get search from the user console
int result;//get result from the binsearch()
float sum1=0,sum2=0;
STUDENT *pArray=(STUDENT*)calloc(n,sizeof(STUDENT));//pArray which has 5 Struct Person
//read the file
FILE *fp;//fp means filepointer
fp=fopen(data.txt,r);//r means read
if(fp==0)
{
printf(CANNOT OPEN THE FILE, CHEAK PLEASE);//exception handling
exit(0);
}
fscanf(fp,%d,&n);//The fist number means the struct PERSON Numbers
//the number of all students in the dataprintf(The Number of Student is %d \n,n);
for(i=0;in;i++)//load the data from the file
{
fscanf(fp,%s,&pArray[i].name);//fscanf take fp into variable.
fscanf(fp,%d,&pArray[i].id);
fscanf(fp,%d,&pArray[i].math);
fscanf(fp,%d,&pArray[i].eng);
}
for(i=0;in;i++)
{
fprintf(stdout,%s ,pArray[i].name);
fprintf(stdout,%d ,pArray[i].id);
fprintf(stdout,%d ,pArray[i].math);
fprintf(stdout,%d ,pArray[i].eng);
printf(\n);
}
printf(Input the name (ex)Ann \n : );
scanf(%s,&search);
result = binsearch(pArray,search,0,n-1);//binsearch()
printf(Name:%s\nStudentID:%d\nEnglish:%d\nMath:%d\n\n,
pArray[result].name, pArray[result].id, pArray[result].math, pArray[result].eng);
//close the file pointer
fclose(fp);
free(pArray);
}//main end
int binsearch(STUDENT *list,int search, int left, int right){
int middle=(left+right)/2;
if(left=right)
{
switch(strcmp(list[middle].name,list[search].name))
{
case -1 :return binsearch(list,search,middle+1,right);
case +1 : return binsearch(list,search,left,middle-1);
case 0 :return middle;
}
}
return -1;
}access error가 나는데, 구조체를 가리키는 포인터에 문제가 있는건지;-_-
제가 switch 안에를 1로 했을땐 됬는데,
문제는 strcmp인거 같아염;; 근데 왜 그런건지 모르겠음.ㅜㅠ
-
남자
list의 범위 문제겠네요.
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2700562 | 함수포인터에서요 (7) | 소심한여자 | 2025-07-06 |
2700530 | 전처리문 질문입니다. (1) | 아놀드 | 2025-07-05 |
2700510 | c언어를 어케하면 잘할수 있을까요.. | 연연두 | 2025-07-05 |
2700484 | 두 개가 차이가 뭔지 알려주세요...(소수 찾는 프로그램) (2) | 날위해 | 2025-07-05 |
2700426 | 인터넷 창 띄우는 질문이요 (1) | 정훈 | 2025-07-04 |
2700400 | 원넓이를 계산이요 ㅜㅜ | 천칭자리 | 2025-07-04 |
2700368 | if에 관해서 질문이요... | Orange | 2025-07-04 |
2700339 | 이거 결과값이 왜이런건지.. (4) | 그댸와나 | 2025-07-04 |
2700313 | 파일 읽어서 저장하는데 빈파일일 경우 문재가 발생하네요.. (2) | 크나 | 2025-07-03 |
2700287 | 구조체 동적할당 연습을 하는데 오류가 뜹니다...(해결) (3) | 아련나래 | 2025-07-03 |
2700264 | 문자와 숫자 동시에 입력??? | 글고운 | 2025-07-03 |
2700236 | txt파일로만 쓰고 읽게 하려면 어떻게 해야 하나요..?? (8) | 미국녀 | 2025-07-03 |
2700211 | 전위 연산자 (2) | 어른처럼 | 2025-07-02 |
2700183 | C에서 파일이름을 받고, 그 파일의 사이즈를 출력해줘야하는데 내용이 출력이 안되네요 ;ㅅ; | 피스케스 | 2025-07-02 |
2700150 | 꼭좀 도와주세요ㅠㅠㅠ | 호습다 | 2025-07-02 |
2700095 | 연산문제...질문... | 오빤테앵겨 | 2025-07-01 |
2700070 | while문 , 3의배수 출력하는 프로그램좀 짜주세욤. | 횃불 | 2025-07-01 |
2700041 | 초보인데요 ㅎ 배열안에 배열을 집어넣을수 있나요?? | 헛장사 | 2025-07-01 |
2700012 | 배열// (1) | 전갈자리 | 2025-07-01 |
2699895 | 무한루프에 빠집니다.!! 해결좀부탁드려요 (10) | 선아 | 2025-06-30 |