i need help finding my mistake.
Write a program that reads a multi-line CSV file and reads any number of values
<X>,<Y>,...,<N>
per line. Example file1.csv:4,10,9,13 -1 7,13,100
After reading in, the values are to be output in the format
<X>/<Y>/ <N>
:4/10/9/13 -1 7/13/100
If opening the file fails, it should print "Error opening file\n".
Hint: Use fscanf for formatted reading from the file and strtok to split the lines.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct profile
{
char a[30];
char b[30];
char c[30];
char d[30];
char e[30];
char f[30];
struct profile* next;
} profile;
profile* createProfile(char* line);
struct profile* addFirst(profile* item, profile* head);
void printList(struct profile* head);
struct profile* addLast(profile* item, profile* head);
int main() {
FILE* file;
char input[256];
printf("Enter filename: ");
scanf("%s", input);
file = fopen(input, "r");
if(file == NULL)
printf("Error opening file\n");
else
{
profile* head = NULL;
while(fscanf(file, "%255s\n", input) != EOF)
{
profile* profile = createProfile(input);
head = addLast(profile, head);
}
fclose(file);
printList(head);
}
return 0;
}
profile* createProfile(char* line)
{
profile* newProfile = malloc(sizeof(profile));
char* token;
token = strtok(line, ",");
if(token != NULL)
strcpy(newProfile->a, token);
token = strtok(NULL, ",");
if(token != NULL)
strcpy(newProfile->b, token);
token = strtok(NULL, ",");
if(token != NULL)
strcpy(newProfile->c, token);
token = strtok(NULL, ",");
if(token != NULL)
strcpy(newProfile->d, token);
token = strtok(NULL, ",");
if(token != NULL)
strcpy(newProfile->e, token);
token = strtok(NULL, ",");
if(token != NULL)
strcpy(newProfile->f, token);
newProfile->next = NULL;
return newProfile;
}
struct profile* addFirst(profile* item, profile* head)
{
item->next = head;
return item;
}
void printList(struct profile* head)
{
if(head != NULL)
{
printf("%s/%s/%s/%s/%s/%s\n",
head->a,
head->b,
head->c,
head->d,
head->e,
head->f);
printList(head->next);
}
}
struct profile* addLast(profile* item, profile* head)
{
if (head == NULL)
{
return item;
}
profile* current = head;
while(current->next != NULL)
{
current = current->next;
}
current->next = item;
return head;
}
tried a lot nothing worked. any help is greatly appreciated.