文件处理fprintf错误

时间:2016-03-04 04:25:58

标签: c

我得到了关于文件处理的任务,关于创建银行账户,然后它会告诉您输入帐号,姓名,密码和初始余额。我的程序工作但它在文件上打印错误。我该怎么办?

计划:

#include<stdio.h>  
#include<conio.h>  
#include<windows.h>  
#include<ctype.h>  

struct accacc
{  
    char accno[5];  
    char accna[50];  
    int accpin[4];  
    int accba;  
};  
main()
{  
    FILE *fp;  
    struct accacc acc;  
    char ch;  
    char t[5];  
    fp=fopen("Accounts.txt","a");  
    printf("ENTER ACCOUNT NUMBER:\t");  
    gets(acc.accno);  
    printf("ENTER ACCOUNT NAME:\t");  
    scanf("\n");  
    gets(acc.accna);  
    printf("ENTER ACCOUNT PIN:\t");  
    scanf("%d",&acc.accpin);  
    printf("ENTER INITIAL BALANCE:\t");  
    scanf("%d",&acc.accba);  
    printf("CREATE ANOTHER ONE?:\t");  
    scanf(" %c",&ch);  
    fprintf(fp,"%s\t%s\t%d\t%d",acc.accno,acc.accna,acc.accpin,acc.accba);  
    if(toupper(ch)=='Y')
    {  
        do
        {
            system("cls");  
            printf("ENTER ACCOUNT NUMBER:\t");  
            scanf("\n");  
            gets(t);  
            if(strcmp(acc.accno,t)==0)
            {   
                printf("ACCOUNT ALREADY EXISTS!");  
            }
            else
            {
                printf("ENTER ACCOUNT NAME:\t");  
                scanf("\n");  
                gets(acc.accna);  
                printf("ENTER ACCOUNT PIN:\t");  
                scanf("%d",&acc.accpin);  
                printf("ENTER INITIAL BALANCE:\t");  
                scanf("%d",&acc.accba);  
                printf("CREATE ANOTHER ONE?:\t");  
                scanf(" %c",&ch );  
                fprintf(fp,"%s\t%s\t%d\t\t%d\t\n",t,acc.accna,acc.accpin,acc.accba);  
            }
        }while(toupper(ch)=='Y');  
    }  
    else
    {  
        fclose(fp);  
    }  
    getch();  
}

1 个答案:

答案 0 :(得分:1)

您已将acc.accpin的{​​{1}}转换为int* %d fprintf()int调用#include<stdio.h> #include<stdlib.h> /* add this to use system() */ #include<string.h> /* add this to use strcmp() */ #include<ctype.h> struct accacc{ char accno[5]; char accna[50]; int accpin; /* change type from int[4] to int */ int accba; }; char* safer_gets(char* outbuf, size_t max){ size_t idx = 0; int input; if(max == 0) return NULL; while(idx + 1 < max && (input = getchar()) != EOF && input != '\n'){ outbuf[idx++] = input; } if (idx == 0 && input == EOF) return NULL; outbuf[idx] = '\0'; return outbuf; } int main(void){ FILE *fp; struct accacc acc; char ch; char t[5]; fp=fopen("Accounts.txt","a"); if(fp == NULL){ /* add error check */ perror("fopen"); return 1; } /* stop using gets() and add error check for input */ printf("ENTER ACCOUNT NUMBER:\t"); if(safer_gets(acc.accno,sizeof(acc.accno)) == NULL){ fputs("input error\n", stderr); return 1; } printf("ENTER ACCOUNT NAME:\t"); scanf("\n"); if(safer_gets(acc.accna,sizeof(acc.accna)) == NULL){ fputs("input error\n", stderr); return 1; } printf("ENTER ACCOUNT PIN:\t"); if(scanf("%d",&acc.accpin) != 1){ fputs("input error\n", stderr); return 1; } printf("ENTER INITIAL BALANCE:\t"); if(scanf("%d",&acc.accba) != 1){ fputs("input error\n", stderr); return 1; } printf("CREATE ANOTHER ONE?:\t"); if(scanf(" %c",&ch) != 1){ fputs("input error\n", stderr); return 1; } fprintf(fp,"%s\t%s\t%d\t%d",acc.accno,acc.accna,acc.accpin,acc.accba); if(toupper(ch)=='Y'){ do{ system("cls"); printf("ENTER ACCOUNT NUMBER:\t"); scanf("\n"); safer_gets(t,sizeof(t)); if(strcmp(acc.accno,t)==0){ printf("ACCOUNT ALREADY EXISTS!"); } else { printf("ENTER ACCOUNT NAME:\t"); scanf("\n"); if(safer_gets(acc.accna,sizeof(acc.accna)) == NULL){ fputs("input error\n", stderr); return 1; } printf("ENTER ACCOUNT PIN:\t"); if(scanf("%d",&acc.accpin) != 1){ fputs("input error\n", stderr); return 1; } printf("ENTER INITIAL BALANCE:\t"); if(scanf("%d",&acc.accba) != 1){ fputs("input error\n", stderr); return 1; } printf("CREATE ANOTHER ONE?:\t"); if(scanf(" %c",&ch ) != 1){ fputs("input error\n", stderr); return 1; } fprintf(fp,"%s\t%s\t%d\t\t%d\t\n",t,acc.accna,acc.accpin,acc.accba); } }while(toupper(ch)=='Y'); fclose(fp); /* add fclose() here */ } else{ fclose(fp); } } ,因此您调用了未定义的行为

修复其他问题,您的代码可能是这样的:

.then

请注意,此代码中的“检查重复的帐号”不起作用。