-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample-8.c
65 lines (53 loc) · 1.56 KB
/
Example-8.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 1024
#define MAX_NAME_LENGTH 50
// Structure to hold CSV data
typedef struct {
char name[MAX_NAME_LENGTH];
int age;
float score;
} Student;
// Function to write data to a CSV file
void writeCSV(const char *filename) {
FILE *file = fopen(filename, "w");
if (file == NULL) {
printf("Error opening file!\n");
return;
}
fprintf(file, "Name,Age,Score\n"); // Write header
fprintf(file, "Alice,20,85.5\n");
fprintf(file, "Bob,22,90.0\n");
fprintf(file, "Charlie,19,78.2\n");
fclose(file);
printf("CSV file written successfully.\n");
}
// Function to read data from a CSV file
void readCSV(const char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Error opening file!\n");
return;
}
char line[MAX_LINE];
fgets(line, sizeof(line), file); // Read header
printf("Reading CSV data:\n");
while (fgets(line, sizeof(line), file)) {
Student student;
char *token = strtok(line, ",");
if (token) strcpy(student.name, token);
token = strtok(NULL, ",");
if (token) student.age = atoi(token);
token = strtok(NULL, ",");
if (token) student.score = atof(token);
printf("Name: %s, Age: %d, Score: %.2f\n", student.name, student.age, student.score);
}
fclose(file);
}
int main() {
const char *filename = "students.csv";
writeCSV(filename);
readCSV(filename);
return 0;
}