473,793 Members | 2,922 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.B ut could not succeed. If anyone has implemented the
above could give me a clue to it??

Bubunia
Nov 14 '05 #1
7 12617

"Sobhan" <bu*********@ya hoo.co.in> wrote in message
news:81******** *************** ***@posting.goo gle.com...
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.B ut 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*********@ya hoo.co.in> wrote in message
news:81******** *************** ***@posting.goo gle.com...
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.B ut 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*********@yah oo.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.B ut 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","MyPhon e"
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*********@yah oo.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.B ut 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*********@yah oo.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","MyPhon e"
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*********@yah oo.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.B ut 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
43018
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 mediate between webapps and arbitrary database backends using JDBC. I am very unwilling indeed to write special-case code for particular databases. Our code has worked satisfactorily with many databases, including many instances MS SQLServer 2000...
7
3166
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 dates in UK date format to input into the database. In Enterprise Manager on the SQL Server I can manually enter a record into a table and just type in a UK date (MM/DD/YYYY e.g. 25/12/2004) and it accepts it happily.
6
31186
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 format exception (SQLSTATE=22007). The same queries worked fine on all the other workstations. The date format we want to use is "dd/mm/yyyy".After reinstalling several times the db2 runtime client w/ different options, we found on a technical forum...
6
1993
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 standard for the file format, if existing tools would then be of use (but the format must not prevent my file from being portable)
48
2607
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. Does that carry over into the pointer, or does Unix use the flat memory model? -- Daniel Rudy
0
1490
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 the mm/dd/yyyy format. I created a deployment package and installed the program on another server and it also returned the correct mm/dd/yyyy format. Recently I got complaints saying that the format changed to mm-dd-yyyy format. This...
11
10577
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)) || '-' || RTRIM(CHAR(YEAR(COVG_TYP_STRT_DT))) FROM twd_coverage_type 1 ---------------------------------------------------------------------------------------------------------------------------- 6-May-2006 1 record(s) selected.
4
1530
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 output in the target language. I am thinking of using a hashed map to handle translations but am have trouble in deciding on the schema in which the XML must be stored The application must work as follows...
7
3074
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
4044
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 date format - we receive error: "Cannot convert date type varchar to datetime". Datetime used in application are sent to SQL Server 2000 in format YYYY-MM-DD as varchar. (in Query Analyzer all view/query works fine but when application sends it...
0
9671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10433
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10212
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10161
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9035
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6777
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.