Advertisement
Patrickquinn1212

wrte csv

Sep 19th, 2022 (edited)
902
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /*******************************************************************************
  5. *
  6. * Program: Read CSV File Data To An Array Of Structs
  7. *
  8. * Description: Example of reading CSV file data into an array of structs in C.
  9. *
  10. * YouTube Lesson: https://www.youtube.com/watch?v=rbVt5v8NNe8
  11. *
  12. * Author: Kevin Browne @ https://portfoliocourses.com
  13. *
  14. *// https://github.com/portfoliocourses/c-example-code/blob/main/csv_to_struct_array.c
  15. *******************************************************************************/
  16.  
  17.  
  18.  
  19. typedef struct
  20. {
  21.     // members for the student's type, name, age and average
  22.     char type [5];
  23.     char name[50];
  24.     int age;
  25.     double average;
  26.  
  27. } Student;
  28.  
  29. // total number of students stored in the array of structs
  30. #define TOTAL_STUDENTS 6
  31.  
  32. int main() {
  33.  
  34.     // file pointer variable for accessing the file
  35.     FILE *file;
  36.  
  37.     // Attempt to open the file file.csv for writing, if the file does not exist
  38.     // it will be created and if it does any existing content will be erased
  39.     // before we begin to write to the file.
  40.     file = fopen("Students.csv", "w");
  41.  
  42.     // If the call to fopen() above is successful it will return a file pointer
  43.     // we can use to write to the file, and if it fails it will return NULL.  So
  44.     // if file is set to NULL we know there has been an error opening the file,
  45.     // and we exit with an error message and status.
  46.     if (file == NULL)
  47.     {
  48.         printf("Error opening file.\n");
  49.         return 1;
  50.     }
  51.  
  52.     int TotalStudents;
  53.     printf("no. of students\n");
  54.     scanf("%d", &TotalStudents);
  55.  
  56.     Student *student;
  57.     student = malloc(sizeof( student) * TotalStudents);
  58.  
  59.     int i;
  60.     printf("\nstudents information:\n\n");
  61.     https://stackoverflow.com/questions/22344953/how-to-use-malloc-or-new-in-struct
  62.     for (i = 0; i < TotalStudents; ++i) {
  63.         printf("enter the type:");
  64.         scanf(" %s", student[i].type);
  65.         printf("enter the name:");
  66.         scanf("%s", student[i].name);
  67.         printf("enter age:");
  68.         scanf("%d", &student[i].age);
  69.         printf("enter the average:");
  70.         scanf("%lf", &student[i].average);
  71.     }
  72.     for (int i = 0; i < TotalStudents; i++)
  73.     {
  74.         // Output to the file a line of comma separated values for the current
  75.         // Student struct.  The placeholders %c %s %d and %.2f are used to output
  76.         // a char, a string, an int and a double respectively for the members/fields
  77.         // type, name, age and average.  The %.2f is used to output the average
  78.         // with two decimal digits of accuracy.
  79.  
  80.         //fprintf(file,"type, Name      ,age, average\n");
  81.         fprintf(file,
  82.                 "%-5s,%-10s,%-3d,%.2f\n",
  83.                 student[i].type,
  84.                 student[i].name,
  85.                 student[i].age,
  86.                 student[i].average);
  87.  
  88.         // If there is an error writing to the file we identify that it has
  89.         // occurred using ferror(file) and exit with an error message and status
  90.         // if so.
  91.         if (ferror(file))
  92.         {
  93.             printf("Error writing to file.\n");
  94.             return 1;
  95.         }
  96.     }
  97.  
  98.     // close the file since we are done working with it
  99.     fclose(file);
  100.     // output the total number of records written to the file
  101.     printf("\n%d records written.\n\n", TotalStudents);
  102.  
  103.  
  104.     // free memory when you are not using this.
  105.     free(student)
  106.  
  107.             ;
  108.     // student5 isnull
  109.     student = NULL
  110.  
  111.             ;
  112.     return 0;
  113. }
  114.  
  115.  
  116.  
Tags: strttok
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement