以下编程在' fputs'的第一个实例崩溃,试图打印' fname'到文件Member_inf。我还试图打印一个常量字符串" abcdefg"它仍然失败了。究竟做错了什么?它似乎是文件的初始化。如果可能,请仅帮助解决问题'问题,我宁愿在我遇到他们时自己尝试任何其他人。
#include<stdio.h>
#include<dos.h>
#include<time.h>
#include<conio.h>
FILE *Member_inf;
FILE *Admin_inf;
FILE *Book;
void delay(unsigned int mseconds);
struct meroDate
{
int mm,dd,yy;
};
char *newlinestring(char t[]);
int strilen(char t[]);
int main (){
int choice,age,contact;
char *member_fname=(char*)malloc(30);
char *member_lname=(char*)malloc(30);
char *fname=(char*)malloc(20);
char *lname=(char*)malloc(20);
char *address=(char*)malloc(60);
do{
printf("\xB1\xB2\xB1\xB2\xB1\xB2\xB1\xB2\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1");
printf("\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1");
printf("\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB2\xB1\xB2\xB1\xB2\xB1");
printf("\xB2\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1 WELCOME TO THE ST. JAGO HIGH SCHOOL LIBRARY SYSTEM ");
printf("\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1");
printf("\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1");
printf("\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1");
printf("\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\xB1\n\n");
printf("\xDB\xDB\xDB\xDB\xB2 1:ADD MEMBER\n\n");
printf("\xDB\xDB\xDB\xDB\xB2 2:SEARCH EXISTING MEMBER\n\n");
printf("\xDB\xDB\xDB\xDB\xB2 3:EXIT\n\n");
printf("\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16");
printf("\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16");
printf("\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16\x16");
time_t t;
time(&t);
printf("Date and time:%s\n\n\n\n",ctime(&t));
printf("Please enter your choice: ");
scanf("%d",&choice);
system("cls");
printf("You chose option %d\n\n",choice);
if (choice==3){
printf("Thank you for visiting St. Jago High School Library. \n");
}else if(choice==1){
printf("***************************** MEMBER REGISTRATION ******************************\n\n");
printf("Please enter first name: \n");
scanf("%s",&*fname);
printf("\nPlease enter last name: \n");
scanf("%s",&*lname);
printf("\nPlease enter age: \n");
scanf("%d",&age);
printf("\nPlease enter address: \n");
scanf("%s",&*address);
printf("\nPlease enter contact number: \n");
scanf("%d",&contact);
Member_inf=fopen("Members.txt","a");
fputs(fname,Member_inf); <======== /*HERE HERE HERE*/
fputs(lname,Member_inf);
fputs(age,Member_inf);
fputs(address,Member_inf);
fputs(contact,Member_inf);
fclose(Member_inf);
}else if (choice==2){
printf("***************************** MEMBER INFORMATION *******************************\n\n");
printf("Please enter members first name: \n");
scanf("%s",&member_fname);
printf("\nPlease enter members last name: \n");
scanf("%s",&member_lname);
}else if (choice!=1,2,3){
printf("wrong choice.Enter Again");
}
system("cls");
}while(choice!=0);
return 0;
}
void delay(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock());
}
char* newlinestring(char t[]){
t[strilen(t)+1]='\0';
return t;
}
int strilen(char t[]){
int i;
for(i=0;t[i]!=NULL;i++){}
return i;
}
答案 0 :(得分:3)
scanf("%s",&*fname);
应为scanf("%s",fname);
fgets(fname);
建议之一 - 函数具有返回值 - 利用它们
修改强>
关于使用better yet
的说明,fgets()
表示停止使用scanf()
并仅使用fgets()
。
答案 1 :(得分:0)
fputs(age,Member_inf);
的值是否为NULL来检查文件是否已成功打开。for(i=0;t[i]!=NULL;i++){}
- 您不能使用fputs将整数写入文件,您应该先将其强制转换为字符串(可能使用itoa)。strlen
- 这不是你获得字符串长度的方式。没有人能保证最后一个字符是NULL。更糟糕的是,如果您使用的是Microsoft的msvc编译器,则不会编译。您应该使用内置id
函数。