2016-05-07 11 views
0

Guten Abend, arbeiten im auf einem Code für ein Projekt und ich habe folgendes Problem festgestellt: Die Datenspeicherung ist nicht richtig, nachdem das Programm geschlossen wird. Die Daten speichern als mehrere Arrays in einer TXT-Datei, aber es speichert seltsame Informationen, bitte helfen Sie. Bemerkung: Die info.txt ist hier, weil die erste Phase des Projektes (wir bei Phase 2) sind erforderlich, um, so dass, wenn es irgendwelche Probleme bitte ignorieren verursachen tut. Dies gilt auch für die leeren Funktionen bitte, in Add-Konto, wenn wir ein Konto sind wir verpflichtet, zu bewegen, um die Felder auf der rechten SeiteFehler bei der Rettung Daten, ungültige Daten

#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

sollten Sie einige Dateien schließen, irgendwo. –

Antwort

1

Erste Compiler-Warnungen hinzuzufügen. Wenn wir zuerst Warnungen korrigieren, wissen wir es besser.

$ 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); 

Sie können ein minimales Beispiel in http://ideone.com/ machen und oben nur die Datei-I/O und Verwendung printf und sehen, wo das Problem ist, wenn nicht in den Warnungen auszuschließen.

Die Warnungen bedeuten, dass Ihre Typen sind falsch, aber der Code ist noch runnable und Sie werden nicht definiertes Verhalten aus dem Programm oder ein ähnliches, wenn Sie verschiedene Arten mischen. Sei vorsichtig, was ein Zeiger ist und was Daten sind.

Verwandte Themen