C文件更新和创建帮助

时间:2011-02-09 04:49:07

标签: c

我有两个.dat(ascii)文件。两者都排序了。

1:包含的客户端文件;帐号,姓名,余额 2:包含的事务文件;帐号,日期,销售额(交易金额)

我想要完成的是创建一个新的更新客户端文件,该文件根据添加或减去匹配交易的销售额来更新客户的余额。

到目前为止,我的代码可以让我:

1:如果客户端的事务不超过一个,则代码运行完美地将.dat文件与客户端及其更新的余额一起写入。

2:如果客户有多个交易我的代码几乎完美运行,因为它会在屏幕上打印更新的客户和帐户,例如:

1詹姆斯540.00 2约翰762.00 3保罗414.00 4 sam 502.00

将显示

,但由于john有两个事务,所以包含

时创建的.dat文件

1詹姆斯540.00 2约翰662.00 2约翰762.00 3保罗414.00 4 sam 502.00

我的问题在于,我需要找到一种方法让创建的.dat只为每个客户端包含一行(帐号)

我的代码附有任何帮助将不胜感激。

#include <stdio.h>
#include <string.h>
int main(void)
{ 
    int account, matches=0;     /* account number */
    char date[ 30 ]; /* account Date */
    double balance, saleamount,total=0, temp;;  /* account SaleAmount */
    int transaccount;
    char name [ 30 ];
    char lastname[30];
    int lastaccount=-1;
    double lastbalance;



    FILE *cfPtr;     /* cfPtr = clients.dat file pointer */
    FILE *ctPtr;    /* cfPtr = transaction.dat file pointer */
    FILE *cfPtr2;    /* cfPtr2 = new client file */

    cfPtr2 = fopen( "clientupdate.dat", "w" );

    /* fopen opens file; exits program if file cannot be opened */ 
    if ( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL ) {
        printf( "clients could not be opened\n" );
        /*fflush(stdin);*/
    } /* end if */
    else
        if( ( ctPtr = fopen( "transactions.dat", "r" ) ) == NULL)
        {
            printf( "File could not be opened\n" );
            /*fflush(stdin);*/
        }
    else { /* read account, date,name, balance and SaleAmount from files */
        fscanf( cfPtr, "%d%s%lf", &account, &name, &balance );
        /*fflush(stdin);*/

        fscanf( ctPtr, "%d%s%lf", &transaccount, &date, &saleamount );
        /*fflush(stdin);*/

        printf( "%-13s%-10s%s\n", "  Account", "Name", "Balance" );
        printf("|----------------------------------|\n");
        while( !feof(ctPtr))
        {   

            while( !feof(cfPtr) &&matches==0 )
            {   
                if(account == transaccount)
                {
                    if (lastaccount != account) {
                    if (lastaccount != -1)
                    printf("  %-10d%-10s%.2lf\n", lastaccount, lastname, lastbalance);
                    lastaccount = account;
                    strcpy (lastname, name);
}

                    matches=1;
                    total=0;
                    temp = balance+saleamount;
                    total = total + temp;
                    balance = total; 
                    lastbalance = balance;
                }
                else
                {

                    fscanf( cfPtr, "%d%s%lf", &account, &name, &balance );
                    /*fflush(stdin);*/
                }   

            }
            fprintf( cfPtr2, "%d %s %.2lf\n", account, name, total );
            fscanf( ctPtr, "%d%s%lf", &transaccount, &date, &saleamount );
            /*fflush(stdin);*/
           matches=0;

        }
    }
       if (lastaccount != -1)
    printf("  %-10d%-10s%.2lf\n", lastaccount, lastname, lastbalance);
        fclose( cfPtr2 );
        getchar();
    return 0;
}

3 个答案:

答案 0 :(得分:1)

正如所承诺的,这是一个父子更新的快速例子。我没有打扰检查文件打开错误,因为你已经在代码中使用了它们,并且真正的应用程序需要检查的不仅仅是ffcanf的EOF,以便检测无效的文件格式。

#include <stdlib.h>
#include <stdio.h>

#define bool int
#define false 0
#define true !false

#define MAX_ACCOUNT_NUMBER 99999

typedef struct
{
 int accountNumber;
 char accountName[30];
 float accountTotal;
} ClientRecordType;

typedef struct
{
 int accountNumber;
 char transactionDate[30];
 float transactionAmount;
} TransactionRecordType;

FILE *oldClientFile;
FILE *newClientFile;
FILE *transactionFile;


void getNextClient(FILE *p_clientFile, ClientRecordType *p_clientRecord)
{
 if (EOF == fscanf(p_clientFile, "%d%s%f", 
                   &p_clientRecord->accountNumber,
                   &p_clientRecord->accountName,
                   &p_clientRecord->accountTotal))
   p_clientRecord->accountNumber = MAX_ACCOUNT_NUMBER;
}


void getNextTransaction(FILE *p_transactionFile,
                        TransactionRecordType *p_transactionRecord)
{
 if (EOF == fscanf(p_transactionFile, "%d%s%f", 
                   &p_transactionRecord->accountNumber,
                   &p_transactionRecord->transactionDate,
                   &p_transactionRecord->transactionAmount))
   p_transactionRecord->accountNumber = MAX_ACCOUNT_NUMBER;
}


void writeUpdatedClientRecord(FILE *p_newClientFile,
                              ClientRecordType *p_clientRecord)
{
 fprintf(p_newClientFile, "%d %s %.2f ",
         p_clientRecord->accountNumber,
         p_clientRecord->accountName,
         p_clientRecord->accountTotal);
} 


bool performTransactionUpdate(FILE *p_oldClientFile,
                              FILE *p_newClientFile,
                              FILE *p_transactionFile)
{
 ClientRecordType clientRecord;
 TransactionRecordType transactionRecord;

 getNextClient(p_oldClientFile, &clientRecord);
 getNextTransaction(p_transactionFile, &transactionRecord);
 while (MAX_ACCOUNT_NUMBER != clientRecord.accountNumber)
 {
  if (clientRecord.accountNumber == transactionRecord.accountNumber)
  {
   clientRecord.accountTotal += transactionRecord.transactionAmount;
   getNextTransaction(p_transactionFile, &transactionRecord);
  }
  else if (clientRecord.accountNumber < transactionRecord.accountNumber)
  {
   writeUpdatedClientRecord(p_newClientFile, &clientRecord);
   getNextClient(p_oldClientFile, &clientRecord);
  }
  else
  {
   return false;
  }
 }
 if (MAX_ACCOUNT_NUMBER != transactionRecord.accountNumber) return false;
 return true;
}


int main(int argc, char *argv[])
{
 oldClientFile = fopen("clients.dat", "r");
 newClientFile = fopen("newclients.dat", "w");
 transactionFile = fopen("transactions.dat", "r");

 if (performTransactionUpdate(oldClientFile,
                              newClientFile,
                              transactionFile))
   printf("\nUpdate completed without errors!\n");
 else
   printf("\nUnsorted files or invalid transactions encountered\n");

 close(newClientFile);
 close(oldClientFile);
 close(transactionFile);
 return 0;
}

答案 1 :(得分:0)

一种典型的方法是将客户端文件加载到内存中,针对每个客户端处理事务文件,然后将客户端数据转储回磁盘。

如果您更喜欢以旧方式做事,您当然可以在处理之前将交易文件排序为客户订单。

答案 2 :(得分:0)

我最后得到了一个答案的帮助,一位同学提出了这个工作代码。感谢您帮助我弄清楚如何使其正常工作。

#include <stdio.h>
#include <string.h>
int main(void)
{ 
    int account;     /* account number */
    char date[ 30 ]; /* account Date */
    double balance, saleamount; /* account SaleAmount */
    int transaccount;
    char name [ 30 ];
    int newaccount;
    char newname[ 30];
    double newbalance;
    int match=0;



    FILE *cfPtr;     /* cfPtr = clients.dat file pointer */
    FILE *ctPtr;    /* cfPtr = transaction.dat file pointer */
    FILE *cfPtr2;    /* cfPtr2 = new client file */

    cfPtr2 = fopen( "clientupdate.dat", "w" );

    /* fopen opens file; exits program if file cannot be opened */ 
    if ( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL ) {
        printf( "clients could not be opened\n" );

    } /* end if */
    else
        if( ( ctPtr = fopen( "transactions.dat", "r" ) ) == NULL)
        {
            printf( "File could not be opened\n" );

        }
    else { /* read account, date,name, balance and SaleAmount from files */
        fscanf( cfPtr, "%d%s%lf", &account, &name, &balance );


        fscanf( ctPtr, "%d%s%lf", &transaccount, &date, &saleamount );
        fscanf( cfPtr2, "%d%s%lf", &newaccount, &newname, &newbalance );
        while ( !feof( cfPtr ) )
        {
            transaccount=1;
            newbalance=balance;
            while ( !feof( ctPtr ) )
            {
                if(transaccount==account&&match==0)
                {
                 newbalance = balance + saleamount;
                 match++;
                }
                else
                    if(transaccount==account&&match>0)
                     newbalance = newbalance + saleamount;

                fscanf( ctPtr, "%d%s%lf", &transaccount, date, &saleamount );
            }
            rewind( ctPtr);
            match=0;

        fprintf( cfPtr2, "%d %s %.2f\n", account, name, newbalance );
        fscanf( cfPtr, "%d%s%lf", &account, name, &balance );
        }

        rewind( cfPtr2);
    }
        fclose( cfPtr2 );
        fclose( cfPtr );
        fclose( ctPtr );
        getchar();
    return 0;
}