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
进入上面的搜索框),其中一些(here, here and here,例如)包括我一直以来最喜欢的功能,getLine
,为了更安全的用户输入。