B
Brent Lievers
Greetings,
I have a question about parsing a fixed-width integer from a string.
Lines of data are being read from a file having a very strict
column-delimited format. In my example below, columns 0-7 are an integer
and columns 8-23 are a float. In _most_ files, the first few columns of
the float are blank space to make it human readable. But not always.
So, once they have been read from the file, I try to parse out the
different values using something like the following:
/*------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
char line1[]= " 12 15.796473824857";
char line2[]= " 1215.7964738248571";
int id;
float num;
sscanf(line1, "%8d%16E", &id, &num);
printf("ID= %d\n", id);
printf("NUM= %f\n\n", num);
sscanf(line2, "%8d%16E", &id, &num);
printf("ID= %d\n", id);
printf("NUM= %f\n", num);
return EXIT_SUCCESS;
}
/*------------------------------------------*/
which works in one case and not the other.
I could read the first 8 columns as characters and strtol() them to an
integer. Is there another/better way?
Thanks,
Brent
I have a question about parsing a fixed-width integer from a string.
Lines of data are being read from a file having a very strict
column-delimited format. In my example below, columns 0-7 are an integer
and columns 8-23 are a float. In _most_ files, the first few columns of
the float are blank space to make it human readable. But not always.
So, once they have been read from the file, I try to parse out the
different values using something like the following:
/*------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
char line1[]= " 12 15.796473824857";
char line2[]= " 1215.7964738248571";
int id;
float num;
sscanf(line1, "%8d%16E", &id, &num);
printf("ID= %d\n", id);
printf("NUM= %f\n\n", num);
sscanf(line2, "%8d%16E", &id, &num);
printf("ID= %d\n", id);
printf("NUM= %f\n", num);
return EXIT_SUCCESS;
}
/*------------------------------------------*/
which works in one case and not the other.
I could read the first 8 columns as characters and strtol() them to an
integer. Is there another/better way?
Thanks,
Brent