Question

In: Computer Science

Create a C module convertTo_csv.c that will implement the function loadAndConvert(const char* file) The code must...

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

Solutions

Expert Solution

//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


Related Solutions

There is a C function decodeMorse(const String & string, char message[]). This function examines the binary...
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary string and iteratively constructs a decimal value (val) and width of each binary pattern (separated by spaces), until a space or a null character ('\0') is encountered in the string. Once a space or a null character is found, this function should call the assembly code (decode_morse()) to obtain the corresponding ASCII value, for the current val and width, and place the ASCII value...
Translate the following C++ program to Pep/9 assembly language. const char chConst = '+'; char ch1;...
Translate the following C++ program to Pep/9 assembly language. const char chConst = '+'; char ch1; char ch2; int main() { cin.get(ch1); cin.get(ch2);    cout << ch1 << chConst << ch2;    return 0; }
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes...
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes the character x from string s. For s[] = “America”, a call to deleteSymbol(s, ‘a’) converts s[] = “Ame”
Below is the code for the isSmaller() member function: bool Location::isSmaller(const Location & r) const {...
Below is the code for the isSmaller() member function: bool Location::isSmaller(const Location & r) const { // ORDER OF COMPARISONS ==> 1st country; 2nd state; 3rd city int a = strcmp(country,r.country); // need strcmp // country and r.country are arrays and in C++ the expression country < r.country will compare address, not data // the data must be compared cell by corresponding cell in the 2 arrays, which takes place in the strcmp() function // There are three possible return...
MUST BE DONE IN C (NOT C++) Your create a program that can implement the cases...
MUST BE DONE IN C (NOT C++) Your create a program that can implement the cases in which the initial unit is Fahrenheit or something not recognizable. Your program should incorporate Fahrenheit to Celsius, Fahrenheit to Kelvin and unknown initial units (display an error message for this last one). You must use functions to calculate Fahrenheit degrees.
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char *state; int time_spent; }; Given Array: struct patients* patientsArray[4] = {&p1, &p2, &p3, &p4}; Create two functions one for pushing this array to the stack and one for popping the variables such as p1 p2 p3 p4 by its ID
Implement the following function in the PyDev module functions.py and test it from a PyDev module...
Implement the following function in the PyDev module functions.py and test it from a PyDev module named : def statistics(n): """ ------------------------------------------------------- Asks a user to enter n values, then calculates and returns the minimum, maximum, total, and average of those values. Use: minimum, maximum, total, average = statistics(n) ------------------------------------------------------- Parameters: n - number of values to process (int > 0) Returns: minimum - smallest of n values (float) maximum - largest of n values (float) total - total of...
Implement the following function in the PyDev module functions.py and test it from a PyDev module...
Implement the following function in the PyDev module functions.py and test it from a PyDev module named : def gym_cost(cost, friends): """ ------------------------------------------------------- Calculates total cost of a gym membership. A member gets a discount according to the number of friends they sign up. 0 friends: 0% discount 1 friend: 5% discount 2 friends: 10% discount 3 or more friends: 15% discount Use: final_cost = gym_cost(cost, friends) ------------------------------------------------------- Parameters: cost - a gym membership base cost (float > 0) friends...
c program //the file's size in bytes unsigned int fileSize(const char* name){     return 0; }...
c program //the file's size in bytes unsigned int fileSize(const char* name){     return 0; } //same as fileSize except if the file is a directory then it recursively finds the size of directory and all files it contains unsigned int fileSizeRec(const char* name){     return 0; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT