I have to write user given data to a .txt file. I did with fprintf but only string did not write in the file? here is my code.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main()
{
struct student { // student record field
int st_id;
char st_name[20];
int st_age;
char st_dep[6];
float st_grade;
}line;
char name[20];
printf("Please Enter 6 digit student ID\n");
scanf("%d",&line.st_id);
printf("please Enter student name\n");
scanf("%s",name);
printf("%s",name);
printf("Please Enter student age\n");
scanf("%d", &line.st_age);
printf("Please Enter student Department(MATHS,CHEM,CSE,BME)\n");
scanf("%s",&line.st_dep[6]);
printf("Please Enter student grade(0-100)\n");
scanf("%f",&line.st_grade);
FILE * fp; // file open from here.
if ((fp = fopen ("file.txt", "a")) != NULL){
printf("%s",line.st_name);
fprintf(fp, "%d, %s, %d, %s,%f \n", line.st_id, line.st_name, line.st_age,line.st_dep,line.st_grade);
fclose(fp);
printf("data recorded\n");
}
else printf("Writing Error.\n");
return 0;
}
file.txt file is here
111111, , 23232, ,33.000000 // here string is missing?