C - 无法正确循环 fputs 和 fprintf。预期输出完全错误

标签 c for-loop fgets printf

首先,这是代码:

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
FILE *stream;

int main()
{
    int clients;
    printf("Number of Clients: ");
    scanf ("%d", &clients);
    int age;
    char name[100];
    char gender[100];
    char test[100];

    for (int x = 0; x < clients; x++)
    {
        printf("Enter your name: ");
        scanf("%s", &name[x]);
        printf("Enter your age: ");
        scanf("%d", &age);
        printf("Enter your gender (M/F): ");
        scanf("%s", &gender[x]);

        stream = fopen ("input.txt", "w");

        fprintf (stream, "Client #%d\n", x+1);
        fputs ("Name:   ", stream);
        fputs (name, stream);
        fprintf (stream, "\nAge:    %d", age);
        fputs ("\nGender: ", stream);
        fputs (gender, stream);
        fclose (stream);

    }

    system("input.txt");
    stream = fopen ("input.txt", "w");

    return 0;
}

有1个client时输出正常:

Client #1
Name:   Jack
Age:    23
Gender: M

但是,当添加客户端数量时,一切都出错了:

Client #2
Name:   JSam
Age:    10
Gender: MF

预期输出应该是:

Client #1
Name:   Jack
Age:    23
Gender: M

Client #2
Name:   Sam
Age:    10
Gender: F

由于我没有编码经验,我尝试但失败了:

  • 为姓名、年龄和性别设置一个数组。 (例如 name[x], age[x] | 不确定这是否构成数组)
  • 尝试更改 fopen 以便每个条目生成多个 txt 文件 http://ubuntuforums.org/showthread.php?t=1098904 - 失败,因为我无法摆脱所有错误。

如果有人能指出我做错了什么或帮助我修改代码,我将不胜感激。 我的主要目标是通过在 for 循环中使用 fputs、fprints、fgets 等来实现上述预期输出。

最佳答案

好吧,您的第一个错误是您定义了一个包含 100 个字符的字符数组,然后像双维数组一样递增它:

int age;
char name[100];
char gender[100];
char test[100];

让我们运行你的例子,这里是你的数组的内存:

name = [ ][ ][ ][ ][ ][ ]

然后你在0的位置写入这个“Jack”:

name = [J][a][c][k][\0]

然后你在 1 的位置写入这个 "Sam":

name = [J][S][a][m][\0]

这就是你的错误!

所以基本上,您想要的是重用您的变量,而不是无用地迭代它们(您可以感谢@Pandrei 和@M-Oehm 纠正我的那部分):

int main()
{
    int clients;
    printf("Number of Clients: ");
    scanf ("%d", &clients);
    int age;
    char name[100];
    char gender;
    char test[100];

    stream = fopen ("input.txt", "w");

    for (int x = 0; x < clients; x++) {
        printf("Enter your name: ");
        scanf("%s", &name); // here you rewrite your variable
        printf("Enter your age: ");
        scanf("%d", &age); // here you were already doing it
        printf("Enter your gender (M/F): ");
        scanf("%c ", &gender); // here you only need one character, you don't need a full string 


        fprintf (stream, "Client #%d\n", x+1);
        fputs ("Name:   ", stream);
        fputs (name, stream);
        fprintf (stream, "\nAge:    %d", age);
        fputs ("\nGender: ", stream);
        fputs (gender, stream);

    }

    fclose (stream);



    // I don't understand what you meant below, but executing a text file might not work, and if it works could be **really** dangerous, the below should be erased!
    // system("input.txt");
    // here you open again a stream without using it!
    // stream = fopen ("input.txt", "w");

    return 0;
}

请注意,如果您使用超过 100 个字符的名称,您可能会遇到缓冲区溢出问题。这就是为什么您最好使用 fgets() 而不是 scanf()

但是你绝对应该先打开Kernighan and Ritchie book并阅读有关内存管理、字符数组或字符串的章节……

您还应该查看 Stack Overflow 上关于“为什么 scanf 是危险的?”的答案,或者 FAQ entry

关于C - 无法正确循环 fputs 和 fprintf。预期输出完全错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21405649/

相关文章:

c - 在 C 中处理输入

PHP strtotime() 函数

C编程: Reading from file and printing to console

c - 如何在结构中为 c 中的字符串创建内存分配?

c - MSG_PEEK 套接字读取/接收时的错误行为

python - 使用 for 循环创建元组。

c# - 是让一个 "for loop"循环遍历 9 个数组还是让 9 个 "for loops"每个循环遍历一个数组更好

c - strncpy 段错误,但可以手动分配字符串

更改结构模块指针

reactjs - "JSX maps to function calls"是什么意思