Iam trying to write student details in text file, But, i found that the file was filled with some gaurbage values.
plz anyone can help me to find the solution.
here is my code...
plz anyone can help me to find the solution.
here is my code...
Code:
#include<stdio.h> #include<conio.h> struct student { char roll_no[10]; char name[10]; char previous_payment[5]; char present_payment[5]; char total_payment[5]; }; int toint(char str[]) { int len = strlen(str); int i, num = 0; for (i = 0; i < len; i++) { num = num + ((str[len - (i + 1)] - '0') * pow(10, i)); } return num; } void tostring(char str[], int num) { int i, rem, len = 0, n; n = num; while (n != 0) { len++; n = n / 10; } for (i = 0; i < len; i++) { rem = num % 10; num = num / 10; str[len - (i + 1)] = rem + '0'; } str[len] = '\0'; } void main() { int previous,present,total,choice; char ch,str1[10],str2[10]; struct student std_data; FILE *data,*report; printf("Enter the following details of student\n"); printf("enter roll.no of the student:"); scanf("%s",&std_data.roll_no); printf("enter student name:"); scanf("%s",std_data.name); printf("enter the following bill payments of Roll.no : %s \n",std_data.roll_no); printf("Enter previous payment:"); scanf("%d",&previous); tostring(str1,previous); printf("str1 = %s\n",str1); printf("Enter present payment:"); scanf("%d",&present); tostring(str2,present); printf("str2 = %s\n",str2); strcpy(std_data.previous_payment,str1); printf("std_data.previous_payment = %s\n",std_data.previous_payment); strcpy(std_data.present_payment,str2); printf("std_data.present_payment = %s\n",std_data.present_payment); total = previous + present; printf("total = %d\n",total); tostring(std_data.total_payment,total); printf("std_data.total_payment = %s\n",std_data.total_payment); printf("\n"); data = fopen("student_details.txt","a"); if(data == NULL) { printf("error in opening input_details file\n"); exit(1); } fwrite(&std_data,sizeof(struct student),1,data); fclose(data); return; }
Comment