#include <stdio.h>
#include <stdlib.h>
/*******************************************************************************
*
* Program: Read CSV File Data To An Array Of Structs
*
* Description: Example of reading CSV file data into an array of structs in C.
*
* YouTube Lesson: https://www.youtube.com/watch?v=rbVt5v8NNe8
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*// https://github.com/portfoliocourses/c-example-code/blob/main/csv_to_struct_array.c
*******************************************************************************/
typedef struct
{
// members for the student's type, name, age and average
char type [5];
char name[50];
int age;
double average;
} Student;
// total number of students stored in the array of structs
#define TOTAL_STUDENTS 6
int main() {
// file pointer variable for accessing the file
FILE *file;
// Attempt to open the file file.csv for writing, if the file does not exist
// it will be created and if it does any existing content will be erased
// before we begin to write to the file.
file = fopen("Students.csv", "w");
// If the call to fopen() above is successful it will return a file pointer
// we can use to write to the file, and if it fails it will return NULL. So
// if file is set to NULL we know there has been an error opening the file,
// and we exit with an error message and status.
if (file == NULL)
{
printf("Error opening file.\n");
return 1;
}
int TotalStudents;
printf("no. of students\n");
scanf("%d", &TotalStudents);
Student *student;
student = malloc(sizeof( student) * TotalStudents);
int i;
printf("\nstudents information:\n\n");
https://stackoverflow.com/questions/22344953/how-to-use-malloc-or-new-in-struct
for (i = 0; i < TotalStudents; ++i) {
printf("enter the type:");
scanf(" %s", student[i].type);
printf("enter the name:");
scanf("%s", student[i].name);
printf("enter age:");
scanf("%d", &student[i].age);
printf("enter the average:");
scanf("%lf", &student[i].average);
}
for (int i = 0; i < TotalStudents; i++)
{
// Output to the file a line of comma separated values for the current
// Student struct. The placeholders %c %s %d and %.2f are used to output
// a char, a string, an int and a double respectively for the members/fields
// type, name, age and average. The %.2f is used to output the average
// with two decimal digits of accuracy.
//fprintf(file,"type, Name ,age, average\n");
fprintf(file,
"%-5s,%-10s,%-3d,%.2f\n",
student[i].type,
student[i].name,
student[i].age,
student[i].average);
// If there is an error writing to the file we identify that it has
// occurred using ferror(file) and exit with an error message and status
// if so.
if (ferror(file))
{
printf("Error writing to file.\n");
return 1;
}
}
// close the file since we are done working with it
fclose(file);
// output the total number of records written to the file
printf("\n%d records written.\n\n", TotalStudents);
// free memory when you are not using this.
free(student)
;
// student5 isnull
student = NULL
;
return 0;
}
Comments