FYI - this is a coding project so i cant make changes to the spreadsheet
I have to save column 1,2,4,5, and 14 in arrays.I printed these columns and this is the output. This is the output right now
Since I need to convert Column 1 and Column 14 to float arrays. How do i remove the quotations from all column arrays??
the values in csv are saved and seperated by commas and quotes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COLS 18 // Maximum number of columns in the CSV file
#define MAX_ROWS 211 // Maximum number of rows in the CSV file
int main() {
char filename[] = "statscan_diabetes.csv";
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error opening file %s\n", filename);
return 1;
}
char line[1024];
char *token;
int row_count = 0;
char ref_date[MAX_ROWS][11];
char geo[MAX_ROWS][100];
char age_group[MAX_ROWS][20];
char sex[MAX_ROWS][10];
char value_str[MAX_ROWS][10];
float value[MAX_ROWS];
// Read the header line and ignore it
fgets(line, 1024, fp);
// Read each line of the file and store values in arrays
while (fgets(line, 1024, fp) != NULL && row_count < MAX_ROWS) {
token = strtok(line, ",");
int col_count = 0;
while (token != NULL && col_count < MAX_COLS) {
if (col_count == 0) {
strcpy(ref_date[row_count], token);
} else if (col_count == 1) {
strcpy(geo[row_count], token);
} else if (col_count == 3) {
strcpy(age_group[row_count], token);
} else if (col_count == 4) {
strcpy(sex[row_count], token);
} else if (col_count == 13) {
strcpy(value_str[row_count], token);
}
token = strtok(NULL, ",");
col_count++;
}
row_count++;
}
double ontario_total = 0, quebec_total = 0, bc_total = 0, alberta_total = 0;
int ontario_count = 0, quebec_count = 0, bc_count = 0, alberta_count = 0;
//for (int i=0;i<=row_count;i++)
// {
//if(geo[i]=="Quebec")
//{
// quebec_total=quebec_total+
//}
// }
// for (int i = 0; i < row_count; i++) {
// printf("%s\n", value_str[i]);
// }
//Print the values stored in the arrays
for (int i = 0; i < row_count; i++) {
printf("%s %s %s %s %s\n", ref_date[i], geo[i], age_group[i], sex[i], value_str[i]);
}
fclose(fp);
return 0;
}
At first i tried using float arrays to store the numbers in Column 1 and 14 and tried printing them but the output gave me 0.0 as all values.
I realized since the numbers are surrounded by quotes its reading the quotes as well.
How do I remove the quotes from all arrays?
Rather than
strcpy(ref_date[row_count], token);, which risks buffer overflow and copies the undesired", call a helper function and have it skip"and avoid copying too many characters.And call it and like-wise for the other tokens
A possible implementation below is simply something thrown together. OP is unclear on what should happen if
"lacks a match", input is too long, etc.For conversation to
float, make a different helper function that skips over a leading"(if there), callsstrotd(), checks for conversion success, looks to trailing non-numeric text, etc.A weakness to OP's code is lack of error detection: overlong line, too long a token, missing
", too many", non-numeric input forfloat, etc.This is tolerable for a learner exercise, yet robust code would detect and handle bad input. Helper functions are a first step to divide & conquer to help achieve these goals.