Click here to Skip to main content
16,016,580 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
#include <stdio.h>
#include <stdlib.h>

typedef struct student
{
    int rno;
    char name[20];
    struct subject
    {
        int scode;
        char sname[20];
        int mark;
    } sub[3];
    int total;
    float per;
} student;

void create(){
    FILE *fp;
    student *s;
    int n, i, j;
    printf("Enter how many students you want : ");
    scanf("%d", &n);
    s = (student *)calloc(n, sizeof(student));
    fp = fopen("mystudents.txt", "w");
    for (i = 0; i < n; i++)
    {
        s[i].total = 0;
        s[i].per = 0;

        printf("Enter roll no. : ");
        scanf("%d", &s[i]);

        fflush(stdin);

        printf("Enter name : ");
        scanf("%[^\n]s", s[i].name);

        for (j = 0; j < 3; j++)
        {
            printf("Enter marks of subjects%d : ", j + 1);
            scanf("%d", &s[i].sub[j].mark);
            s[i].total += s[i].sub[j].mark;
        }
        fwrite(&s[i], sizeof(student), 1, fp);
    }
    fclose(fp);
}

int main()
{
create();
    return 0;
}


What I have tried:

program is running as intended , but when I check content of the file except name ,it's printing some random characters , not sure what's happening there?
Posted
Updated 21-Apr-22 20:39pm
v2
Comments
Shao Voon Wong 22-Apr-22 1:00am    
fwrite() writes binary bytes which are not human-readable bytes. If you want human-readable content you have to call fprintf() on every member of your struct. Please remember to call fflush() to flush the unwritten contents from the buffer to the file before calling fclose().

Quote:
program is running as intended , but when I check content of the file except name ,it's printing some random characters , not sure what's happening there?

Your problem is your expectations, you write C program with python like expectations.
If you read documentation about printf and fwrite, you will not see anywhere something telling that printing a pointer to a structure will convert it to a human readable format like json.
C++
fwrite(&s[i], sizeof(student), 1, fp);

This just dump the piece of memory where the instance of student structure is stored.
If you want to print the data of the instance of student, you have to print each part manually while telling how to format them.
 
Share this answer
 
Comments
CPallini 22-Apr-22 3:00am    
5.
Patrice T 22-Apr-22 3:04am    
Thank you
Shao Voon Wong 22-Apr-22 3:37am    
5, concise explanation!
Patrice T 22-Apr-22 3:44am    
Thank you.
xguybluehat 22-Apr-22 9:19am    
hm, rightly said, my brother also say so.
I started programming a month ago .My first language is c .So , I think it'll take time for me to understand things more deeply .I'll take this as advice n surely work on that. thanks for your advice. Your explanation is superb.
Do yourself a favour and rewrite that.
Write a function that accepts a single parameter (a pointer to a student) and gets the relevant information from the user:
C
student* GetStudentDetail (student* s)
   {
   ...
   return s;
   }

Use the debugger to test that that works properly when you pass it a valid student pointer:
C
int main()
   {
   Student s;
   GetStudentDetail(&s);
   }

Write a second function PrintStudentDetail which takes the same parameter and displays a human readable student:
C
student* PrintStudentDetail (student* s)
   {
   ...
   return s;
   }
Then again use the debugger to prove that works:
C
int main()
   {
   Student s;
   PrintStudentDetail(GetStudentDetail(&s));
   }

Then modify the main function to support multiple Students: prove that works.

Then write a function called SerializeStudents which takes an array of Student objects and outputs them to a file:
C
void SerializeStudents(char* path, student students[])
   {
   ...
   return s;
   }

And then look at the file content using a hex editor, since the data will not be human readable.
 
Share this answer
 
Comments
CPallini 22-Apr-22 2:11am    
5.

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