Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-未将数据添加到txt文件_C_File_Error Handling_Save - Fatal编程技术网

C-未将数据添加到txt文件

C-未将数据添加到txt文件,c,file,error-handling,save,C,File,Error Handling,Save,我应该为一家私人诊所创建一个小客户管理程序(在你问之前,是的,这是大学的工作),但我似乎发现了两个不允许我在这方面取得进展的bug #include <stdio.h> #include <stdlib.h> #include <string.h> struct DATA { int dia, mes, ano; }; struct cliente { char pnome[15]; char snom

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

#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;
}
#包括
#包括
#包括
结构数据{
国际、中东和非洲;
};
结构客户端{
char-pnome[15];
charsnome[15];
char telefone[10];
char-bi[10];
浮动比索,阿尔图拉;
结构数据;
};
int main(){
文件*c,*tempo;
int op,i;
内特斯特勒莫维尔,sbi;
字符文件名[15];
结构客户a;
printf(“1-采购商”);
printf(“2-inserir novo\n”);
printf(“3-gravar\n”);
printf(“4-sair\n”);
scanf(“%d”和&op);
开关(op){
**(...)**
案例2:
printf(“novo cliente.Dados:\n”);
tempo=fopen(“temp.txt”,“a”);//打开一个文件来存储数据,以便我以后可以复制它,这是为了纠正一个错误,我将在前面解释
printf(“primeiro nome\n”);
scanf(“%s”,&a.pnome);//扫描名字
fprintf(节奏,“%s”,a.pnome);//打印到节奏文件
printf(“segundo nome\n”);
scanf(“%s”,&a.snome);//第二个名称
fprintf(节奏,“%s”,a.snome);//打印
printf(“peso\n”);//到目前为止,您已经了解情况了
做{
加拿大法郎(“%f”和a.peso);
}而(a.peso电话线:

char telefone[9];
太小,无法容纳您输入的电话号码加上字符串的空对位符。将其更改为更大的值,如:

char telefone[16];
可以。但这里的问题是,您不必检查输入数据是否存在缓冲区溢出。

始终检查最大输入。

您的代码同时打印
电话
bi
的原因是您没有在字符串末尾为“\0”留出足够的空间。C中的字符串以null结尾。这也表明您没有清理输入。正在执行
扫描(“%s”,str)非常危险,导致缓冲漏洞。你应该给它提供一个宽度,如<代码> SCANF(“%8s”,STR);< /代码>或考虑使用诸如<代码> fgScript()/<代码> < < /P>
此缓冲区最多可容纳8个字符的字符串,加上将
'\0'
作为第9个元素终止。写入
XXX XXX XXX
会导致缓冲区溢出。请将缓冲区扩大到至少10个字符,并在每次调用中始终检查
scanf
的返回值:

char telefone[10];
if (scanf("%s", &a.telefone) != 0)
     // handle error
考虑使用FGET以避免缓冲区溢出:

char buf[10];
fgets(buf, sizeof(buf), stdin);

发布的代码的一个主要问题是在已打开的文件上使用相同的变量重复调用
fopen()


建议在每次新的递归之前和退出程序之前调用
fclose()

您不应该将问题的一部分作为图像发布,尤其是在这种情况下,您正在拍摄文本的屏幕截图。请将文本放在此处。(…)只是代码中与问题无关的部分,只是为了节省空间,所以我省略了我没有问的其他情况…返回main()这是我从一开始就重复这个例子的方法,这是我认为重新启动case.Stop.Bean.Counting最简单的方法。除非您使用的是RAM受限的嵌入式控制器,否则不要声明小于128的字符缓冲区。只要停止编写小于[128]的任何内容即可,就好像这是语言中的一个基本限制。如果在任何时候,你认为这是一个不合适的大小,就把它加倍。如果开发人员将它加倍,并添加他们最初想到的数量,那么发布的文章就会更少。Weenie buffers,例如[9],[15],[10],不值这个名字。如果你是男性,他们会让你不孕不育,如果是女性,他们会让你难看不育。为了避免社交尴尬,今天就加入128俱乐部吧!一般来说,递归调用main()是一个非常糟糕的主意。除其他原因外,main()有一组非常特殊的参数。建议使用main()调用另一个函数,另一个函数执行递归。我会记下这一点以备将来使用……老实说,我不知道你可以限制这样的字符,从来没有这样的习惯……我个人更喜欢使用“get”最近有很多事情让事情变得更简单了…@Xiriko强烈建议在本地或在线上广泛使用
man
页面。对发布的代码调用的每个系统函数都这样做。这样你就知道了格式说明符的修饰符(以及许多其他重要细节)
char buf[10];
fgets(buf, sizeof(buf), stdin);