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...
Make pesodocode of this code. void loadData() { char line[MAX_LENGTH]; const char * delimiter = ",\n";...
Make pesodocode of this code. void loadData() { char line[MAX_LENGTH]; const char * delimiter = ",\n"; FILE * fp; fp = fopen("patients.txt", "r"); char c; if (fp == NULL) { printf("file not found"); return; } while (fp != NULL) { if (fgets(line, MAX_LENGTH - 1, fp) == NULL) break; if (line[1] == '\0') break; patients[patientCount].id = atoi(strtok(line, delimiter)); strcpy(patients[patientCount].name, strtok(NULL, delimiter)); patients[patientCount].age = atoi(strtok(NULL, delimiter)); patients[patientCount].annualClaim = strtok(NULL, delimiter); patients[patientCount].plan = atoi(strtok(NULL, delimiter)); strcpy(patients[patientCount].contactNum, strtok(NULL, delimiter)); strcpy(patients[patientCount].address, strtok(NULL, delimiter)); strcpy(line,...
C Implement the function Append (char** s1, char** s2) that appends the second character array to...
C Implement the function Append (char** s1, char** s2) that appends the second character array to the first, replaces the first array with the result and replaces the second character array with the original first array. For example, if the first character array is "hello" and the second is "world" at the end of the function the new value of the first character array should be"helloworld" and the second array should be "hello". If the input is invalid the function...
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.
Using C: Implement function types that takes no input, declares 3 variables of type char, 3...
Using C: Implement function types that takes no input, declares 3 variables of type char, 3 of type short, 3 of type int, and 3 of type double---in that order---and prints the addresses of the 12 variables---in the same order---in both hex (use %p conversion instruction) and unsigned long format. &a1 = 0x7ffd45e3ac0f, 140725776002063 &a2 = 0x7ffd45e3ac0e, 140725776002062 &a3 = 0x7ffd45e3ac0d, 140725776002061 &b1 = 0x7ffd45e3ac0a, 140725776002058 &b2 = 0x7ffd45e3ac08, 140725776002056 ...
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 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT