CSV Format in C language

S

Sobhan

Hi all,
Iam writing a small C program to print a array of numbers in
Comma Separated Values in VC++ editor.

Lets say:
Title Title1 Title2
----- ------ ------
10,20,30
50,90,100
120,180,300

I was trying to print the above values in a .txt file and the values
should be printed as above. I played a lot with different
lib.functions.But could not succeed. If anyone has implemented the
above could give me a clue to it??

Bubunia
 
T

Tom St Denis

Sobhan said:
Hi all,
Iam writing a small C program to print a array of numbers in
Comma Separated Values in VC++ editor.

Lets say:
Title Title1 Title2
----- ------ ------
10,20,30
50,90,100
120,180,300

I was trying to print the above values in a .txt file and the values
should be printed as above. I played a lot with different
lib.functions.But could not succeed. If anyone has implemented the
above could give me a clue to it??

It's fairly trivial to read in with the use of

while (*c && isdigit(*c)) ++c;
the fgets command
the sscanf command

Good luck!

Tom
 
A

Allan Bruce

Sobhan said:
Hi all,
Iam writing a small C program to print a array of numbers in
Comma Separated Values in VC++ editor.

Lets say:
Title Title1 Title2
----- ------ ------
10,20,30
50,90,100
120,180,300

I was trying to print the above values in a .txt file and the values
should be printed as above. I played a lot with different
lib.functions.But could not succeed. If anyone has implemented the
above could give me a clue to it??

Bubunia

This is trivial for writing a csv file, use
fprintf("%d,%d,%d", num1, num2, num3);
if the number of variables are known.
HTH
Allan
 
J

Jan Engelhardt

Hi all,
This is trivial for writing a csv file, use
fprintf("%d,%d,%d", num1, num2, num3);

printf. fprintf still takes a FILE * arg. :)
if the number of variables are known.

For strings,
char *ar[]; // suppose your data is here

char **arp = ar;
while(*arp != NULL) {
int q = 0;
if(strchr(*arp, ',') != NULL) { ++q; printf("\""); }
printf("%s", *arp);
if(q) { printf("\""); }
++arp;
}



Jan Engelhardt
 
R

Richard

(e-mail address removed) wrote...
Hi all,
Iam writing a small C program to print a array of numbers in
Comma Separated Values in VC++ editor.

Lets say:
Title Title1 Title2
----- ------ ------
10,20,30
50,90,100
120,180,300

I was trying to print the above values in a .txt file and the values
should be printed as above. I played a lot with different
lib.functions.But could not succeed. If anyone has implemented the
above could give me a clue to it??

In CSV format, your column headers are comma-delimited as well, all
strings (non-numbers) are in quotes, there are a fixed number of
columns in each row, empty fields are OK, and each row ends with a
newline character.

Simplest way to get this into a text file is simple to have the
program write its non-error output to stdout using printf(), and then
capture that output via redirection at the command line.

So you'd invoke it as

myprog > mycsv.txt

Your two title rows are:

#define TITLEROWFMT "\"%s\",\"%s\",\"%s\"\n"

printf( TITLEROWFMT, "Title", "Title1", "Title2" ) ;
printf( TITLEROWFMT, "-----", "------", "------" ) ;

You don't mention where you're getting your input, so I'll presume
you're generating it from within your program (rather than, say,
reading it from some other file). You also don't describe the range
of values in your output, so I'll presume an int will get the job
done.

Your data rows are:

#define DATAROWFMT "%d,%d,%d\n"

printf( DATAROWFMT, 10, 20, 30 ) ;
printf( DATAROWFMT, 50, 90, 100 ) ;
printf( DATAROWFMT, 120, 180, 300 ) ;

Presumptions and limitations:

1) I presume your question is about how to get the output into CSV
format, not how to use variables (rather than numberic constants as
in my data row example) or how to open and write to an output file.

2) Null fields are not handled by DATAROWFMT. For rows with any null
fields, you would use a different output format specifier. Here's an
example, with the first field empty:

printf( ",%d,%d\n", 50, 100 ) ;
 
J

Jarno A Wuolijoki

It's fairly trivial to read in with the use of

while (*c && isdigit(*c)) ++c;

Provided that *c is representable as an unsigned char or equal to EOF..
 
S

Sobhan

Thanks for the information.My program generates some values in a .txt
file that is not in a CSV format,it is line by line below.I need to
control from my program that the values will be represented in CSV
format(not line by line).

For exp:

my program generates output in a file like:
Myage=40
MyPhone=167262
Etc etc.. through printf and fprintf statements.

I want output like:
"MyAge","MyPhone"
50 ,8056453
100 ,256353
67 ,1423232
I don't want to read the file once again to convert the line by line
statements
to convert CSV format.I want to manage it once only..

Regards
Bubunia
 
R

Richard

(e-mail address removed) wrote...
Thanks for the information.My program generates some values in a .txt
file that is not in a CSV format,it is line by line below.I need to
control from my program that the values will be represented in CSV
format(not line by line).

For exp:

my program generates output in a file like:
Myage=40
MyPhone=167262
Etc etc.. through printf and fprintf statements.

I want output like:
"MyAge","MyPhone"
50 ,8056453
100 ,256353
67 ,1423232
I don't want to read the file once again to convert the line by line
statements
to convert CSV format.I want to manage it once only..

It sounds like you've been given enough information to put your
program together, then. I don't know (nor do I want to know) what
your input is, but while the problem isn't hard there's no good
reason for someone else to write the program for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,137
Messages
2,570,797
Members
47,345
Latest member
tektheone

Latest Threads

Top