 |
|
พอเราใส่ข้อมูลเข้าไปแล้ว พอจะ วิวดูหรือ search ดูข้อมูลที่เราใส่เข้าไป แล้วมันไม่ทำงานต่อ ต้องแก้ยังไงครับ
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define max 10
struct book {
int bid;
char bname[30];
};
void line(){
printf("=================================\n");
}
main() {
book p;
int i;
FILE *fptr;
clrscr();
do {
printf("Welcome to book store\n");
printf("\nPlease select you choice\n");
line();
printf("[1].insert\n[2].delete \n[3].view\n[4].search\n[5].exit\n");
printf("Insert you choice : ");
scanf("%d", &i);
line();
if (i == 1) {
printf("insert book stroe\n");
fptr = fopen("d:\\text.txt", "ab");
do {
printf("\nbook id : ");
scanf("%d", &p.bid);
printf("\nbook name : ");
scanf("%s", p.bname);
//fwrite(&student[i], sizeof(student[i]), 1, fptr);
fprintf(fptr, "%d\t%s\t", p.bid, p.bname);
printf("Add another student(y/n)?: ");
line();
} while (tolower(getche()) == 'y');
fclose(fptr);
} else if (i == 2) {
int a = 0;
book ps[max];
printf("delete profile\n");
fptr = fopen("d:\\text.txt", "r+t");
while (!feof(fptr)) {
fscanf(fptr, "%d\t%s", &ps[a].bid, ps[a].bname);
a++;
}
for(i=0;i<a-1;i++){
printf("%d\t",ps[i].bid);
}
fclose(fptr);
int choice;
printf("\nenter id :\n");
scanf("%d", &choice);
fptr = fopen("d:\\text.txt", "w+t");
for (int b = 0; b < (b - 1); b++) {
if (choice != ps[b].bid) {
fprintf(fptr, "%d\t%s\n", ps[b].bid, ps[b].bname);
}
}
fclose(fptr);
} else if (i == 3) {
int a = 0;
book ps[max];
fptr = fopen("d:\\text.txt", "r+t");
while (!feof(fptr))
{
fscanf(fptr, "%d\t%s", &ps[a].bid, ps[a].bname);
a++;
}
fclose(fptr);
printf("view data\n\n");
for(i=0;i<a-1;i++){
printf("%d\t%s\n",ps[i].bid,ps[i].bname);
}
} else if (i == 4) {
int x = 0;
int choice;
book ps[max];
fptr = fopen("d:\\text.txt", "r+t");
while (!feof(fptr))
{
fscanf(fptr, "%d\t%s", &ps[x].bid, ps[x].bname);
x++;
}
//fclose(fptr);
do{
for(i=0;i<x-1;i++){
printf("%d\t",ps[i].bid);
}
printf("\n0 to exit loop\n");
printf("enter id :\n");
scanf("%d", &choice);
for(i=0;i<x;i++){
if(ps[i].bid == choice)
{
printf("\nid %d\tname %s\n",ps[i].bid,ps[i].bname);
line();
//break;
}
//else if(ps[i].id != choice)
//{
// printf("\nname not found\n");
// star();
// break;
// }
}
} while (choice != 0);
}
line();
printf("\nexit (y/n)?: ");
} while (tolower(getche()) == 'n');
return 0;
}
Tag : - - - -
|
|
 |
 |
 |
 |
Date :
2010-12-07 23:25:57 |
By :
sampoo |
View :
1501 |
Reply :
0 |
|
 |
 |
 |
 |
|
|
|
 |