Overview:
The following program attempts to scan 5 float values per line from an input file using sscanf into an array of structs. If a value(s) is missing from a line during sscanf, the program should detect this missing value and assign it the value 1.500, but this substitution step isn't working.
The input file looks like this (note how the third value in the second row is missing):
0.123f, 0.234f, 0.345f, 0.456f, 0.567f
1.123f, 1.642f, , 1.456f, 1.567f
1.678f, 1.789f, 1.891f,1.911f, 2.001f
Expected output (note how the third value in the second row has been substituted with 1.500):
0.123, 0.234, 0.345, 0.456, 0.567
1.123, 1.642, 1.500, 1.456, 1.567
1.678, 1.789, 1.891, 1.911, 2.001
Actual output (not working as expected):
0.123 0.234 0.345 0.456 0.567
1.123 1.642 -431602080.000 -431602080.000 -431602080.000
1.678 1.789 1.891 1.911 2.001
Current attempt:
#include "stdio.h"
int main() {
typedef struct {
float x, y, vx, vy, mass;
}DATA;
FILE *file = fopen("null_detector.nbody", "r");
if (file == NULL)
{
printf(stderr, "ERROR: file not opened.\n");
return -1;
}
int Nbodies = 3;
DATA* data = malloc(Nbodies * sizeof * data); // Allocation for array of structs
char line[256];
int i;
for (i = 0; i < Nbodies; i += inc)
{
fgets(line, sizeof(line), file);
// Scan 5 float variables per line (this works fine)
sscanf(line, "%ff, %ff, %ff, %ff, %ff",
&data[i].x, &data[i].y, &data[i].vx, &data[i].vy, &data[i].mass);
// Check if any of the above scanned vars are NULL.
// Used individual IF statements instead of IF/ELSE to cover
// multiple NULL occurrences per line
float substitute = 1.500;
if (&data[i].x == '\0') { data[i].x = substitute; }
if (&data[i].y == '\0') { data[i].y = substitute; }
if (&data[i].vx == '\0') { data[i].vx = substitute; }
if (&data[i].vy == '\0') { data[i].vy = substitute; }
if (&data[i].mass == '\0') { data[i].mass = substitute; }
}
// Print the contents of array of structs to check for correct output
for (i = 0; i < Nbodies; i++)
{
printf("%.3f %.3f %.3f %.3f %.3f\n", data[i].x, data[i].y, data[i].vx, data[i].vy, data[i].mass);
}
return 0;
}
Summary:
Can anyone see why this doesn't work? I thought that the individual IF statements after sscanf would successfully substitute the missing value for 1.500. It seems that this method can atleast detect when a value is missing, but cannot replace the missing value with 1.500.