您只为 name 和 affiliation_number 的缓冲区地址分配了空间,但从未为这些地址分配缓冲区本身。
因此,当您在 中使用它们时fscanf()
,就会出现问题。编译器通过这些注释警告您(无论如何都是 gcc)指针类型错误 - 它们应该是您要fscanf
覆盖的目标缓冲区中第一个字节的地址:
foo.c:19:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char **’ [-Wformat]
foo.c:20:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char **’ [-Wformat]
这里有一些事情要做——如果你的系统不支持 fscanf 的"%ms"
规范,试试这个:
- 在obtain_name_affiliation 中,将
char buffer[1024]
数据读入。
- 使用
buffer
或&buffer[0]
(对于某些纯粹主义者)作为字符串 fscanfs 中的目标
- 使用
%1023s
或类似的方法来防止读取的数据超出缓冲区的长度 - 超出可能会使您的程序发疯。
- 如果 fscanf 返回成功(对于预期的一个字段,fscanf 应该返回值 1,否则输入可能是错误的),使用
strdup
将数据克隆到name
oraffiliation_number
中。这将malloc()
是一个新的内存块,其大小适合您读取的字符串并将数据复制到其中。
无论您使用这些步骤还是"%ms"
方法,都需要free()
稍后编辑缓冲区以避免内存泄漏!
这有点简单(特别是 1024 的限制),但应该能让你走上正确的道路。
例子:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* tested with user_data.txt containing "12 Barney 42" or "12 Barney" */
struct myret {
int age;
char *name;
char *affiliation_number;
};
int obtain_name_affiliation_number(struct myret *r)
{
int success = 0;
int age;
char *name = 0;
char *affiliation_number = 0;
FILE *user_data = fopen("user_data.txt", "r");
if(user_data)
{
#if 0 /* use if you have "%ms" */
if((1 == fscanf(user_data, "%d", &age)) &&
(1 == fscanf(user_data, "%ms", &name)) &&
(1 == fscanf(user_data, "%ms", &affiliation_number)))
{
success = 1;
} else {
/* a small annoyance: if only the first "%ms" succeeded,
* we need to free it:
*/
if(name)
free(name);
}
#else
char buffer[1024];
/* This if-structure can be used with the "%ms" as well, and
* would make the "annoyance" look a lot cleaner
*/
if(1 == fscanf(user_data, "%d", &age))
{
if(1 == fscanf(user_data, "%1023s", buffer))
{
name = strdup(buffer);
if(1 == fscanf(user_data, "%1023s", buffer))
{
affiliation_number = strdup(buffer);
success = 1;
}
}
}
#endif
fclose(user_data);
} else perror("error opening data file");
if(success)
{
r->age = age;
r->name = name;
r->affiliation_number = affiliation_number;
}
return success;
}
int main(void)
{
struct myret r;
int rc = obtain_name_affiliation_number(&r);
if(rc) {
printf("%d %s %s\n", r.age, r.name, r.affiliation_number);
free(r.name);
free(r.affiliation_number);
}
else
fputs("an error occurred reading data\n", stderr);
getchar();
return 0;
}
还有其他方法。例如, name
andaffiliation_number
可以在结构中声明char name[512];
,但是几乎不可能提前选择这个数字,希望有任何正确性。相反,这很常见:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct myret *myret_alloc(int age, char *name, char *affiliation_number)
{
struct myret *r = 0;
if(r = (struct myret*)calloc(1, sizeof(struct myret))) {
r->age = age;
r->name = name; /* or use r->name = strdup(name); */
r->affiliation_number = affiliation_number;
/* note that using strdup would mean the calling function should
* do its own cleanup of its own name and affiliation_number vars
*/
}
return r;
}
void myret_free(struct myret *r)
{
/* this can be called on partially-allocated myret objects */
if(r->affiliation_number)
free(r->affiliation_number);
if(r->name)
free(r->name);
free(r);
}
然后其他两个函数变为(假设fscanf
can "%ms"
):
struct myret *obtain_name_affiliation_number(void)
{
struct myret *r = (struct myret*)0;
FILE *user_data = fopen("user_data.txt", "r");
if(user_data)
{
int age;
char *name = 0; /* the 0 allows us to see if it was used later */
char *affiliation_number = 0;
if((1 == fscanf(user_data, "%d", &age)) &&
(1 == fscanf(user_data, "%ms", &name)) &&
(1 == fscanf(user_data, "%ms", &affiliation_number)))
{
/* The name and affiliation_number were malloc()ed by "%ms"
* so there's nothing to clean up in this function, and
* we can let myret_free() just free those memory areas.
* This also means myret_alloc doesn't need strdup().
*/
r = myret_alloc(age, name, affiliation_number);
} else {
if(name) /* clean up name if it got allocated */
free(name);
}
fclose(user_data);
} else perror("error opening data file");
return r;
}
int main(void)
{
struct myret *r = obtain_name_affiliation_number();
if(r) {
printf("%d %s %s\n", r->age, r->name, r->affiliation_number);
myret_free(r);
}
else
fputs("an error occurred reading data\n", stderr);
getchar();
return 0;
}
您也可以同时使用fscanf
所有三个:
if(3 == fscanf(user_data, "%d %ms %ms", &age, &name, &affiliation_number))
...
祝你好运!