Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Who can find solution for this problem? (http://www.go4expert.com/forums/solution-t29184/)

tenzinagato 9Oct2012 21:40

Who can find solution for this problem?
 
Create a structure called account wit data member acc no., ac type n balance. Now write a program to store record of 20 acc in a file named customer.txt den display only those acc no. wit blc less than 500

xpi0t0s 10Oct2012 02:56

Re: Who can find solution for this problem?
 
Homework?

tenzinagato 10Oct2012 10:37

Re: Who can find solution for this problem?
 
kindof...!!!

xpi0t0s 10Oct2012 21:27

Re: Who can find solution for this problem?
 
How far have you got and where are you stuck? Do you understand the requirements?

tenzinagato 10Oct2012 21:39

Re: Who can find solution for this problem?
 
Code:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct
{
        int account_number;
        char account_type[30];
        int balance;
}account;
void main()
{
        struct account acc[5];
        int i;
        for(i=0;i<5;i++)
        {
                printf("Enter Account Number : ");
                scanf("%d",&acc[i].account_number);
                printf("Enter Account Type : ");
                fflush(stdin);
                gets(acc[i].account_type);
                printf("Enter Balance : ");
                scanf("%d",&acc[i].balance);
        }

        FILE *ofp;
                for (i=0;i<5;i++)
                {
                        printf("%d \n",acc[i].account_number);
                        printf("%s \n",&acc[i].account_type);
                        printf("%d \n",acc[i].balance);
                        ofp = fopen("customer.txt", "wb");
                        fwrite(&acc[i], sizeof acc[i], 5, ofp);
                }
       
                fclose(ofp);

        system("cls");
printf("Account with balance higher then Rs.500/-");
        printf("******************************************\n");

        for(i=0;i<5;i++)
        {
                if(acc[i].balance>500)
                {
                        printf("Record no. %d \n\n",i);
                        printf("Account Number : ");
                        printf("%d \n",&acc[i].account_number);
                          printf("Account Type : ");
                          printf("%s \n",&acc[i].account_type);
                          printf("Balance : ");
                          printf("Rs.%d \n",&acc[i].balance);
                          printf("\n\n");
                }           
        }
}

this is all i could do...the struct is not written in the txt file... there must be some mistake...
and actually the second output printing is to be done by reading the values from the txt file..but i have directly printed it..

xpi0t0s 11Oct2012 01:48

Re: Who can find solution for this problem?
 
Your fopen and fwrite shouldn't be inside the for loop, further if you check the documentation for fwrite()( you'll see you only need to call fwrite once, not five times.

tenzinagato 11Oct2012 04:12

Re: Who can find solution for this problem?
 
i tried it...n something seems to be written in the customer.txt file...
it looks something like this
"H( ž@ 8@ ~x( K@  PR R p( *v @ R ~ˆ( ˜@  ”( w6\v ~( r w ~ž32w ~ *( wš ( E w€@ ~ €@ ~ "
wats the problem here...
here is the code:
FILE *ofp;
for (i=0;i<5;i++)
{
printf("%d \n",acc[i].account_number);
printf("%s \n",&acc[i].account_type);
printf("%d \n",acc[i].balance);
}
ofp = fopen("customer.txt", "w");
fwrite(&acc[i], sizeof acc[i], 5, ofp);
fclose(ofp);

xpi0t0s 11Oct2012 13:46

Re: Who can find solution for this problem?
 
Of course you get junk. i at that point is 5, which is off the end of the array. You need to give fwrite the pointer to the START of the memory block to be written to the file.

tenzinagato 12Oct2012 08:21

Re: Who can find solution for this problem?
 
sorry... i don't get it... can u explain...!!!

xpi0t0s 12Oct2012 13:04

Re: Who can find solution for this problem?
 
What do you think will be the output of this code? If you're not sure, try it.
Code:

for (i=5;i<10;i++)
{
printf("%d \n",acc[i].account_number);
printf("%s \n",&acc[i].account_type);
printf("%d \n",acc[i].balance);
}

Also you might want to try this code so that you can see why I said i=5:

Code:

int i;
for (i=0; i<5; i++)
{
printf("Inside loop, i=%d\n",i);
}
printf("After loop, i=%d\n",i);



All times are GMT +5.5. The time now is 15:36.