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

标签 c database sorting save

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

最佳答案

获取编译器警告。如果我们先修复警告,那么我们就会知道得更清楚。

$ 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 - 保存数据时出错,数据无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37092942/

相关文章:

c - 在 C 中传递 IP 地址字符串

python - Zpsycopg2 与 zope 2 兼容吗?

javascript - 在页面加载时在 Ext.grid 上显示排序箭头

c - 验证 scanf 条目

c - Makefile - 如何组织和构建多个库版本

c - 回调的成本如何?

c - 选择排序程序产生不正确的输出

r - 在 gather 函数中使用变量

mysql - 如何在规范化数据库中加入 tag_map

r - 训练期间的节点排序算法(R : randomForest)