Decomposing mixed text/floating point string

Hi all,

I’m trying to use sscanf to decompose a string which contains a name and floating point numbers. To isolate the problem, there is a little test program
listed below. Here there are two strings, line and line1. They both start with the same keyword ‘FIVE_HIGHS’. The rest of the line is an output filename
to be used later, e.g. ‘image0.asc’ and 10 parameters for the calculation.

Since there may be many such lines with an unknown number, I’d like to use a dynamic array of strings to store them, i.e. char** outfile. However, with the code below, things just don’t work as expected. If I try to use strlen(s1), the program crashes.

In general, the length of the filename is an unknown, i.e. it maybe image123.xyz, so cannot assume a known fixed length for this string.

How can this be made to work? In the real program, these lines would be read from a file.

Appreciate any help on this!

Many thanks and best wishes!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
// Delimiter in line and line may be a tab or a space.
char line = “FIVE_HIGHS image0.asc 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 0.0 0.01”;
char line1 = “FIVE_HIGHS image123.xyz 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 0.0 0.01”;
char** outfile = NULL;
int filecount = 0;

char *s1;
float c1, c2, c3, c4, c5, c6, c7, c8, c9, c10;
sscanf(line, “FIVE_HIGHS %[a-zA-Z0-9.] %f %f %f %f %f %f %f %f %f %f”, &s1, &c1, &c2, &c3, &c4, &c5, &c6, &c7, &c8, &c9, &c10);
// s1 reads ‘imag’ instead of ‘image0.asc’
printf(“s1 %s\n”, &s1);
printf(“c1 c2 c3 %g %g %g\n”, c1, c2, c3);
printf(“c4 c5 c6 %g %g %g\n”, c4, c5, c6);
printf(“c7 c8 c9 %g %g %g\n”, c7, c8, c9);
printf(“c10 %g\n”, c10);
outfile = (char **)realloc(outfile, (filecount + 1) * sizeof(char *));
outfile[filecount] = s1;
filecount++;
sscanf(line1, “FIVE_HIGHS %s %f %f %f %f %f %f %f %f %f %f”, &s1, &c1, &c2, &c3, &c4, &c5, &c6, &c7, &c8, &c9, &c10);
printf(“s1 %s\n”, &s1);
printf(“c1 c2 c3 %g %g %g\n”, c1, c2, c3);
printf(“c4 c5 c6 %g %g %g\n”, c4, c5, c6);
printf(“c7 c8 c9 %g %g %g\n”, c7, c8, c9);
printf(“c10 %g\n”, c10);
outfile = (char **)realloc(outfile, (filecount + 1) * sizeof(char *));
outfile[filecount] = s1;
filecount++;
// Program probably crashes on statement below - why?
for (int i=0; i<filecount; i++)
printf(“outfile %s\n”, outfile[i]);

}

Hi all,

I’m trying to use sscanf to decompose a string which contains a name and floating point numbers. To isolate the problem, there is a little test program
listed below. Here there are two strings, line and line1. They both start with the same keyword ‘FIVE_HIGHS’. The rest of the line is an output filename
to be used later, e.g. ‘image0.asc’ and 10 parameters for the calculation.

Since there may be many such lines with an unknown number, I’d like to use a dynamic array of strings to store them, i.e. char** outfile. However, with the code below, things just don’t work as expected. If I try to use strlen(s1), the program crashes.

In general, the length of the filename is an unknown, i.e. it maybe image123.xyz, so cannot assume a known fixed length for this string.

How can this be made to work? In the real program, these lines would be read from a file.

Appreciate any help on this!

Many thanks and best wishes!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
// Delimiter in line and line may be a tab or a space.
char line = “FIVE_HIGHS image0.asc 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 0.0 0.01”;
char line1 = “FIVE_HIGHS image123.xyz 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 0.0 0.01”;
char** outfile = NULL;
int filecount = 0;

char *s1;
float c1, c2, c3, c4, c5, c6, c7, c8, c9, c10;
sscanf(line, “FIVE_HIGHS %[a-zA-Z0-9.] %f %f %f %f %f %f %f %f %f %f”, &s1, &c1, &c2, &c3, &c4, &c5, &c6, &c7, &c8, &c9, &c10);
// s1 reads ‘imag’ instead of ‘image0.asc’
printf(“s1 %s\n”, &s1);
printf(“c1 c2 c3 %g %g %g\n”, c1, c2, c3);
printf(“c4 c5 c6 %g %g %g\n”, c4, c5, c6);
printf(“c7 c8 c9 %g %g %g\n”, c7, c8, c9);
printf(“c10 %g\n”, c10);
outfile = (char **)realloc(outfile, (filecount + 1) * sizeof(char *));
outfile[filecount] = s1;
filecount++;
sscanf(line1, “FIVE_HIGHS %s %f %f %f %f %f %f %f %f %f %f”, &s1, &c1, &c2, &c3, &c4, &c5, &c6, &c7, &c8, &c9, &c10);
printf(“s1 %s\n”, &s1);
printf(“c1 c2 c3 %g %g %g\n”, c1, c2, c3);
printf(“c4 c5 c6 %g %g %g\n”, c4, c5, c6);
printf(“c7 c8 c9 %g %g %g\n”, c7, c8, c9);
printf(“c10 %g\n”, c10);
outfile = (char **)realloc(outfile, (filecount + 1) * sizeof(char *));
outfile[filecount] = s1;
filecount++;
// Program probably crashes on statement below - why?
for (int i=0; i<filecount; i++)
printf(“outfile %s\n”, outfile[i]);

}

This is a CUDA forum, not a general C forum. However, you are missing to allocate memory for *s1, and should pass s1, not &s1. I am surprised your compiler does not emit a warning for the latter.
While that would be nice, scanf does not allocate memory for strings for you, you have to pass in previously allocated memory…

This is a CUDA forum, not a general C forum. However, you are missing to allocate memory for *s1, and should pass s1, not &s1. I am surprised your compiler does not emit a warning for the latter.
While that would be nice, scanf does not allocate memory for strings for you, you have to pass in previously allocated memory…