我有一个包含以下格式的数据的文本文件:
Lee AUS 2 103 2 62 TRUE
Check AUS 4 48 0 23 FALSE
Mills AUS 8 236 0 69 FALSE
我需要将每一行转换为类似的结构,但是我想避免使用固定长度的数组(据我所知,fgets的问题):
struct Data
{
char *sname;
char *country;
int *a;
int *b;
int *c;
int *d;
char *hsisno;
};
我是C的新手。我应该使用fscanf,还是fgets?
答案 0 :(得分:4)
fscanf
代表“文件扫描格式化”,用户数据与未格式化大致相同。
对于您无法完全控制可读内容的数据,不应使用裸"%s"
格式字符串。
最好的解决方案是使用fgets
来读取一行,因为这样可以防止缓冲区溢出。
然后,一旦你知道你的线的大小,那就是你需要的每个字符串的最大大小。使用sscanf
到您心中的内容来获取实际字段。
最后一件事。由于您知道它们已经具有特定的最大大小,因此整数的int*
类型可能有点浪费。我会使用非指针变体,如:
struct Data {
char *sname; char *country;
int a; int b; int c; int d;
char *hsisno;
};
举个例子,这是一些安全的代码:
#include <stdio.h>
#include <string.h>
// Here's all the stuff for a linked list of your nodes.
typedef struct sData {
char *sname; char *country; char *hsisno;
int a; int b; int c; int d;
struct sData *next;
} Data;
Data *first = NULL; Data *last = NULL;
#define MAXSZ 100
int main (void) {
char line[MAXSZ], sname[MAXSZ], country[MAXSZ], hsisno[MAXSZ];
int a, b, c, d;
FILE *fIn;
Data *node;
// Open the input file.
fIn = fopen ("file.in", "r");
if (fIn == NULL) {
printf ("Cannot open file\n");
return 1;
}
// Process every line.
while (fgets (line, sizeof(line), fIn) != NULL) {
// Check line for various problems (too short, too long).
if (line[0] == '\0') {
printf ("Line too short\n");
return 1;
}
if (line[strlen (line)-1] != '\n') {
printf ("Line starting with '%s' is too long\n", line);
return 1;
}
line[strlen (line)-1] = '\0';
// Scan the individual fields.
if (sscanf (line, "%s %s %d %d %d %d %s",
sname, country, &a, &b, &c, &d, hsisno) != 7)
{
printf ("Line '%s' didn't scan properly\n", line);
return 1;
}
// Allocate a new node to hold data.
node = malloc (sizeof (Data));
if (node == NULL) {
printf ("Ran out of memory\n");
return 1;
}
node->sname = strdup (sname);
node->country = strdup (country);
node->a = a;
node->b = b;
node->c = c;
node->d = d;
node->hsisno = strdup (hsisno);
node->next = NULL;
if (first != NULL) {
last->next = node;
last = node;
} else {
first = node;
last = node;
}
}
fclose (fIn);
// Output the list for debugging.
node = first;
while (node != NULL) {
printf ("'%s' '%s' %d %d %d %d '%s'\n",
node->sname, node->country, node->a, node->b,
node->c, node->d, node->hsisno);
node = node->next;
}
return 0;
}
读取您的文件并将其存储在链接列表中。它输出:
'Lee' 'AUS' 2 103 2 62 'TRUE'
'Check' 'AUS' 4 48 0 23 'FALSE'
'Mills' 'AUS' 8 236 0 69 'FALSE'
最后,正如预期的那样。
我对在非受控数据上使用*scanf
函数的陷阱(在上面的搜索框中输入user:14860 fgets
)做了一系列的答案,其中一些({{3}例如,} here和here包含我常用的最喜欢的函数getLine
,用于更安全的用户输入。