使用fgets()
,sscanf()
和额外的代码来解析。
如果字段中不允许使用分隔符(空格),则此解析将更加健壮。内部的潜在空间或空间.name
使解析复杂化。推荐逗号分隔的字段。此外,该int n2[10]
领域是不寻常的。为什么不unsigned long long n2
呢?
一定要检查fgets()
,的结果sscanf()
。
#include <stdio.h>
#include <string.h>
typedef struct ex {
int n1;
char name[14];
int n2[10];
} ex;
int ReadEx(ex *dest) {
char buf[20 + 1 + 14 - 1 + 1 + 10 + 2]; // Or use something big like buf[100]
if (fgets(buf, sizeof buf, stdin) == NULL ) {
return -1; // EOF or I/O Error
}
char name[14 + 1]; // temp place for name and final space separator
int p1, p2; // Offset for beginning and end of n2 array
unsigned long long ull; // Dummy to save n2 array
if (3 != sscanf(buf, "%d %14[A-Za-z ]%n%llu%n",
&dest->n1, name, &p1, &ull, &p2)) {
return 1; // format error
}
// strlen(name) cannot have the value of 0 here as sscanf() is 3
size_t SpaceIndex = strlen(name) - 1;
if ((name[SpaceIndex] != ' ') || ((p2 - p1) != 10)) {
return 1; // format error, space expected at end of name and 10 digits
}
// name cannot contain only spaces due to the space before %14
while (name[SpaceIndex] == ' ') {
name[SpaceIndex--] = '\0';
}
strcpy(dest->name, name);
int i;
for (i = 0; i < 10; i++) {
dest->n2[i] = buf[p1 + i] - '0';
}
return 0;
// One could use the below approach but it is not so extensible.
// if (12 != sscanf(buf, "%d %14[A-Za-z ]%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d %c",
// &dest->n1, name, &dest->n2[0], &dest->n2[1], ... &dest->n2[9], &dummychar)) {
}