gpt4 book ai didi

C - 数据未添加到 txt 文件

转载 作者:太空宇宙 更新时间:2023-11-04 03:31:59 25 4
gpt4 key购买 nike

我应该为私有(private)诊所创建一个小客户管理程序(在你问之前,是的,这是大学工作)但我似乎发现了 2 个错误,这些错误不允许我在这方面取得进展。

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

struct DATA {
int dia, mes, ano;
};

struct cliente {
char pnome[15];
char snome[15];
char telefone[10];
char bi[10];
float peso, altura;
struct DATA data;
};


int main () {


FILE *c, *tempo;

int op, i;
int stelemovel, sbi;
char filename[15];
struct cliente a;


printf("1 - procurar\n");
printf("2 - inserir novo\n");
printf("3 - gravar\n");
printf("4 - sair\n");

scanf("%d", &op);

switch(op) {


**(...)**


case 2 :

printf("novo cliente. Dados: \n");

tempo = fopen("temp.txt", "a"); //opens a file to store the data on so I can copy it later, this was an attempt to correct a bug I will explain ahead

printf("primeiro nome \n");
scanf("%s", &a.pnome); //scan the first name
fprintf(tempo, "%s", a.pnome); //print to tempo file

printf("segundo nome \n");
scanf("%s", &a.snome); //second name
fprintf(tempo, "%s", a.snome);// print

printf("peso\n"); //you get the picture so far
do{
scanf("%f", &a.peso);
} while (a.peso<1);
fprintf(tempo, "%.2f", a.peso);

printf("altura\n");
do{
scanf("%f", &a.altura);
} while (a.altura<1);
fprintf(tempo, "%.2f", a.altura);

printf("por favor insira data nascimento (dia mes ano)\n");
do {
printf("dia\n");
scanf("%d", &a.data.dia);
} while (a.data.dia<1 || a.data.dia>31);
fprintf(tempo, "%d", a.data.dia);

do {
printf("mes\n");
scanf("%d", &a.data.mes);
} while (a.data.mes<1 || a.data.mes>12);
fprintf(tempo, "%d", a.data.mes);

do {
printf("ano\n");
scanf("%d", &a.data.ano);
} while (a.data.ano<1);
fprintf(tempo, "%d", a.data.ano);

printf("numero telefone\n");
do {
scanf("%s", &a.telefone);
} while (strlen(a.telefone)!=9);
fprintf(tempo, "%d", a.telefone);

printf("numero BI\n");
do {
scanf("%s", &a.bi);
} while (strlen(a.bi)!=9);
fprintf(tempo, "%d", a.bi);

/* printf("%s, %s\n %.2f, %.2f\n %d, %d, %d\n %s, %s\n", a.pnome, a.snome, a.peso, a.altura, a.data.dia, a.data.mes, a.data.ano, a.telefone, a.bi); */
/*this was something I used to test out if the data was saving properly
which is EXCEPT for the a.telefone and the a.bi, they're being printed together for some reason

*/
return main();


case 3 :
printf("nome do ficheiro\n");
scanf("%s", &filename);

c = fopen(filename, "a");

printf("%s, %s\n %.2f, %.2f\n %d, %d, %d\n %s, %s\n", a.pnome, a.snome, a.peso, a.altura, a.data.dia, a.data.mes, a.data.ano, a.telefone, a.bi);

fprintf(c, "%s, %s\n %.2f, %.2f\n %d, %d, %d\n %s, %s\n", a.pnome, a.snome, a.peso, a.altura, a.data.dia, a.data.mes, a.data.ano, a.telefone, a.bi);

/*this part basically should copy all I scanned in case 2 and print it to a document but I can't seem to be able to write on the file. The file is created opened but there's never any content on it */
return main();

**(...)**

return 0;
}

This is the bug I get when printing , a.bi 接下来会正常打印,这基本上告诉我问题出在 a.telefone 上,但我似乎看不到它。

我在这里写这篇文章是因为我真的没有想法,也有点饱和,正如你可能猜到的那样,我并不是文件专家。在此先感谢您提供的任何帮助。我确定解决方案可能很简单,但目前我似乎看不到它...

编辑

现在,如果你们能帮助我解决为什么我无法将我扫描的内容打印到文件中的问题,那么这个问题就可以被视为关闭了。

编辑 2

好吧,它看起来像是在打印到一个文件中,但它并没有用逗号分隔单词。

最佳答案

您的代码同时打印 telephonebi 的原因是您没有为字符串末尾的“\0”留出足够的空间。 C 中的字符串以 null 结尾。这也表明您没有清理您的输入。执行 scanf("%s", str); 非常危险,会导致缓冲区漏洞利用。您应该为其提供宽度,例如 scanf("%8s", str); 或考虑使用类似 fgets() 的方法。

关于C - 数据未添加到 txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35674843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com