How to read data columnwise

S

Sobhan

Hi all,
I am writing a program in C in which I need to read a data
file and export to the excel.The Data in the file in CSV format.
Values in the .txt file are as follows:
"a","b","c"
10,20,30
40,50,60
70,80,90

For that I need to read the values column wise.
Like I have to store the values in a buffer like the following:
"a",
10,
40,
70
etc.

I did the following:

1.Open the file in Read Mode
2.Read line by line
3.Search for comma
4.When Got comma
Printf("\n");
5.Problem is how I will return the buffer(Which is set of integers) to
calling program reading the data values column wise???

Regards
Bubunia
 
P

pete

Sobhan said:
Hi all,
I am writing a program in C in which I need to read a data
file and export to the excel.The Data in the file in CSV format.
Values in the .txt file are as follows:
"a","b","c"
10,20,30
40,50,60
70,80,90

For that I need to read the values column wise.
Like I have to store the values in a buffer like the following:
"a",
10,
40,
70
etc.

I did the following:

1.Open the file in Read Mode
2.Read line by line
3.Search for comma
4.When Got comma
Printf("\n");
5.Problem is how I will return the buffer(Which is set of integers)

'a' is an integer, "a" is a pointer.
Is it supposed to be 'a' or "a" ?
 
P

Peter Pichler

Sobhan said:
Hi all,
I am writing a program in C in which I need to read a data
file and export to the excel.The Data in the file in CSV format.
Values in the .txt file are as follows:
"a","b","c"
10,20,30
40,50,60
70,80,90

For that I need to read the values column wise.

You can't. You can only read it a character at a time or a line at a time.
(Yes, yes, I know, there are other methods, but none of them "column wise".)
I did the following:

1.Open the file in Read Mode
2.Read line by line
3.Search for comma
4.When Got comma
Printf("\n");

ITYM printf (lowercase p). This approach will work only for printing the
first column. It will not work if a) you want to make any sense of the items
in your first column, and/or if b) you want to read second (make it Nth)
column.
5.Problem is how I will return the buffer(Which is set of integers) to
calling program reading the data values column wise???

Read a line at a time (fgets), parse it (sscanf, strtol or whatever is
suitable for your data format) and discard the rest of the line. Repeat for
every line. Simple.

Peter
 
J

Joe Wright

Sobhan said:
Hi all,
I am writing a program in C in which I need to read a data
file and export to the excel.The Data in the file in CSV format.
Values in the .txt file are as follows:
"a","b","c"
10,20,30
40,50,60
70,80,90

For that I need to read the values column wise.
Like I have to store the values in a buffer like the following:
"a",
10,
40,
70
etc.

I did the following:

1.Open the file in Read Mode
2.Read line by line
3.Search for comma
4.When Got comma
Printf("\n");
5.Problem is how I will return the buffer(Which is set of integers) to
calling program reading the data values column wise???
You are lost. This is not a C question. You don't know what CSV means as
opposed to (and including) 'comma delimited' applied to file formats.
And 'comp.lang.c' is the wrong group for this. Maybe 'comp.programming'?
If you ask there, I and several others will offer our best advice.
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top