2016-05-07 37 views
0

晚上好,即时通讯工作的项目代码,我遇到了以下问题:程序关闭后数据没有正确保存。数据保存为一个txt文件中的多个数组,但它可以保存奇怪的信息,请帮助。注意:info.txt在这里是因为项目需要的第一阶段(我们在第二阶段),所以除非它没有引起任何问题,否则请忽略它。请同时使用空函数,在添加帐户时,我们需要将阵列向右移动错误保存数据,无效数据

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#define MAXSIZE 100 

void displayMainMenu(); 
void displayAccounts(); 
void addAccount(int ids[],double balances[],long phones[],char types[],int size); 
void closeAccount(int ids[], double balances[],long phones[],char types[], int size); 
void viewAccountDetails(int ids[],double balances[],long phones[],char types[],int size); 
void modifyAccountDetails(); 
void addAmount(); 
void withdrawAmount(); 
void uploadDataFile(int ids[],double balances[],long phones[],char types[],int size); 
void updateDataFile(int ids[], double balances[],long phones[], char types[],int size); 

int main() { 
    int choice,i=0,j=0,status; 
    int id[MAXSIZE]; 
    char name1[MAXSIZE][30],name2[MAXSIZE][30]; 
    long number[MAXSIZE]; 
    char type[MAXSIZE]; 
    double balance[MAXSIZE]; 
    FILE*inp; 
    inp=fopen("info.text","r"); 
    do { 
     status=fscanf(inp ,"%d",&id[i]); 
     fscanf(inp ,"%s%s" ,name1[i],name2[i]); 
     fscanf(inp ,"%ld" ,&number[i]); 
     fscanf(inp ,"%c" ,&type[i]); 
     fscanf(inp ,"%lf" ,&balance[i]); 
     i++; 
    } while(status!=EOF); 
    fclose(inp); 
    uploadDataFile(id, balance, number, type, i); 
    printf("Information Loaded successfully\n"); 
    do { 
     printf("--------main menu---------\n"); 
     printf("1- Add Account\n"); 
     printf("2- View All Accounts \n"); 
     printf("3- Add Amount\n"); 
     printf("4- Withdraw Amount\n"); 
     printf("5- View Account Details\n"); 
     printf("6- Modify Account\n"); 
     printf("7- Close an Account\n"); 
     printf("8- Exit\n"); 
     printf("please enter your choice:"); 
     scanf("%d" , &choice); 

     switch(choice) { 
      case 1: addAccount(id,balance,number,type,i); 
        break; 
      case 2: displayAccounts(id,balance,number,type,i); 
        break; 
      case 3: addAmount(); 
        break; 
      case 4: withdrawAmount(); 
        break; 
      case 5: viewAccountDetails(id,balance,number,type,i); 
        break; 
      case 6: modifyAccountDetails(); 
        break; 
      case 7: closeAccount(id,balance,number,type,i); 
        break; 
      case 8: displayMainMenu(); 
        break; 
      default: printf("error\n"); 
     } 
    } while (choice!=8); 
    uploadDataFile(id, balance, number, type, i); 
    printf("Thank you,the information has been updated successfully!\n"); 

    return 0; 
} 

void displayMainMenu() { 
    printf("displays the main menu\n"); 
} 

void displayAccounts(int ids[],double balances[],long phones[], 
        char types[],int size) { 
    int i; 
    for(i=0;i<size;i++) 
     printf("The id is:%d\t\t The balance is:%f\nThe phone number is:%d\t The type is:%s\n",ids[i],balances[i],phones[i],types[i]); 
} 

void addAccount(int ids[],double balances[],long phones[],char types[],int size) { 
    int i=0,temp,id,flag,j; 
    double temp2,balance; 
    long temp3,phone; 
    char temp4,type; 
    printf("Please enter the account's new id\n"); 
    scanf("%d",&id); 
    if(size<MAXSIZE) { 
     while(i<size) { 
      if(id==ids[i]) { 
       flag=1; 
       break; 
      } else { 
       flag=0; 
       i++; 
      } 
     } 
    } 
    if(flag==0) { 
     for(i=size;i>0;i--) { 
      ids[i]=ids[i-1]; 
      balances[i]=balances[i-1]; 
      phones[i]=phones[i-1]; 
      types[i]=types[i-1]; 
     } 
     ids[0]=id; 
     for(j=0;j<size-1;j++) { 
      for(i=1;i<size;i++) { 
       temp=ids[i-1]; 
       temp2=balances[i-1]; 
       temp3=phones[i-1]; 
       temp4=types[i-1]; 
       ids[i-1]=ids[i]; 
       balances[i-1]=balances[i]; 
       phones[i-1]=phones[i]; 
       types[i-1]=types[i]; 
       ids[i]=temp; 
       balances[i]=temp2; 
       phones[i]=temp3; 
       types[i]=temp4; 
      } 
     } 
     printf("Please enter the account's balance\n"); 
     scanf("%lf",&balance); 
     printf("Please enter the account's phone number\n"); 
     scanf("%ld",&phone); 
     printf("Please enter the account's type \n"); 
     scanf(" %c",&type); 
     balances[0]=balance; 
     phones[0]=phone; 
     types[0]=type; 
     printf("Account added!\n"); 
    } else 
     ("the Id is already in use\n"); 
} 

void closeAccount(int ids[], double balances[],long phones[], 
        char types[], int size) { 
    int i,j; 
    int id; 
    printf("please enter the id u want to close\n"); 
    scanf("%d",&id); 
    if(size>0) { 
     for(i=0;j<size;j++) { 
      for(i=0;i<size-1;i++) { 
       if(id==ids[i]) { 
        ids[i]=ids[i+1]; 
        balances[i]=balances[i+1]; 
        phones[i]=phones[i]; 
        types[i]=types[i+1]; 
       } else 
        printf("error\n"); 
       break; 
      } 
     } 
    } 
} 

void viewAccountDetails(int ids[],double balances[],long phones[],char types[],int size) { 
    int id,i=0,flag; 
    printf("Please input the id for the desired account\n"); 
    scanf("%d",&id); 
    if(size>0) { 
     while(i<size) { 
      if(id==ids[i]) { 
       flag=1; 
       break; 
      } else { 
       flag=0; 
       i++; 
      } 
     } 
    } 
    if(flag==1) 
     printf("The account information are:\n id: %d/t balance: %f\t phone number:%ld\t Type: %c",ids[i],balances[i],phones[i],types[i]); 
    else 
     printf("Error: The account was not found\n"); 
} 

void modifyAccountDetails() { 
    printf("modifies a certain account details "); 
} 

void addAmount() { 
    printf("increases the balance variable"); 
} 

void withdrawAmount() { 
    printf("decreases the balance variable"); 
} 

void uploadDataFile(int ids[],double balances[],long phones[],char types[],int size) { 
    FILE*inp; 
    inp=fopen("Accounts.txt","r"); 
    int i=1; 
    while(i<size) { 
     fscanf(inp ,"%d",&ids[i]); 
     fscanf(inp ,"%ld" ,&phones[i]); 
     fscanf(inp ,"%c" ,&types[i]); 
     fscanf(inp ,"%lf" ,&balances[i]); 
     i++; 
    } 
} 

void updateDataFile(int ids[], double balances[], long phones[], char types[], int size) { 
    FILE*out; 
    out=fopen("Accounts.txt","w"); 
    fprintf(out,"%d\n",ids); 
    fprintf(out,"%f\n",balances); 
    fprintf(out,"%ld\n",phones); 
    fprintf(out,"%c\n",types); 
} 
+0

您应该关闭某些文件。 –

回答

1

获取编译器警告。如果我们先解决警告,那么我们会更好地了解。

$ gcc database.c 
database.c: In function ‘displayAccounts’: 
database.c:86:11: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘long int’ [-Wformat=] 
    printf("The id is:%d\t\t The balance is:%f\nThe phone number is:%d\t The t 
     ^
database.c:86:11: warning: format ‘%s’ expects argument of type ‘char *’, but argument 5 has type ‘int’ [-Wformat=] 
database.c: In function ‘updateDataFile’: 
database.c:243:16: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat=] 
    fprintf(out,"%d\n",ids); 
       ^
database.c:244:16: warning: format ‘%f’ expects argument of type ‘double’, but argument 3 has type ‘double *’ [-Wformat=] 
    fprintf(out,"%f\n",balances); 
       ^
database.c:245:16: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 3 has type ‘long int *’ [-Wformat=] 
    fprintf(out,"%ld\n",phones); 
       ^
database.c:246:16: warning: format ‘%c’ expects argument of type ‘int’, but argument 3 has type ‘char *’ [-Wformat=] 
    fprintf(out,"%c\n",types); 

你可以做一个小例子,在http://ideone.com/,只有排除的文件I/O和使用printf和看到的问题是,如果没有上面的警告。

警告意味着您的类型错误,但代码仍然可运行,并且如果您混合使用不同类型的程序或类似程序,您将获得未定义的行为。要小心什么是指针和数据。