0

So I have to write a C program to read data from .csv files supplied to me by multiple users, into matrices on which I will perform some operations (like matrix addition, multiplication with necessary conditions on dimensions, etc.) and print these matrices (or the output data) in to .csv files again.

I also need to dynamically allocate memory to my matrices.

Now, I have zero background in dealing with .csv files. I do not at all know the required code to read a .csv file or write into a .csv file. I have searched for long on the Internet but surprisingly I have not found any program that teaches how to deal with .csv files from the elementary level.

I am lost on this and need a lot of guidance, maybe a sample, fully well-written C program as I need a comprehensive example to begin with.

  • CSV files are a pain to use - what do you do if you want a comma? They evolved to cope with these edge cases. I don't think there is a standard way to handle them. A lot of what you need to know is in the name: Comma Separated Values. You might be better off parsing these using a language with more advanced string handling methods than C provides (or C++). – Skizz Jan 16 '15 at 17:19
  • I can give an account of how many sleepless nights I passed using Google and found nothing like this. – Landon Carter Jan 16 '15 at 17:20
  • 2
    Then perhaps you just need more sleep. – M Oehm Jan 16 '15 at 17:20
  • No our professor specifically told us that he will supply us with csv files and we have to run programs on C. :( And M Oehm your comment got deleted somehow. As a result I lost the link. Could you kindly post it again? – Landon Carter Jan 16 '15 at 17:21
  • 1
    I deleted it because I found it a bit too snarky after seeing it on screen. The link is [the 4-point question from the sidebar of this page](http://stackoverflow.com/questions/12911299/read-csv-file-in-c?rq=1). – M Oehm Jan 16 '15 at 17:25
  • You may want to take a look at the [`strtok()`](http://linux.die.net/man/3/strtok) function. – user12205 Jan 16 '15 at 17:39

1 Answers1

2

A CSV file is just a plain ASCII text file that contains a grid of values. Think of the file as a set of rows in a database table where each line in the file represents one record and the order of the data in each line is identical. Each item of data is separated using a comma character (hence the name). So to read the file:-

open file
until the end of the file
  read line into a string
  split the string into sub strings where ',' is the dilimiter
  parse each sub string

Since there is no formatting information in a CSV file, if the data in each value consists of a string, then what do you do if the value has a comma in it? For reading numbers that is not a problem for you.

You could read the file in several passes, the first to determine the amount of data there is (number of columns, number of rows, etc) and the second to actually read the data.

Writing the CSV is quite simple:-

open file
for each record to write
  for each element to write
    write element
    if not last element
      write a comma
   write a new line
Skizz
  • 69,698
  • 10
  • 71
  • 108
  • There is the internet standard for CSV files, [RFC 4180](https://tools.ietf.org/html/rfc4180) "Common Format and MIME Type for Comma-Separated Values (CSV) Files", and then there's what Microsoft uses as a standard (mainly mild extensions to this, and handling some edge cases that the standard says are wrong). The standard is ex post facto. Then there are the variations with alternative delimiters, such as tabs, which are frequently called CSV files despite being an obvious misnomer (DSV — delimiter separated values — is more apposite, but less widely recognized). – Jonathan Leffler Jan 19 '15 at 20:14
  • The standards specify how to handle fields containing commas: enclose the field value in double quotes; any embedded double quotes in the value become two adjacent double quotes. If you have double quotes at the beginning of the field, then you also have to enclose the field in double quotes and double up the double quote (so `"Hi there", he said, "how are you?"` becomes `"""Hi there"", he said, ""how are you?"""` in a CSV file). – Jonathan Leffler Jan 19 '15 at 20:16
  • @JonathanLeffler: I think the point I was trying to make is that CSV files existed before there were standards to define them and that a lot of these edge cases (commas, quotes, etc) were hacked into the specification as common work around and that the complexity of writing a parser for these things depends on how much you want to support (for example, you could always treat commas as delimiters or just ignore the whole quote thing entirely). It's always worth seeing if there's a library available first and let someone else do the hard work. – Skizz Jan 20 '15 at 14:02