This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

reading data from sequential Access file have problem


Dear Gcc help or any linux-programmer:

  I have two c programs, which get input from stdio then store in a file
called client.dat, then the other program inquire from that client.dat

/* Create a sequential file */
#include <stdio.h>
 
main()
{
        int account;
        char name[30];
        float balance;
        FILE *cfPtr;     /* cfPtr = clients.dat file pointer */
 
        if ((cfPtr = fopen("clients.dat", "w")) == NULL)
                printf("File could noot be opend\n");
        else {
                printf("Enter the acount, name, and balance.\n");
                printf("Ente EOF to end input.\n");
                printf("? ");
                scanf("%d%s%f", &account, name, &balance);
 
                while (!feof(stdin)) {
                        fprintf(cfPtr, "%d %s %.2f\n",
                                account, name, balance);
                        printf("? ");
                        scanf("%d%s%f", &account, name, &balance);
                }
 
                fclose(cfPtr);
        }
        return 0;
}


/************************/
/* Credit inquiry program */
#include <stdio.h>
main()
{
        int request, account;
        float balance;
        char name[30];
        FILE *cfPtr;
 
        if((cfPtr = fopen("clients.dat", "r")) == NULL)
                printf("File could not be opened\n");
        else {
                printf("Enter request\n"
                        " 1 - List accounts with zero balances\n"
                        " 2 - List accounts with credit balances\n"
                        " 3 - List accounts with debit balances\n"
                        " 4 - End of run\n? ");
                scanf("%d", &request);
 
                while (request != 4) {
                        fscanf(cfPtr, "%d%s%f", &account, name,
&balance);
 
                        switch (request) {
                                case 1:
                                        printf("\nAccounts with zero
balances:\n");
                                        while (!feof(cfPtr)) {
 
                                                if (balance == 0)
                                                       
printf("%-10d%-13s%7.2f\n",
                                                                       
account, name, balance);
 
                                                fscanf(cfPtr, "%d%s%f",
                                                               
&account, name, &balance);
                                        }
 
                                        break;
                                    case 2:
                                        printf("\nAccounts with credit
balances:\n");
 
                                        while (!feof(cfPtr)) {
 
                                                if (balance < 0)
                                                       
printf("%-10d%-13s%7.2f\n",
                                                                       
account, name, balance);
 
                                                fscanf(cfPtr, "%d%s%f",
                                                               
&account, name, &balance);
                                        }
 
                                        break;
                                case 3:
                                        printf("\nAccounts with debit
balnces:\n");
 
                                        while (!feof(cfPtr)) {
 
                                                if (balance > 0)
                                                       
printf("%-10d%-13s%7.2f\n");
 
                                                fscanf(cfPtr, "%d%s%f",
                                                               
&account, name, &balance);
                                        }
 
                                        break;
                        }
                        rewind(cfPtr);
                        printf("\n? ");
                        scanf("%d", &request);
                }
 
                printf("end of run.\n");
                fclose(cfPtr);
        }
        return 0;
}
/****************************/

Then I tried to execute them by enter a one data line <return>  cntl-D

[root@localhost ~/c_program]# ./p11_3
Enter the acount, name, and balance.
Ente EOF to end input.
? 100 Jones 244.98
? [root@localhost ~/c_program]# ls -l
total 76
-rwxr-xr-x    1 root     root        15218 Dec 30 11:36 a.out
-rw-r--r--    1 root     root           17 Dec 30 12:00 clients.dat
-rw-r--r--    1 root     root          146 Dec 29 16:28 count.c
-rw-r--r--    1 root     root           65 Dec 30 08:44 hello.c
-rw-r--r--    1 root     root          213 Dec 29 11:45 hello.cpp
-rwxr-xr-x    1 root     root        14542 Dec 30 11:24 p11_3
-rw-r--r--    1 root     root          603 Dec 30 11:20 p11_3.c
-rwxr-xr-x    1 root     root        15218 Dec 30 11:27 p11_8
-rw-r--r--    1 root     root         1465 Dec 30 11:04 p11_8.c
-rw-r--r--    1 root     root          249 Dec 29 11:42 p9-8.cpp
[root@localhost ~/c_program]# ./p11_8
Enter request
 1 - List accounts with zero balances
 2 - List accounts with credit balances
 3 - List accounts with debit balances
 4 - End of run
? 1
 
Accounts with zero balances:
 
? 2
 
Accounts with credit balances:
 
? 3
 
Accounts with debit balnces:
-1073743908               -2.00
 
? 4
end of run.
[root@localhost ~/c_program]#
/*--------------------------------------------------------------------------*/

why my result is not expected at debit balance is 100 Jones 244.98? but
ugly number -1073743908             -200?

gcc 2.96 is what I use(in mandrake8)
sincere eric, fsshl@centurytel.net, http://www.ezinfocenter.com/4436786


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]