FCLOSE()
#include <stdio.h>
#include <string.h>
void run_main_program() {
FILE *file = fopen("employees.csv", "r"); // Step 1: Open file
if (!file) { // Step 2: Error check
printf("Error: Could not open file.\n");
}
else{
char line[100]; // Buffer to store each line
// Step 3: Read lines one by one
while (fgets(line, sizeof(line), file)) {
// Remove newline character at the end if present
line[strcspn(line, "\n")] = '\0';
// Split line into fields using comma as delimiter
char delimiter[] = ",";
char *field = strtok(line, delimiter);
// Step 4: Print each field with spacing
while (field != NULL) {
printf("%s ", field);
field = strtok(NULL, delimiter); // Keep splitting until found NULL
}
printf("\n"); // Newline after each row
}
fclose(file); // Step 5: Close the file
}
}
int main() {
run_main_program();
return 0;
}
Last updated