[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

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.
0
jk2002
Asked:
jk2002
  • 3
  • 2
1 Solution
 
AxterCommented:
Hi jk2002,
Exactly what is your question?

David Maisonave :-)
Cheers!
0
 
person1994Commented:
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
0
 
jk2002Author Commented:
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?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
person1994Commented:
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

0
 
jk2002Author Commented:
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.
0
 
person1994Commented:
try this:


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

     if(!file)
          return -1;

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

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

     fclose(file);
     return 0;
}

Good luck
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now