gpt4 book ai didi

c - fputs 使我的程序崩溃

转载 作者:行者123 更新时间:2023-11-30 18:36:12 27 4
gpt4 key购买 nike

以下编程在“fputs”的第一个实例处崩溃,尝试将“fname”打印到文件 Member_inf。我还尝试打印常量字符串“abcdefg”,但仍然失败。到底做错了什么?看来是文件的初始化。如果可能的话,请只帮助解决“fputs”问题,我宁愿在遇到其他问题时首先尝试自己尝试。

#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;
}

最佳答案

  1. 不要强制转换 malloc 的结果。
  2. 您应该始终测试 malloc 的返回值(它不应该为 null)
  3. scanf("%s",&*fname); 应为 scanf("%s",fname);
    或者,更好的是,fgets(fname);
    (显然,这适用于所有字符串输入)。

一句建议 - 函数有返回值 - 利用它们

编辑
我的注释,更好,关于使用 fgets() 意味着停止使用 scanf() 并使用 fgets() > 仅。

关于c - fputs 使我的程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42631835/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com