473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Q: Newbee. Read and convert CSV file...

Dear group

I need to make a very simple piece of code in C, that can be command line
executed and will compile on Linux, i.e. gcc.

It should read a ascii Comma Separated Values (CSV) file and write the
data (numbers) to another ascii file in a specific fixed width format
(fwf). Let me show an example.
Infile.csv

123.4,234.5,345,45678
234.5,345.6,456,56789
345.6,456.7,567,67890
....

<<< outfile.fwf
000123.4000234.5000345000045678
000234.5000345.6000456000056789
000345.6000456.7000567000067890

The input format is plain CSV with (,) as column separator and (.) As
decimal separator.

The output is a “fixed width format” who's parameters are given on the
command line. In this case the call look like this: csv2fwf , 8 8 6 9
Indicating that the program “csv2fwf” should assume (,) as a the separator
in the CSV file and output to columns of width 8,8,6 and 9 characters
respectively.

I assume the solution can include something like the code below, that I
got off the web. But I need help to finish it off

code:
#include <stdio.h>

struct record
{
int a, b;
double c, d, e;
};

int main(int argc, char *argv[])
{
FILE *pInFile;

if (argc!=2)
{
printf("\n Usage: %s <Infile>\n",argv[0]);
return 1;
}
else
{
if ((pInFile = fopen(argv[1],"r"))==NULL)
{
printf("\n\n ERROR - Can't open InFile: %s\n\n",argv[1]);
return 2;
}
else
printf("> InFile %s open ...\n",argv[1]);
}

{
char line [80];
struct record record [750];
size_t count, i = 0;
while ( i < sizeof record / sizeof *record )
{
if ( fgets(line, sizeof line, pInFile) == NULL )
{
break;
}
if ( sscanf(line, "%d,%d,%lf,%lf,%lf", &record[i].a, &record[i].b,
&record[i].c, &record[i].d, &record[i].e) == 5 )
{
++i;
}
}
fclose(pInFile);
for ( count = i, i = 0; i < count; ++i )
{
printf("record[%lu]: a = %d, b = %d, c = %g, d = %g, e = %g\n",
(long unsigned)i, record[i].a, record[i].b, record[i].c,
record[i].d, record[i].e);
}
}

return 0;
}
Nov 14 '05 #1
4 5797
On Wed, 23 Feb 2005 15:06:09 +0100, in comp.lang.c , "Martin Hvidberg"
<mh*@dmu.dk> wrote:
Dear group

I need to make a very simple piece of code in C, that can be command line
executed and will compile on Linux, i.e. gcc.

It should read a ascii Comma Separated Values (CSV) file and write the
data (numbers) to another ascii file in a specific fixed width format
(fwf). Let me show an example.
Infile.csv

123.4,234.5,345,45678

<<< outfile.fwf
000123.4000234.5000345000045678


read in the line with fgets, tokenize it with either strtok if you are
happy with it, or your own tokenizer if not, then use printf with a format
string of %06.03f with each token. .

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #2


Mark McIntyre wrote:
On Wed, 23 Feb 2005 15:06:09 +0100, in comp.lang.c , "Martin Hvidberg"
<mh*@dmu.dk> wrote:

Dear group

I need to make a very simple piece of code in C, that can be command line
executed and will compile on Linux, i.e. gcc.

It should read a ascii Comma Separated Values (CSV) file and write the
data (numbers) to another ascii file in a specific fixed width format
(fwf). Let me show an example.

>Infile.csv


123.4,234.5,345,45678

<<< outfile.fwf
000123.4000234.5000345000045678

read in the line with fgets, tokenize it with either strtok if you are
happy with it, or your own tokenizer if not, then use printf with a format
string of %06.03f with each token. .


In addition:
- determine what you would like to happen if the value is <0; consider
the output of other flags for *printf (instead only 0, e.g. %+6.3f,
%0+6.3)

Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #3
Mark McIntyre <ma**********@spamcop.net> wrote:
On Wed, 23 Feb 2005 15:06:09 +0100, in comp.lang.c , "Martin Hvidberg"
<mh*@dmu.dk> wrote:
I need to make a very simple piece of code in C, that can be command line
executed and will compile on Linux, i.e. gcc.
(This i.e. is not necessarily guaranteed, btw, and if you keep to ISO C
rather than specialise in Ganuck, your code should compile on Linux as
well as anywhere else, using gcc, Turbo C, or whatever ISO C compiler
you can find. And for this job, ISO C is quite adequate.)
It should read a ascii Comma Separated Values (CSV) file and write the
data (numbers) to another ascii file in a specific fixed width format
(fwf). Let me show an example.
> Infile.csv123.4,234.5,345,45678

<<< outfile.fwf
000123.4000234.5000345000045678


read in the line with fgets, tokenize it with either strtok if you are
happy with it, or your own tokenizer if not,


He's already got that, though he uses sscanf(). The format string is
wrong, though. Martin: what do you think your sscanf() call reads from
the fgets()-read line?
then use printf with a format string of %06.03f with each token. .


No, not with a fixed format string. He needs to read the widths (but
not, oddly enough, the file names) from the command line.
Martin: how familiar are you with argc and argv? You need to construct a
format string as Mark has written above, but with the actual numbers
read from the members of argv. Use strtol() instead of atoi(), it's
safer.
Devise a strategy for wrong input. Consider where you read from, and
where you write to: you may not need to store all your data in an array,
as your example shows; reading a line and sending it out immediately in
the desired format may be sufficient.

Richard
Nov 14 '05 #4
Thanks to all - I now have a number of good things to work with.

Sorry that the sscanf() format string didn't match my csv example, they
were taken from seperate sources. But it didn't fool you. I'll be more
carefull in the feature...

:-) Martin

I'll be back...
Nov 14 '05 #5

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

Similar topics

3
by: hunterb | last post by:
I have a file which has no BOM and contains mostly single byte chars. There are numerous double byte chars (Japanese) which appear throughout. I need to take the resulting Unicode and store it in a...
4
by: PerryC | last post by:
All, 1. Do the following codes seem ok? 2. If so, then how do I pull the value of YOE1 and YOE2 into my report? (to do some further calculations) ...
11
by: Martin Hvidberg | last post by:
Please refere to the source below: The program is supposed to read s ascii text file and count the number of occurences of each of the 256 ascii codes. The result is presented on screen. It...
1
by: vkrasner | last post by:
It works with VS2003 and does not in VS2005: in VS2003 : string sMyvalue = ConfigurationSettings.AppSettings; in VS2005 (does not work!!) string sMyvalue = ConfigurationManager.AppSettings; ...
5
by: JenHu | last post by:
Hi experts, I wrote a function which retrieves a file in the folder, the file path is : Dim sr As New StreamReader(strFilepath & ReturnFileName) What if I have more than 1 file_name in...
4
by: John please don't spam me! | last post by:
Hi Guys, I writing a project with just one module in it (the reason for this is to debug code before it becomes a service) and am getting an error which I do not understand: No accessible...
2
by: Rene | last post by:
Hello to all! I am a newbee to C++, I did a beginners' course, and now I am stuck with a strange problem. I have written the function that is pasted downwards. The peculiar thing is that when...
2
by: Plumebee | last post by:
Hi, I am very new to programming and have just started to use Visual Basic 2005 Express Edition. I am trying to read from a text file to draw a rectangles and lines. However to begin I'm trying to...
2
by: necron99 | last post by:
import RuntimeEnvironment as renv import os, sys import win32com.client import getpass OMI = win32com.client.Dispatch("MOVEitAPI.clientObj") OMI.Host = system
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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
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
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 projectplanning, coding, testing,...
0
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...

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.