Здесь можно указать два раздела в любом порядке, или можно указать только один, или даже ни одного:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Element {
double length;
double velocity;
} Element;
void * read_section(FILE * fp, const char * what, int * n, size_t sz)
{
if (*n != 0) {
fprintf(stderr, "invalid file, several sections [%s]\n", what);
exit(-1);
}
char fmt[64];
sprintf(fmt, " Number of %s = %%d", what); /* notice space at beginning */
if (fscanf(fp, fmt, n) != 1) {
fprintf(stderr, "invalid file, expected 'Number of %s = <n>'\n", what);
exit(-1);
}
if (*n <= 0) {
fprintf(stderr, "number of %s must be > 0\n", what);
exit(-1);
}
void * r = malloc(*n * sz);
if (r == NULL) {
fprintf(stderr, "not enough memory for %d %s\n", *n, what);
exit(-1);
}
return r;
}
void read_var(FILE * fp, const char * var, const char * unity, int rank, double * v)
{
char fmt[64];
sprintf(fmt, " %s_%d = %%lg%s", var, rank, unity); /* notice space at beginning */
if (fscanf(fp, fmt, v) != 1) {
fprintf(stderr, "invalid file, expected '%s_%d = <val>%s'\n", var, rank, unity);
exit(-1);
}
}
int main(int argc, char ** argv)
{
if (argc != 2) {
fprintf(stderr, "Usage: %s <file>\n", *argv);
exit(-1);
}
FILE * fp = fopen(argv[1], "r");
if (fp == NULL) {
perror("cannot open file");
exit(-1);
}
int nelts = 0;
Element * elts = NULL;
int ncells = 0;
double * cells = NULL;
char line[32];
int i;
while (fscanf(fp, " %31s", line) == 1) { /* notice space at beginning of format */
if (!strcmp(line, "[elements]")) {
elts = read_section(fp, "elements", &nelts, sizeof(*elts));
for (i = 0; i != nelts; ++i)
read_var(fp, "length", "m", i+1, &elts[i].length);
for (i = 0; i != nelts; ++i)
read_var(fp, "velocity", "m/s", i+1, &elts[i].velocity);
}
else if (!strcmp(line, "[cells]")) {
cells = read_section(fp, "cells", &ncells, sizeof(*cells));
for (i = 0; i != ncells; ++i)
read_var(fp, "cell", "m", i+1, &cells[i]);
}
else {
fputs("invalid file, section header expected\n", stderr);
exit(-1);
}
}
fclose(fp);
/* to check */
printf("%d elements:\n", nelts);
for (i = 0; i != nelts; ++i)
printf("%d) length=%g, velocity=%g\n", i, elts[i].length, elts[i].velocity);
printf("\n%d cells:", ncells);
for (i = 0; i != ncells; ++i)
printf(" %g", cells[i]);
putchar('\n');
free(elts);
free(cells);
return 0;
}
Обратите внимание на пробел в начале форматов, когда он необходим для обхода символов новой строки (и возможных других пробелов).
Компиляция и выполнение с вашим примером:
pi@raspberrypi:/tmp $ gcc -g -Wall x.c
pi@raspberrypi:/tmp $
pi@raspberrypi:/tmp $ cat f1
[elements]
Number of elements = 2
length_1 = 1.5m
length_2 = 2.1m
velocity_1 = 0.35m/s
velocity_2 = 0.11m/s
[cells]
Number of cells = 2
cell_1 = 0.0m
cell_2 = 1.3m
pi@raspberrypi:/tmp $ ./a.out f1
2 elements:
0) length=1.5, velocity=0.35
1) length=2.1, velocity=0.11
2 cells: 0 1.3
pi@raspberrypi:/tmp $
порядок обмена разделами
pi@raspberrypi:/tmp $ cat f2
[cells]
Number of cells = 2
cell_1 = 0.0m
cell_2 = 1.3m
[elements]
Number of elements = 2
length_1 = 1.5m
length_2 = 2.1m
velocity_1 = 0.35m/s
velocity_2 = 0.11m/s
pi@raspberrypi:/tmp $ ./a.out f2
2 elements:
0) length=1.5, velocity=0.35
1) length=2.1, velocity=0.11
2 cells: 0 1.3
pi@raspberrypi:/tmp $
только элементы
pi@raspberrypi:/tmp $ cat f3
[elements]
Number of elements = 2
length_1 = 1.5m
length_2 = 2.1m
velocity_1 = 0.35m/s
velocity_2 = 0.11m/s
pi@raspberrypi:/tmp $ ./a.out f3
2 elements:
0) length=1.5, velocity=0.35
1) length=2.1, velocity=0.11
0 cells:
pi@raspberrypi:/tmp $
только ячейки
pi@raspberrypi:/tmp $ cat f4
[cells]
Number of cells = 2
cell_1 = 0.0m
cell_2 = 1.3m
pi@raspberrypi:/tmp $ ./a.out f4
0 elements:
2 cells: 0 1.3
pi@raspberrypi:/tmp $
пустой входной файл
pi@raspberrypi:/tmp $ ./a.out /dev/null
0 elements:
0 cells:
pi@raspberrypi:/tmp $
другие элементы и ячейки
pi@raspberrypi:/tmp $ cat f
[elements]
Number of elements = 3
length_1 = 1.5m
length_2 = 2.1m
length_3 = 1.1m
velocity_1 = 0.35m/s
velocity_2 = 0.11m/s
velocity_3 = 0.33m/s
[cells]
Number of cells = 5
cell_1 = 0.0m
cell_2 = 1.3m
cell_3 = 2.3m
cell_4 = 3.3m
cell_5 = 4.3m
pi@raspberrypi:/tmp $ ./a.out f
3 elements:
0) length=1.5, velocity=0.35
1) length=2.1, velocity=0.11
2) length=1.1, velocity=0.33
5 cells: 0 1.3 2.3 3.3 4.3
pi@raspberrypi:/tmp $
и проверяются возможные случаи ошибок , Я призываю вас никогда не предполагать, что все всегда в порядке, но проверить, что это
Завершить sh, если вы находитесь под Linux / Unix, проверьте, что ваша программа работает под valgrind , для пример:
pi@raspberrypi:/tmp $ valgrind ./a.out f
==13981== Memcheck, a memory error detector
==13981== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==13981== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==13981== Command: ./a.out f
==13981==
3 elements:
0) length=1.5, velocity=0.35
1) length=2.1, velocity=0.11
2) length=1.1, velocity=0.33
5 cells: 0 1.3 2.3 3.3 4.3
==13981==
==13981== HEAP SUMMARY:
==13981== in use at exit: 0 bytes in 0 blocks
==13981== total heap usage: 5 allocs, 5 frees, 5,560 bytes allocated
==13981==
==13981== All heap blocks were freed -- no leaks are possible
==13981==
==13981== For lists of detected and suppressed errors, rerun with: -s
==13981== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
pi@raspberrypi:/tmp $