Question

According to Wikipedia , a comma-separated values (CSV) file is a delimited text file that uses...


According to Wikipedia , a comma-separated values (CSV) file is a delimited text file that uses a
comma to separate values. A CSV file stores tabular data (numbers and text) in plain text. Each line of
the file is a data record. Each record consists of one or more fields, separated by commas. The use of
the comma as a field separator is the source of the name for this file format.
A company has text data that is not CSV; however, they need to convert their data to CSV format.
The original text file looks like the following extract of the first 3 lines:
Jake Maria Desmond Ali Maria
25 34 19 42 29
Montreal Wawa Head-Smashed-In-Buffalo-Jump Bangor Saint-Louis-du-Ha-Ha
The needed CSV file should look like this:
Jake, 25, Montreal
Maria, 34, Wawa
Desmond, 19, Head-Smashed-In-Buffalo-Jump
Ali, 42, Bangor
Maria, 29, Saint-Louis-du-Ha-Ha
You need to use modular programming techniques to enhance code readability and team work.
Remember that modular programming is [citing Wikipedia again] a software design technique that
emphasizes separating the functionality of a program into independent, interchangeable modules, such
that each contains everything necessary to execute only one aspect of the desired functionality.
Your mission is to implement a C program that will implement the needed logic to read a text file and
convert it to CSV. The program should also be able to read the generated csv file and print its content
to the screen.
Let’s create a separate module to convert the input file to a csv file, then another module that reads a
csv file and print its content to the screen. We should also create a third module that is the main one
having the main function. This last module will also implement multiple utility functions to search the
csv file, adding a new record to it and also deletes a record from it.
You can follow these steps:

1. Create a C module called convert_to_csv.c that implements the following function: void
load_and_convert(const char* filename)
Split the code in 2 files:
● convert_to_csv.c that contains the implementation
● convert_to_csv.h that contains the declaration
The function takes one argument that is the name of the input file to be converted.
It creates a new csv file called output.csv. Note that load_and_convert function is the public
interface that will be called by other modules. You may create other private helper functions to
help you break down your code into multiple logical smaller functions. These functions
however should not be callable by other modules.
2. Create a C module called read_csv.c that implements the following function: void
read_csv(const char* csv_filename)
The function will read a csv file and print its content to the screen line by line
Split the code in 2 files:
● read_csv.c that contains the implementation
● read_csv.h that contains the declaration
Note that read_csv function is the public interface that will be called by other modules. You
may create other private helper functions to help you break down your code into multiple
logical smaller functions. These functions however should not be callable by other modules.
3. Write a third module main.c that will contain your main function as described below as well as
the following functions:
1. Write a function that will check whether a name exists in the csv file. If it does exist,
print its record to the screen. If the name is repeated multiple times in the csv file, then
print to the screen all the records for that name:
void find_name(const char* csv_filename, const char* name);
If the name was not found, then print “Name not found” to the screen.
2. Write a function that will add a new record to the end of the csv file. The signature of
the function will be:
void add_record(const char* csv_filename, const char* name, const int age, const
char* city);
3. Write a function that will delete a record from the csv file. The signature of the function
will be:
void delete_record(const char* csv_filename, const char* name);
delete only the first occurence of name from the csv file.

The main() function that will be used to test your functions is provided as follow, please use it as the
main function of your code:
int main ()
{
/* Question 1 */
load_and_convert( "input.txt" );
/* Question 2 */
read_csv( "output.csv" );
/* Question 3.1 */
find_name( "output.csv" , "Maria" );
find_name( "output.csv" , "Jason" ); //Jason doesn't exist
/* Question 3.2 */
add_record( "output.csv" , "Jason" , 36 , "Skookumchuk" );
read_csv( "output.csv" ); // to print to the screen
/* Question 3.3 */
delete_record( "output.csv" , "Maria" );
read_csv( "output.csv" ); // to print to the screen
return 0 ;
}

PLEASE ANSWER QUESTION 3

0 0
Add a comment Improve this question Transcribed image text
Answer #1

3.

CODE:

FILE read_csv.h:

/*
This file contains the declarations.
The trim funtion is a helper function which helpsto process each line, remove unnecessary commmas and spaces.
This is static funtion. and is only callable by by read_csv module(file) .

read_csv is the required funtion and a public interface callable by any module.

*/

static void trim( char*);

extern void read_csv(const char*);

FILE read_csv.c:

#include <stdio.h>

/*
stdio.h contains the required definitions for input/output methods.
*/
/*
The trim funtion is a helper function which helpsto process each line, remove unnecessary commmas and spaces.
This is static funtion. and is only callable by by read_csv module(file) .
*/
#define MAX_LENGTH 1024 // Assuming that a line contains no more than 1024 characters
static void trim( char* line){
// iterate through every character of the line.

for(int i=0; line[i]!='\0';i++){
// replace the character ',' by a tabspace \ t
if(line[i]==',')
line[i] ='\t';
// replace the character space ' ' by a tabspace \ t
if(line[i]==' ')
line[i]='\t';
// replace the character '\n' by a null character
//denotes the end of the line
if(line[i]=='\n')
line[i]='\0';
}

}
extern void read_csv(const char* csv_filename){

char line[MAX_LENGTH]; // declare a variable to hold data of a line
FILE *fp = fopen(csv_filename , "r");
// fp is a file pointer
//fopen(filename,mode) . mode parameter is the mode in which the file is opened
// it can be 'w' or 'r' which denotes write or read operation

// if fp is 0 or NULL, file is not present in the directory
if(!fp){
// print error
printf("Error Opening File\n");
//return to the calling funtion which is main.
return ;
}
// Print the header. can remove if unnecessary
printf("Column1\t\tColumn2\t\tColumn3\n");

//fgets scans a file line-by-line and returns null when an EOF is encountered.
//1024 is the assumed length of line
// fgets scans from the file specified by the file pointer. (fp, third argument)
while (fgets(line, MAX_LENGTH, fp) != NULL) {
trim(line); // process the line for printing
//print line
printf("%s\n",line);

}

}

FILE output.csv:

Jake, 25, Montreal
Maria, 34, Wawa
Desmond, 19, Head-Smashed-In-Buffalo-Jump
Ali, 42, Bangor
Maria, 29, Saint-Louis-du-Ha-Ha

FILE driver.c: (for only question 2, however your main.c works)

#include "read_csv.h"
int main(){
read_csv("output.csv");

}

OUTPUT :

Add a comment
Know the answer?
Add Answer to:
According to Wikipedia , a comma-separated values (CSV) file is a delimited text file that uses...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function that will add a new record to the end of the csv file....

    Write a function that will add a new record to the end of the csv file. The signature of the function will be: void add_record(const char* csv_filename, const char* name, const int age, const char* city); format of the csv file is ; Jake, 25, Montreal Maria, 34, Wawa Desmond, 19, Head-Smashed-In-Buffalo-Jump Ali, 42, Bangor Maria, 29, Saint-Louis-du-Ha-Ha

  • Consider the data file “assignment-07-input.csv. The data contains of 4 different things separated by comma –...

    Consider the data file “assignment-07-input.csv. The data contains of 4 different things separated by comma – origin airport code (3 characters), destination airport code (3 characters), airline code (2 characters), passenger count (integer). There is no header row, so you do not have to deal with it. Write code to do the following: • The program will take 3 extra command line arguments & use a makefile to run. o Input file name o Output file name o Airline code...

  • Write code to read student data from a csv file and add (prepend) that into a...

    Write code to read student data from a csv file and add (prepend) that into a linked list. Assume the code is written in only one file - main.c. You are REQUIRED to write the struct definition for the linked list node. . The input file name is taken from command line argument. . You can write everything except the struct definition in the main function, or can create multiple functions up to you. Following is the sample file data....

  • soccerApp.cpp is used to test your code and contains the main function. No changes to this...

    soccerApp.cpp is used to test your code and contains the main function. No changes to this file are necessary. Inside of the soccerPlayer.cpp file, implement the three member functions that are specified in soccerPlayer.hpp. The member function definition for print is already provided. soccerPlayer.cpp: #include "soccerPlayer.hpp" void TopTwoPlayer::print() const { cout << left << setw(8) << "Name: " << setw(18) << name << setw(10) << "Country:" << setw(18) << country << setw(14) << "appearances:" << right << setw(3) << appearances...

  • In c programming . Part A: Writing into a Sequential File Write a C program called...

    In c programming . Part A: Writing into a Sequential File Write a C program called "Lab5A.c" to prompt the user and store 5 student records into a file called "stdInfo.txt". This "stdInfo.txt" file will also be used in the second part of this laboratory exercise The format of the file would look like this sample (excluding the first line) ID FIRSTNAME LASTNAME GPA YEAR 10 jack drell 64.5 2018 20 mina alam 92.3 2016 40 abed alie 54.0 2017...

  • In this module you learned how to create polymorphic code using virtual functions. A file filter...

    In this module you learned how to create polymorphic code using virtual functions. A file filter reads an input file, transforms it in some way, and writes the results to an output file. Write an abstract file filter class that defines a pure virtual function for transforming a character. Create one subclass of your file filter class that performs encryption, another that transforms a file to all uppercase, and another that creates an unchanged copy of the original file. The...

  • In this module you learned how to create polymorphic code using virtual functions. A file filter...

    In this module you learned how to create polymorphic code using virtual functions. A file filter reads an input file, transforms it in some way, and writes the results to an output file. Write an abstract file filter class that defines a pure virtual function for transforming a character. Create one subclass of your file filter class that performs encryption, another that transforms a file to all uppercase, and another that creates an unchanged copy of the original file. The...

  • Write a program that opens a text file called input.txt and reads its contents into a...

    Write a program that opens a text file called input.txt and reads its contents into a stack of characters. The program should then pop the characters from the stack and save them in a second text file called output.txt. The order of the characters saved in the second file should be the reverse of their order in the first file. We don’t know the text file length. Input.txt This is a file is for test output.txt tset rof si elif...

  • This program has an array of floating point numbers as a private data member of a...

    This program has an array of floating point numbers as a private data member of a class. The data file contains floating point temperatures which are read by a member function of the class and stored in the array. Exercise 1: Why does the member function printList have a const after its name but getList does not? Exercise 2: Fill in the code so that the program reads in the data values from the temperature file and prints them to...

  • Code in C++: Please help me fix the error in function: void write_account(); //function to write...

    Code in C++: Please help me fix the error in function: void write_account(); //function to write record in binary file (NOTE: I able to store data to a txt file but however the data get error when I try to add on data the second time) void display_all(); //function to display all account details (NOTE: This function is to display all the info that save account.txt which is ID, Name, and Type) void modify_account(int); //function to modify record of file...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT