M
Martin Hvidberg
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.
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.a, &record.b,
&record.c, &record.d, &record.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.a, record.b, record.c,
record.d, record.e);
}
}
return 0;
}
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.
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.a, &record.b,
&record.c, &record.d, &record.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.a, record.b, record.c,
record.d, record.e);
}
}
return 0;
}