保存数据时出错,数据无效

Posted

技术标签:

【中文标题】保存数据时出错,数据无效【英文标题】:Errors in saving Data, invalid data 【发布时间】:2016-05-07 19:42:25 【问题描述】:

晚上好,我正在为一个项目编写代码,我遇到了以下问题:程序关闭后数据没有正确保存。数据保存为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);

【问题讨论】:

你应该在某个地方关闭一些文件。 【参考方案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 并查看问题出在哪里,如果不是在上面的警告中。

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

【讨论】:

以上是关于保存数据时出错,数据无效的主要内容,如果未能解决你的问题,请参考以下文章

保存核心数据时出错

保存/加载 C++ 数据时出错

在 Core Data (IOS) 中保存数据时出错

Swift:从 url 保存视频数据时出错

ios/objective-c:保存核心数据时出错

尝试将文件路径保存到数据库时出错