[英]Reading text file data into an array of structs (while ignoring comment lines)
概述:
以下程序的目的是将输入文件中的数据逐行读取到结构数组中,同时忽略输入文件中以character '#'
开头的任何注释行。 然后程序应该遍历结构数组并打印内容,以确认程序按预期工作。
这是输入文件的示例,其中可以看到 3 行非注释数据。 非注释行的数量在编译之前是已知的,如下面的尝试所示int Nbodies = 3
行。
30 07 6991
# some comment
28 02 4991
09 09 2991
注意:请注意,在决定发布此问题之前,已经研究了以下 SO 问题:
困境:
该程序可以成功地将行读入结构数组并在没有注释行时打印内容。 该程序还可以成功检测一行何时以“#”字符开头,从而将其视为注释行。 问题在于,即使检测到注释行,程序仍会尝试错误地将这一行读入结构数组中。
这是预期的 output:
30 07 6991
28 02 4991
09 09 2991
这是实际的(和不正确的)output,它似乎忽略了最后一行未注释的数据:
30 07 6991
-842150451 -842150451 -842150451
28 02 4991
当前尝试:
fgets
已用于读取每一行,从而确定该行的开头是否以'#'
开头。 此注释检查在IF
语句中执行,该语句在FOR
循环条件中递增Nbodies
变量(这样迭代不会在注释行上“浪费”,如果这有意义吗?)。 在此之后,使用sscanf
尝试将当前非注释行的三个值读取到结构数组中。 fscanf
也是一种尝试过的方法,但没有奏效。 通过使用continue;
在示例中看到的 IF 语句中,如果检测到注释行,不应该“跳过” sscanf
吗? 它似乎没有按预期进行。
到目前为止的代码:
#include "stdio.h"
#define EXIT_SUCCESS 0
#define EXIT_FAILURE !EXIT_SUCCESS
int main() {
typedef struct {
int a1, b1, c1;
}DATA;
FILE *file = fopen("delete.nbody", "r");
if (file == NULL)
{
printf(stderr, "ERROR: file not opened.\n");
return EXIT_FAILURE;
}
int Nbodies = 3;
int comment_count = 0;
DATA* data = malloc(Nbodies * sizeof * data); // Dynamic allocation for array
char line[128]; // Length won't be longer than 128
int x;
for (x = 0; x < Nbodies; x++)
{
fgets(line, sizeof(line), file);
if (line[0] == '#')
{
comment_count++;
Nbodies++;// Advance Nbodies so that iteration isn't 'wasted' on a comment line
continue;
}
// QUESTION: doesn't "continue;" within above IF mean that the
// following sscanf shouldn't scan the comment line?
sscanf(line, "%d %d %d", &data[x].a1, &data[x].b1, &data[x].c1);
}
// Nbodies - comment_count, because Nbodies advanced
// every time a comment was detected in the above FOR loop
for (x = 0; x < Nbodies - comment_count; x++)
{
printf("%d %d %d\n", data[x].a1, data[x].b1, data[x].c1);
}
return (EXIT_SUCCESS);
}
问题:
谁能明白为什么这个程序不起作用? 我原以为continue
字会在检测到时跳过 sscanf 读取注释行。 任何帮助将不胜感激。
for (x = 0; x < Nbodies; ) {
int rc;
if ( !fgets(line, sizeof line, file)) break;
if (line[0] == '#')
{
comment_count++;
continue;
}
rc = sscanf(line, "%d %d %d", &data[x].a1, &data[x].b1, &data[x].c1);
if (rc != 3) continue;
x++;
}
如果您收到评论,请不要增加Nbodies
。
这是错误的方法,因为由于预循环malloc
, data
仅限于Nbodies
的原始值
而且,您将在data
数组中引入一个间隙。
这是一种更好的方法,使data
只包含有效数据。 注释行变得完全“不可见”(即它们不影响数据的计数):
int
main()
{
typedef struct {
int a1,
b1,
c1;
} DATA;
FILE *file = fopen("delete.nbody", "r");
if (file == NULL) {
printf(stderr, "ERROR: file not opened.\n");
return EXIT_FAILURE;
}
int Nbodies = 3;
int comment_count = 0;
// Dynamic allocation for array
DATA *data = malloc(Nbodies * sizeof *data);
char line[128]; // Length won't be longer than 128
int x;
#if 0
for (x = 0; x < Nbodies; x++) {
fgets(line, sizeof(line), file);
if (line[0] == '#') {
comment_count++;
// Advance Nbodies so that iteration isn't 'wasted' on a comment
Nbodies++;
continue;
}
// QUESTION: doesn't "continue;" within above IF mean that the
// following sscanf shouldn't scan the comment line?
sscanf(line, "%d %d %d", &data[x].a1, &data[x].b1, &data[x].c1);
}
#else
int inc = 1;
for (x = 0; x < Nbodies; x += inc) {
fgets(line, sizeof(line), file);
inc = (line[0] != '#');
if (! inc) {
comment_count++;
continue;
}
// QUESTION: doesn't "continue;" within above IF mean that the
// following sscanf shouldn't scan the comment line?
sscanf(line, "%d %d %d", &data[x].a1, &data[x].b1, &data[x].c1);
}
#endif
// Nbodies - comment_count, because Nbodies advanced
// every time a comment was detected in the above FOR loop
#if 0
for (x = 0; x < Nbodies - comment_count; x++) {
#else
for (x = 0; x < Nbodies; x++) {
#endif
printf("%d %d %d\n", data[x].a1, data[x].b1, data[x].c1);
}
return (EXIT_SUCCESS);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.