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.
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.
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
#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
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?
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
char tmp = strtok(fName, ".");
then use tmp as your fName in the print statment
Good luck
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Exactly what is your question?
David Maisonave :-)
Cheers!