Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include
#include
calc(struct salesdata *salesptr);
struct salesdata{
		char transno[4];
		int salesno;
		int prodno;
		int unit_sold;
		float value_of_sale;
		};
void main()
{

FILE *fp;
struct salesdata salesrec,*ptr;
clrscr();
ptr=fopen("rand.txt","w");
printf("Enter transsaction no: ");
scanf("%3s",salesrec.transno);
fflush(stdin);

printf("Enter salesman no: ");
scanf("%d",&salesrec.salesno);
fflush(stdin);

printf("Enter the product number: ");
scanf("%d",&salesrec.prodno);
fflush(stdin);

printf("Enter unit sold: ");
scanf("%d",&salesrec.unit_sold);
fflush(stdin);

(float)salesrec.value_of_sale=calc(&salesrec);
printf("Value of sale: %f");

ptr=&salesrec;
fwrite(ptr, sizeof(struct salesdata),1,fp);


getch();
}
calc(struct salesdata *salesptr)
{
static float prod_rate[4]={ 12.0, 65.0, 30.0, 20.5 };
salesptr->value_of_sale=(float)salesptr->unit_sold* prod_rate[salesptr->prodno-1];
return salesptr->value_of_sale;
}

I'm writing the record to rand.txt file. Writing is being done but it's shown in an encoded form. So, please help....
Posted
Updated 28-Dec-11 1:14am
v2
Comments
Wendelius 28-Dec-11 7:14am    
Pre tags added

It isn't in an encoded form but its written to file as binary data. If you would like to see it as text you should convert the values to text (or string). You can't convert the struct with a one single command and write it to file the way you do because there is no such conversion function and also the size of the struct wouldn't match the text equivalent. For a start, you can convert int values using itoa. For writing text files, have a look at the link:
http://www.cprogramming.com/tutorial/cfileio.html[^]

Good luck!
 
Share this answer
 
v2
Help with what? You are writing an array of bytes (your structure) into a file so that is how it will appear. If you want it in some sort of text format then you need to write some serialization routine that will convert each individual field to text and write the text string to your file. However, be aware that doing so for your float value will cause potential loss of precision when reading the values back in. As a general rule you should never use float to represent monetary values.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900