文件问题
#include <stdio.h>
#include <stdlib.h>
#include "string.h"
typedef struct students
{
char name[20];
char sex;
char phone[9];
int flag;
}stu;
void Input();
void Add();
void Delete();
void Display();
void Seach();
main()
{
int option;
FILE *fp;
printf("**********Please choose the option you want to operate (1/2/3/4)\n\n");
printf(" 1): add a new content\n");
printf(" 2): delete a content\n");
printf(" 3): display all contents\n ");
printf(" 4): seach a content\n ");
printf(" 5): exit.\n");
printf(" Please input the option:");
scanf("%d",&option);
if((fp=fopen("students.txt","w"))==NULL)
{
printf("can't open this file or this file does't exit!");
return;
}
switch(option)
{
case 1 : Add(fp);break;
case 2 : Delete(fp);break;
case 3 : Display(fp);break;
case 4 : Seach(fp);break;
case 5 : return;
default: {
printf("input error! exit.");
return;
}
}
}
void Input(stu *student)
{
printf("\ninput the student name.:");
scanf("%s",student->name);
printf("\ninput the student phone:");
scanf("%s",student->phone);
printf("\ninput the student sex:");
scanf("%c",student->sex);
}
void Add(FILE *fp)
{
int num;
stu student;
num=ftell(fp)/sizeof(stu);
printf("the flag you shuold input:%d\n",num+1);
scanf("%d",&student.flag);
feek(fp,0,2);
Input(&student);
if(fwrite(&student,sizeof(stu),1,fp)!=1)
printf("write error\n");
else
printf("write succse!\n");
fclose(fp);
return;
}
void Delete(FILE *fp)
{
int num;
char ch;
stu play,student={"0",'0',"0",0};
printf("input the num you want to delete:");
scanf("%d",&num);
fseek(fp,sizeof(stu)*(num-1),0);
fread(&play,sizeof(stu),1,fp);
printf("the student's information you want to delete is:\n");
printf("%-20s%2c%2d",play.name,play.sex,play.phone);
printf("Are you sure to delete y/n ?\n");
ch=getch();
if(ch=='n')
{
fclose(fp);
return;
}
else
{
if(fwrite(&student,sizeof(stu),1,fp)!=1)
{
printf("Delete fail \n");
fclose(fp);
}
return;
}
void Display(FILE *fp)
{
int num,i;
rewind(fp);
fseek(fp,0,2);
num=ftell(fp)/sizeof(stu);
rewind(fp);
for(i=0;i<num;i++)
{
fread(&student,sizeof(stu),1,fp);
printf("%-20s%2c%2d",student.name,student.sex,student.phone);
}
fclose(fp);
}
void Seach(FILE *fp)
{
char namea[20],i;
stu student;
printf("Please input the name you want to seach:");
scanf("%s",namea);
rewind(fp);
fseek(fp,0,2);
num=ftell(fp)/sizeof(stu);
rewind(fp);
for(i=0;i<num;i++)
{
fread(&student,sizeof(stu),1,fp);
if(strcmp(student.name,namea)==0)
printf("%20s%2c%2s",student.name,student.sex,student.phone);
}
fclose(fp);
}
编译时总有一个错误:syntax error at end of input
[解决办法]
酷哦,我还从没编过这么长的程序呢,我要加油了
[解决办法]
这个应该不止一个问题吧...好像有很多错...以下代码.我做了点修改...应该勉强能用吧....
#include <stdio.h > #include <stdlib.h > #include <conio.h> //+++++#include "string.h" typedef struct students { char name[20]; char sex; char phone[9]; int flag; }stu; void Input(stu *student); void Add(FILE *fp) ; void Delete(FILE *fp) ; void Display(FILE *fp) ; void Seach(FILE *fp); stu student; //+++++++main() { int option; FILE *fp; printf("**********Please choose the option you want to operate (1/2/3/4)\n\n"); printf(" 1): add a new content\n"); printf(" 2): delete a content\n"); printf(" 3): display all contents\n "); printf(" 4): seach a content\n "); printf(" 5): exit.\n"); printf(" Please input the option:"); scanf("%d",&option); if((fp=fopen("students.txt","w"))==NULL) { printf("can 't open this file or this file does 't exit!"); return; } switch(option) { case 1 : Add(fp);break; case 2 : Delete(fp);break; case 3 : Display(fp);break; case 4 : Seach(fp);break; case 5 : return; default: printf("input error! exit."); return; } } void Input(stu *student) { printf("\ninput the student name.:"); scanf("%s",student->name); printf("\ninput the student phone:"); scanf("%s",student->phone); printf("\ninput the student sex:"); scanf("%c",student->sex); } void Add(FILE *fp) { int num; stu student; num=ftell(fp)/sizeof(stu); printf("the flag you shuold input:%d\n",num+1); scanf("%d",&student.flag); fseek(fp,0,2); //++++++++++ Input(&student); if(fwrite(&student,sizeof(stu),1,fp)!=1) printf("write error\n"); else printf("write succse!\n"); fclose(fp); return; } void Delete(FILE *fp) { int num; char ch; stu play,student={"0", '0 ',"0",0}; printf("input the num you want to delete:"); scanf("%d",&num); fseek(fp,sizeof(stu)*(num-1),0); fread(&play,sizeof(stu),1,fp); printf("the student 's information you want to delete is:\n"); printf("%-20s%2c%2d",play.name,play.sex,play.phone); printf("Are you sure to delete y/n ?\n"); ch=getch(); if(ch== 'n ') { fclose(fp); return; } else { if(fwrite(&student,sizeof(stu),1,fp)!=1) { printf("Delete fail \n"); fclose(fp); } return; } }//++++++++++++++++++++++++++ void Display(FILE *fp) { int num,i; rewind(fp); fseek(fp,0,2); num=ftell(fp)/sizeof(stu); rewind(fp); for(i=0;i <num;i++) { fread(&student,sizeof(stu),1,fp); printf("%-20s%2c%2d",student.name,student.sex,student.phone); } fclose(fp); } void Seach(FILE *fp) { char namea[20],i; stu student; int num = 0; //++++++++ printf("Please input the name you want to seach:"); scanf("%s",namea); rewind(fp); fseek(fp,0,2); num=ftell(fp)/sizeof(stu); rewind(fp); for(i=0;i <num;i++) { fread(&student,sizeof(stu),1,fp); if(strcmp(student.name,namea)==0) printf("%20s%2c%2s",student.name,student.sex,student.phone); } fclose(fp); }
[解决办法]
学生管理系统~~~~~~
看看老帖子,有很多的
[解决办法]
void Input(stu *student) //以前面的类型不对应呀,fp是FILE是文件指针,而这里接收的不是文件指针呀{ printf("\ninput the student name.:"); scanf("%s",student- >name); printf("\ninput the student phone:"); scanf("%s",student- >phone); printf("\ninput the student sex:"); scanf("%c",student- >sex); }