473,503 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CSV Format in C language

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
Nov 14 '05 #1
7 12585

"Sobhan" <bu*********@yahoo.co.in> wrote in message
news:81**************************@posting.google.c om...
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
Nov 14 '05 #2

"Sobhan" <bu*********@yahoo.co.in> wrote in message
news:81**************************@posting.google.c om...
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
Nov 14 '05 #3
>> 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
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
Nov 14 '05 #4
bu*********@yahoo.co.in 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 ) ;

Nov 14 '05 #5
On Thu, 22 Jan 2004, Tom St Denis wrote:
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..

Nov 14 '05 #6
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

Richard <rh***@hotmail.com> wrote in message news:<MP************************@news.verizon.net> ...
bu*********@yahoo.co.in 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 ) ;

Nov 14 '05 #7
bu*********@yahoo.co.in 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.
Regards
Bubunia

Richard <rh***@hotmail.com> wrote in message news:<MP************************@news.verizon.net> ...
bu*********@yahoo.co.in 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 ) ;


--
http://www.howtobuyamerican.com/
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
42977
by: Simon Brooke | last post by:
I'm investigating a bug a customer has reported in our database abstraction layer, and it's making me very unhappy. Brief summary: I have a database abstraction layer which is intended to...
7
3142
by: Niall Porter | last post by:
Hi all, I'm building an ASP app on a Windows 2000/IIS machine which interfaces with our SQL Server 2000 database via OLE DB. Since we're based in the UK I want the users to be able to type in...
6
31128
by: Dario Di Bella | last post by:
Hi all, we have the following urgent issue affecting our development team. Initially we had one particular workstation that failed executing queries on a DB2 database, raising an invalid date...
6
1972
by: Mark R.Bannister | last post by:
Hi, I'm currently designing a new language that compiles into a bytecode format. I have a choice: a) Design my own binary file format for the bytecode, symbols and data b) Use an existing...
48
2547
by: Daniel Rudy | last post by:
Hello, On a x86 machine, what is the format of a pointer in C? I know for a fact that the x86 p-mode uses a /selector:offset/ notation where the selector is defined in either the GDT or LDT. ...
0
1470
by: GFro | last post by:
I have a calendar page that returns a date to a textbox on the parent page. It is returning the wrong format on the deployment server. On the development server the calendar returns to textbox in...
11
10540
by: shsandeep | last post by:
I used the following query to retrieve the date in dd-mon-yyyy format. db2 => SELECT RTRIM(CHAR(DAY(COVG_TYP_STRT_DT))) || '-' || RTRIM(MONTHNAME(COVG_TYP_STRT_DT)) || '-' ||...
4
1509
by: AViS | last post by:
Hi, I am building a language translator, that must convert input from source languages to a language neutral format in XML. This XML must be read by the target language translator and produce the...
7
3050
by: Rick | last post by:
With String.Format, if I have an incorrect number of args specified for a format string, compile fails. How can I implement similar design-time functionality for my own string functions?
1
4029
by: Maciej07 | last post by:
Hello, We are using SQL server 2000 on W2k Server and MS Access 2000 ADP (like front-end). Now we try to change operating system for SQL Server 2000 from W2k to W2k3 and we found problem with...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.