Question: can someone help me fix my code the output should be like this student@student:~ / CSC 4 1 5 / AssignmentCreation / CSVThreads$ make test

can someone help me fix my code the output should be like this student@student:~/CSC415/AssignmentCreation/CSVThreads$ make test
./Bierman_Robert_HW4_csv
Header Column 00: Name - Last, First
Header Column 01: Age
Header Column 02: Favorite Color
Header Column 03: Comments
Data for line 1
Field 00: Bierman, Robert
Field 01: 943
Field 02: Blue
Field 03: What can I say, this is a large comment
Data for line 2
Field 00: Smith, John
Field 01: 28
Field 02: Green
Field 03: I want to have a multiple
line comment
Data for line 3
Field 00: Clark, Dwight "The Catch"
Field 01: 61
Field 02: Red & Gold
Field 03: 1981 NFC Championship
CSV Tests Passed
student@student:~/CSC415/AssignmentCreation/CSVThreads$
but my output is ./Girmay_Mulugeta_HW4_csv
Header Column 00: Name - Last, First
Header Column 01: Age
Header Column 02: Favorite Color
Header Column 03: Comments
Data for line 1
Field 00: Bierman, Robert
Field 01: 943
Field 02: Blue
Field 03: What can I say, this is a large comment"
Field Incorrect - Should be: What can I say, this is a large comment
make: ***[Makefile:76: test] Error 252
below is the code help me fix the correct code please. thank you
#include
#include
#include
#include "Girmay_Mulugeta_HW4_csv.h"
FILE *csv_file = NULL;
char **header = NULL;
char *current_line = NULL;
char **current_fields = NULL;
int num_fields =0;
char **tokenize_line(char *line){
// Initial size of tokens array
int initial_size =10;
int token_count =0;
char **tokens = malloc(initial_size * sizeof(char *));
char *token;
const char *delimiter =",";
const char *quote ="\"";
const char *escaped_quote ="\"\"";
token = strtok(line, delimiter);
while (token != NULL){
if (token_count >= initial_size){
// Resize the tokens array if needed
initial_size *=2;
tokens = realloc(tokens, initial_size * sizeof(char *));
}
tokens[token_count]= malloc((strlen(token)+1)* sizeof(char));
if (token[0]=='\"'){
strcpy(tokens[token_count], token +1);
while (token[strlen(token)-1]!='\"'){
token = strtok(NULL, delimiter);
if (token == NULL){
// Handle unexpected end of line
break;
}
strcat(tokens[token_count],",");
strcat(tokens[token_count], token);
}
tokens[token_count][strlen(tokens[token_count])-1]='\0';
} else {
strcpy(tokens[token_count], token);
}
// Handle escaped quotes
if (strstr(tokens[token_count], escaped_quote)!= NULL){
char *pos = strstr(tokens[token_count], escaped_quote);
while (pos != NULL){
memmove(pos +1, pos +2, strlen(pos +1));
pos = strstr(pos +2, escaped_quote);
}
}
token_count++;
token = strtok(NULL, delimiter);
}
// Resize the tokens array to its actual size
tokens = realloc(tokens,(token_count +1)* sizeof(char *));
tokens[token_count]= NULL;
return tokens;
}
char **csvopen(char *filename){
char *line = NULL;
size_t len =0;
if ((csv_file = fopen(filename,"r"))== NULL){
return NULL;
}
if (getline(&line, &len, csv_file)!=-1){
header = tokenize_line(line);
}
free(line);
return header;
}
char **csvnext(void){
char *line = NULL;
size_t len =0;
if (getline(&line, &len, csv_file)!=-1){
if (current_fields != NULL){
for (int i =0; current_fields[i]!= NULL; i++){
free(current_fields[i]);
}
free(current_fields);
}
current_fields = tokenize_line(line);
free(line);
return current_fields;
}
free(line);
return NULL;
}
char **csvheader(void){
return header;
}
int csvclose(void){
if (csv_file != NULL){
fclose(csv_file);
csv_file = NULL;
}
if (header != NULL){
for (int i =0; header[i]!= NULL; i++){
free(header[i]);
}
free(header);
header = NULL;
}
if (current_fields != NULL){
for (int i =0; current_fields[i]!= NULL; i++){
free(current_fields[i]);
}
free(current_fields);
current_fields = NULL;
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!