Reading CSV File Using C Program

June 2, 2008

Since I'm on an integration project, I'm mostly dealing with transferring of data from two or more systems whether it's from the legacy system or newly implemented system. Most of the type of data movement is sending CSV files to and from the different system. Here is a simple tutorial on how to read CSV. The CSV file contains the following:

CODE:
  1. 1111,1414,1000
  2. 1112,1010, 1001
  3. 1113,1112,1002

The fields are Item Number, Class Number and Supplier Number.

The example below is the simple code of reading CSV file in C.

CODE:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(int argc, char* argv[])
  6. {
  7.    if (argc <2)
  8.    {
  9.       fprintf(stderr,"Usage: %s csv_file\n",argv[0]);
  10.       return(1);
  11.    }
  12.  
  13.     FILE *f = fopen(argv[1], "rt");
  14.     char Line[256];
  15.     unsigned int AllocSize = 0, Size = 0, n;
  16.     char *L_text;
  17.  
  18.     while(fgets(Line, sizeof(Line), f))
  19.     {
  20.  
  21.       printf("Item = %s \n",strtok(Line, ", "));
  22.       printf("Class = %s \n",strtok(NULL, ", "));
  23.       printf("Supplier = %s \n",strtok(NULL, ", "));
  24.  
  25.     }
  26.    return(0);
  27. }

How to display newline in Excel?

January 12, 2008

The commonly used to display newline in Visual Basic is using VbCrLF. But the problem is you will see a small rectangle at the end of the line.

Excel New Line

Visual Basic:
  1. Dim strFirstLine As String
  2. Dim strSecondLine As String
  3.  
  4. strFirstLine = "This is the first line"
  5. strSecondLine = "This is second line"
  6.  
  7. Worksheets("Sheet1").Range("b4").Value = _
  8. strFirstLine & vbCrLf & strSecondLine

To solve this problem, you will need to change the code from VbCrLF to VbLf.

Visual Basic:
  1. Dim strFirstLine As String
  2. Dim strSecondLine As String
  3.  
  4. strFirstLine = "This is the first line"
  5. strSecondLine = "This is second line"
  6.  
  7. Worksheets("Sheet1").Range("b4").Value = _
  8. strFirstLine & vbLf & strSecondLine

Excel New Line

Pro *C Programming Do’s and Don’t

November 18, 2007

  1. Use one array per action such as inserting, updated and deleting records.
  2. Always increment the array counter every time you add new records.
  3. Always resize array every time you add new records.
  4. Always reset the array counter after the bulk update, insert and delete.
  5. Inserting or updating records should comes last if the value of column in comes from a separate logic such as calculation, sequence retrieval.

Software Best Practices Conference

September 23, 2007

I want to share ideas from Youdon. His presentation for Software Best Practices Conference is about Ten Most Important Ideas in Software Engineering.  Here is what he talks about:

  1. You can't control what you can't measure
  2. Peopleware
  3. Incrementalism
  4. Iteration
  5. Repair costs increase
  6. Tradeoffs are non-linear
  7. Reuse is important
  8. Risk management provides insights
  9. Consistency trumps brillance + death march
  10. Don't reinvent the wheel

You can download the pdf here.