【问题标题】:C - Can't for-loop fputs and fprintf properly. Expected output it totally wrongC - 无法正确循环 fputs 和 fprintf。预期的输出完全错误
【发布时间】:2014-01-28 12:33:34
【问题描述】:

首先,代码如下:

#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 #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 等来实现上述预期输出。

【问题讨论】:

  • 这里发生了几件事:扫描姓名时,不要使用scanf("%s", &amp;name[x]) - 这会将姓名存储在xth 学生的xth 位置。用户scanf("%s", name),虽然只扫描一个单词。接下来是您在循环中打开文件,它将尝试一遍又一遍地重新打开它。不。在循环之前打开文件,检查它是否打开,即不是NULL,做你的循环和fclose 完成后的文件。也请删除 system 命令。

标签: c for-loop fgets printf


【解决方案1】:

嗯,你的第一个错误是你定义了一个 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的答案

【讨论】:

  • 我认为你错了:他只需要 3 个数组(不是 2d 数组),因为他一直在覆盖值
  • 我怎么错了?!对于每一个,他定义了一个他想要存储字符串的一维数组。让我在回答中解释一下。
  • @zmo:潘德雷是对的。他覆盖了循环中每个学生的数据,这没关系,因为他马上又把它写出来了。然而,他在scanf 调用中的格式语法是错误的。
  • 好的,很抱歉我被同事和我说话分心了,我只剩下一半的大脑活跃......所以让我重新回答一下
  • 注:如果您支持我的帖子,请考虑为 pandrei 和 m-oehm 的 cmets 投票,他们的帮助也应该获得一些代表!
猜你喜欢
  • 1970-01-01
  • 2013-04-19
  • 2013-12-15
  • 2018-04-11
  • 2021-10-29
  • 1970-01-01
  • 1970-01-01
  • 2017-06-08
  • 1970-01-01
相关资源
最近更新 更多