Link to home
Start Free TrialLog in
Avatar of jk2002
jk2002

asked on

Looking for a program that would enter a value to a CSV file

I am looking for a program (written in C++ or C#) that would take filename of filename.csv and add it to the beginning of each line in the file.

For example, my filename.csv looks like this:

Bob, Smith, 658954
Jane, Doe, 128756
Jim, Brown, 564864

I would like the output to look like this:

filename, Bob, Smith, 658954
filename, Jane, Doe, 128756
filename, Jim, Brown, 564864

I would prefer the syntax to look something like: ModCSV.exe filename.csv, which would make it very simple to use.

I am planning to use it on Win 2000 SP4 and Win XP SP1, SP2 machines. Thanks in advance.
Avatar of Axter
Axter
Flag of United States of America image

Hi jk2002,
Exactly what is your question?

David Maisonave :-)
Cheers!
Avatar of person1994
person1994

Here is a some code that would read your input file and insert the change into each line and show them on stdout.

#include <stdio.h>
#include <string.h>

#define MAX_LINE_LENGTH      1024

int handle_file(const char *fName)
{
      FILE *file = fopen(fName, "r");
      char buffer[MAX_LINE_LENGTH];

      if(!file)
            return -1;

      while ( fgets(buffer, MAX_LINE_LENGTH, file ) != NULL )
            fprintf(stdout, "%s,%s", fName, buffer);

      fclose(file);
      return 0;
}

int main(int argc, char *argv[])
{

      if(argc < 2)
            return -1;

      handle_file(argv[1]);

      return 0;
}


Good luck
Avatar of jk2002

ASKER

Thanks a lot person1994 for quick reply. I was able to compile it and run it with no problems. The output is fine except one litte thing: it inserts the entire filename including the extension. For example, the output looks like this:

filename.csv, Bob, Smith, 658954
filename.csv, Jane, Doe, 128756
filename.csv, Jim, Brown, 564864

What would have to be modified in the code to put just the filename in the CSV output file?
in handle_file you can use strtok to cut the fName before the '.' char.

char tmp = strtok(fName, ".");

then use tmp as your fName in the print statment

Good luck

Avatar of jk2002

ASKER

I think I must be placing the extra line in a wrong place as the compiler throws errors now. Same with the tmp in the print statement - I guess I am not as good with C as I thought I was. Can you provide me with direction where I should enter these to compile it succesfully? Thanks.
ASKER CERTIFIED SOLUTION
Avatar of person1994
person1994

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial