2013-03-19 66 views
0

我正在创建一个程序,该程序应该创建用户输入的人员列表的结构;我遇到的唯一问题是让用户输入数据出现在文本文件中。有人知道怎么做吗?下面是代码:用户输入到文件

#include<stdio.h> 
#include<stdlib.h> 
#include<string.h> 

struct person{ 
    char name[20]; 
    int age; 
    struct person *next_ptr; 
    } PERSON; 

int main (void){ 


struct person PERSON; 

FILE *fp; 
char ans, ch; 
int ppl=0; 

fp=fopen("person_struct", "w"); 

if(fp != NULL){ 

while(ppl<25){ 


printf("Would you like to add a person to the list? [y/n] "); 
scanf("%c", &ans); 

if(ans == 'y') { 
    printf("\nEnter a name:\n"); 
    scanf("%s", PERSON.name); 
    fprintf(fp, "%s",PERSON.name); 
    printf("\nEnter age:\n"); 
    scanf("%i", &PERSON.age); 
    fprintf(fp, " %i\n", PERSON.age); 
} 
else { 
    ppl=25;  
} 

ppl++; 
} 
fclose(fp); 
} 
printf("\n\n\n"); 
system("pause"); 
return 0; 
} 

回答

3

又饿scanf的说法是错误的,你PERSON.age之前忘记符号&运营商的INT

scanf("%i", PERSON.age); 
     ^& missing 

正确的是:

scanf("%i", &PERSON.age); 

你有两个scanf的雄蕊您代码从用户输入字符串到扫描名称。

scanf("%s", PERSON.name); 

这是正确的,不需要&之前的字符串。但是年龄是int并且要扫描int.float,您需要在变量之前添加&,这是为什么在PERSON.age之前添加&符号&。 REF:scanf

二:

fputs(PERSON.age, fp);是的fputs的错误的语法是:

int fputs(const char *str, FILE *stream); 
       ^you are passing int 

第一个参数应该是const char*,但你是路过int

fputs(PERSON.age, fp); 
    ^wrong , age is int not char* 

当你需要格式化inp UT /输出喜欢的printf和scanf函数,我的建议是改变你的读/写,如:(阅读评论

printf("Enter a name:\n"); 
scanf("%s", PERSON.name); // here is No & because `name` is string 
scanf("%i", &PERSON.age); // age is `int` so & needed 
fprintf(fp,"%s %i\n",PERSON.name, PERSON.age); 

编辑:因为你评论,你的代码是这些整改工作后,看到

$ gcc x.c -Wall 
$ ./a.out 
Would you like to add a person to the list? [y/n]y 
Enter a name: 
yourname 
14 
Would you like to add a person to the list? [y/n]y 
Enter a name: 
firendName 
15 
Would you like to add a person to the list? [y/n]n 
sh: 1: pause: not found 
$ cat person_struct.txt 
yourname 14 
firendName 15 
+0

...,为什么是否需要&符号?如果您要解释这一点,请确保完全解释它,以免人们感到困惑,并开始在'scanf(“%s”,PERSON.name);'中的PERSON.name之前添加&符号一个指针。一个不太多余的方法是链接到[scanf手册](http://pubs.opengroup.org/onlinepubs/007904975/functions/fscanf.html)并询问“哪个类型对应于'%i '格式说明符?“。 – Sebivor 2013-03-19 02:59:58

+0

仍然无法使用;当我添加符号时,该程序在进入年龄之后崩溃。 – user2184761 2013-03-19 03:00:13

+0

@modifiablelvalue是的,你是正确的我应该加上这一点很重要这里 – 2013-03-19 03:02:33

1

除了Grijesh的回答是:

请解释scanf("%s", &ans);。你可以存储多少个字符?字符串“y”需要存储多少个字符?验证你的信念:printf("sizeof ans: %zu\n" "sizeoof \"y\": %zu\n", sizeof ans, sizeof "y");

也许你的意思是:if (scanf("%c", &ans) != 1) { /* assume stdin has closed or reached EOF */ }。请注意0​​,其中将只读取一个字符到ans

另外,如果你改变ANS到int,您可以使用:ans = getchar();

编辑:总之,我认为你的循环应该是这个样子:

for (size_t ppl = 0; ppl < 25; ppl++){ 
    int ans; 

    printf("Would you like to add a person to the list? [y/n]"); 
    do { 
     ans = getchar(); 
    while (ans >= 0 && isspace(ans)); 

    if (ans != 'y') { 
     break; 
    } 

    printf("Enter a name:\n"); 
    if (scanf("%s", PERSON.name) != 1 || scanf("%i", &PERSON.age) != 1) { 
     break; 
    } 
    fprintf(fp, "%s %i\n", PERSON.name, PERSON.age); 
} 
+0

是谢谢添加我的回答:) – 2013-03-19 03:13:37

+0

如果我将scanf(“%s”,&ans);更改为scanf(“%c”,&ans),循环将只有经过一次 – user2184761 2013-03-19 03:14:33

+0

@ user2184761那么,至少'scanf(“%c”,&ans)'不是未定义的行为,为什么你认为循环只能经历一次?你认为这是因为ans包含的东西不是't'y'?这个字符是什么?你认为当你遇到诸如''\ n''这样的空白时(例如'isspace(ans)'),你可能需要'继续';'再次循环?' – Sebivor 2013-03-19 03:18:47