In: Computer Science
Create a C module convertTo_csv.c that will implement the function loadAndConvert(const char* file)
The code must be split into 2 files (.h file and a .c file). The convertTo_csv.c will have the function implementation in ti and the The convertTo_csv.h file will have the declaration.
One argument will be given to the function, that is the name of the input file that needs to be converted. A function will create a new csv file called output.csv
Know that the loadAndConvert method will be called by other modules and you can have helper methods to aid in your code but the helper methods will not be called by other modules, they will not be public.
The original text file looks as follows:
Hanna Sara Ava Alex Eric
25 34 19 42 29
Maine Italy Romania Bengal India
The output CSV file should have this format:
Hanna, 25, Maine
Sara, 34, Italy
Ava, 19, Romania
Alex, 42, Bengal
Eric, 29, India
//please make sure to create input.csv file before running the code
//and input.csv should contains 3 lines
//first line contains names separated by space
//first line contains ages(i thought it was ages if not name it in you like) separated by space
//first line contains cities separated by space
//Person.h
#ifndef __PERSON_H
#define __PERSON_H
//structure to hold person details
typedef struct Person
{
char name[50];
char age[10];
char city[50];
struct Person *next;
}Person;
//sturcture which contains words read from input file
typedef struct Word
{
char text[50];
struct Word *next;
}Word;
#endif
//driver.c
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include "Person.h"
//create a new word by given text and return it
Word * getWord(char *text)
{
Word *word = (Word *) malloc(sizeof(Word));
strcpy(word -> text, text);
word->next = NULL;
return word;
}
//print persons read from input file
void print(Person *first)
{
Person *temp = first;
while(temp != NULL)
{
printf("\nName:
%s\n",temp->name);
printf("Age :
%s\n",temp->age);
printf("City:
%s\n",temp->city);
temp = temp->next;
printf("\n-----------------------------\n");
}
}
//create Persons by names,ages and cities read from input
file
Person * createPersons(Word *names, Word *ages, Word *cities)
{
//store each pointer of type Word
Word *name = names;
Word *age = ages;
Word *city = cities;
//create a Person struct pointer to hold each person
data
//and another one to hold newly created Person struct
pointer
Person *first = NULL,*person;
//it should need each data to create new person
//any missing data is there it won't create a new
person
while(name != NULL && age != NULL &&
city != NULL)
{
//create new person and add it to
the first
person = (Person *)
malloc(sizeof(Person));
strcpy(person -> name , name
->text);
strcpy(person -> age , age ->
text);
strcpy(person -> city,city ->
text);
person -> next = first;
first = person;
name = name ->next;
age = age -> next;
city = city -> next;
}
return first;
}
//function that loads the data from input file
//and creates a structure of Persons and returns it
Person* loadData(char file[50])
{
char line[1000];
FILE *fptr;
if ((fptr = fopen(file, "r")) == NULL)
{
printf("Error! No Such file as input.csv to load data");
exit(1);
}
int len;
//names to store names in the input file
Word *names = NULL;
//ages to store ages in the input file
Word *ages = NULL;
//cities to store cities in the input file
Word *cities = NULL;
Word *word;
int pos = 0;
//get a line from input file
while(fgets(line, sizeof(line), fptr))
{
//new line is also read from input
file
//so set it to null
len = strlen(line);
line[len-1] = '\0';
//token to split by space
char *token = strtok(line,"
");
while(token!= NULL)
{
//create a word
using token
word =
getWord(token);
//if pos is 0 it
means line is 0, line 0 contains names so populate names
if(pos ==
0)
{
if(names == NULL)
{
names = word;
}
else
{
word ->next = names;
names = word;
}
}
//if pos is 0 it
means line is 0, line 0 contains ages so populate ages
else if(pos ==
1)
{
if(ages == NULL)
{
ages = word;
}
else
{
word ->next = ages;
ages = word;
}
}
//if pos is 0 it
means line is 0, line 0 contains names so populate names
else if(pos ==
2)
{
if(cities == NULL)
{
cities = word;
}
else
{
word ->next =
cities;
cities = word;
}
}
token =
strtok(NULL, " ");
}
//increment line positin
pos++;
}
fclose(fptr);
//finally create Persons by usign createPersons() fn
and names,ages,cities of type Word read from input file
Person *first = createPersons(names, ages,
cities);
//return the created person structure
return first;
}
//function that writes Persons to the output.csv
void writeToOutput(Person *persons)
{
FILE *fptr = fopen("output.csv","w");
Person *person = persons;
while(person != NULL)
{
//write person details
fprintf(fptr,"%s,%s,%s\n",person->name,person->age,person->city);
person = person -> next;
}
fclose(fptr);
}
int main()
{
Person *persons = loadData("input.csv");
printf("######## Persons Read from input.csv
######\n");
print(persons);
printf("Writing to output.csv\n");
writeToOutput(persons);
return 0;
}
//input.csv
Hanna Sara Ava Alex Eric
25 34 19 42 29
Maine Italy Romania Bengal India
//output.csv
Hanna,25,Maine
Sara,34,Italy
Ava,19,Romania
Alex,42,Bengal
Eric,29,India