Question

Write a C module called readCSV.c which has the following function signature: void read_csv(const char* csvFileName)....

Write a C module called readCSV.c which has the following function signature: void read_csv(const char* csvFileName). readCSV will be called by other modules.

The job of the function is to read the csv file and print all the contents of the file to the screen one line at a time. Make sure the code is in two files ( a .h file and a .c file) the readCSV.h file will have the declaration and the readCSV.c file will have the implementation of the function.

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

readCSV.h

#ifndef __readCSV_H_INCLUDED__ // if readCSV.h hasn't been included yet...
#define __readCSV_H_INCLUDED__ // #define this so the compiler knows it has been included

// included dependencies
#include <stdio.h>
#include <stdlib.h>


void read_csv(const char* csvFileName);

#endif

/*

The line number 1 and 2 (and last line #endif) is used to avoid conflict when the same header file is called more than once in a program.This technique is called ---Include Guard---

Since it is a small program it doesn't make much difference but it is a good practice.

This works by skipping over the entire header if it was already included. __readCSV_H_INCLUDED__ is #defined the first time readCSV.h is included -- and if readCSV.h is included a second time, the compiler will skip over the header because the #ifndef check will fail.

Always guard your headers. Always always always.

*/

------------------------------------------------------------------------

------------------------------------------------------------------------

readCSV.c

#include "readCSV.h"


void read_csv(const char* csvFileName){
   FILE *fptr;
   char str[60];

   fptr = fopen(csvFileName, "r");
   if (fptr == NULL){
   printf("Cannot open file \n");
   exit(0);
   }
  
   while ( fgets (str, 60, fptr)!=NULL ){
   /* writing content to stdout */
   puts(str);
   }
   fclose(fptr);

}

------------------------------------------------------------------------

------------------------------------------------------------------------

main.c

#include <stdio.h>
#include <stdlib.h>
#include "readCSV.h"

int main(){
  
   const char* fileName = "input.csv";
   read_csv(fileName);

   return 0;
}

------------------------------------------------------------------------

------------------------------------------------------------------------

input.csv

1 232 , 3123 , sam 6758 ??// / +++
2 232 , 3123 , sam 6758 ??// / +++
3 232 , 3123 , sam 6758 ??// / +++
4 232 , 3123 , sam 6758 ??// / +++
5 232 , 3123 , sam 6758 ??// / +++

------------------------------------------------------------------------

------------------------------------------------------------------------

*NOTE:

- You have to link the object file readCSV.c with the main file as seen in the screen shot.

- All the files are in same directory.

- My operating system is ubuntu 18 (linux).

Command Prompt:

$ gcc main.c readCSV.c -o main

$ ./main

------------------------------------------------------------------------

------------------------------------------------------------------------

Screnshot:

------------------------------------------------------------------------

------------------------------------------------------------------------

Add a comment
Know the answer?
Add Answer to:
Write a C module called readCSV.c which has the following function signature: void read_csv(const char* csvFileName)....
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
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