Who can find solution for this problem?
|
Light Poster
|
|
| 9Oct2012,21:40 | #1 |
|
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
|
|
Mentor
|
![]() |
| 10Oct2012,02:56 | #2 |
|
Homework?
|
|
Light Poster
|
|
| 10Oct2012,10:37 | #3 |
|
kindof...!!!
|
|
Mentor
|
![]() |
| 10Oct2012,21:27 | #4 |
|
How far have you got and where are you stuck? Do you understand the requirements?
|
|
Light Poster
|
|
| 10Oct2012,21:39 | #5 |
|
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");
}
}
}
and actually the second output printing is to be done by reading the values from the txt file..but i have directly printed it.. |
|
Mentor
|
![]() |
| 11Oct2012,01:48 | #6 |
|
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.
|
|
Light Poster
|
|
| 11Oct2012,04:12 | #7 |
|
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 ąż~Ō’( rw ąż~ž32w ąż~ *’( ’’’’wš ģ’( Ew€@ ąż~ €@ ąż~ " 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); |
|
Mentor
|
![]() |
| 11Oct2012,13:46 | #8 |
|
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.
|
|
Light Poster
|
|
| 12Oct2012,08:21 | #9 |
|
sorry... i don't get it... can u explain...!!!
|
|
Mentor
|
![]() |
| 12Oct2012,13:04 | #10 |
|
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);
}
Code:
int i;
for (i=0; i<5; i++)
{
printf("Inside loop, i=%d\n",i);
}
printf("After loop, i=%d\n",i);
|

