Hi all,

I am very new to C code and was wondering if someone could please help me.

i am using a program that gets run through a windows command prompt and outputs the result to the command line screen by default. i was trying to get it to output the results to a .csv file injstead but i have no idea how to do this. this is how it outputs it to the command line:

printf( "%d%s%s%s%d%s%s%s%d\n", index, delim, deltime, delim, type, delim, filename, delim, filesize );

can some one please help me?

thanks in advance

scoobie,

i am using a program that gets run through a windows command prompt and outputs the result to the command line screen by default. i was trying to get it to output the results to a .csv file injstead but i have no idea how to do this.

You have to change the program to make the output come out in CSV format. Ancient Dragon's advice will get the data into a file for you, but it won't be in CSV format. It'll still look like the screen.

> printf( "%d%s%s%s%d%s%s%s%d\n", index, delim, deltime, delim, type, delim, filename, delim, filesize );

Something like

FILE *fp = fopen( "file.csv", "w" );
fprintf( fp, "%d%s%s%s%d%s%s%s%d\n", index, delim, deltime, delim, 
         type, delim, filename, delim, filesize );
fclose( fp );

csv format uses commas between fields, and I believe that text fields which contain commas should be enclosed in quotes.

FILE *fp = fopen( "file.csv", "w" );
fprintf( fp, "%d,\"%s\",\"%s\",\"%s\",%d,\"%s\",\"%s\",\"%s\",%d\n", index, delim, deltime, delim, 
         type, delim, filename, delim, filesize );
fclose( fp );
Comments
Good - [Grunt]

> and I believe that text fields which contain commas should be enclosed in quotes.
Yeah, I wasn't going there - too tricky ;)
If the strings contain quotes as well as commas, its going to get ugly.

This article has been dead for over six months. Start a new discussion instead.