Reputation: 11
I'm supposed to be creating a small client managing program for a private clinic (before you ask, yes this is college work) but I seem to have found 2 bugs that don't allow me to progress on this.
#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 gets printed normally next though which basically tells me the problem is on a.telefone but I can't seem to see it.
I'm writing this here because I'm really out of ideas, slightly saturated too and as you can probably guess I'm not exactly a pro on files. Thanks in advance for any help being provided though. I'm sure the solution is probably simple but I can't seem to see it at this point...
EDIT
Now if ya guys can help me on why I'm not able to print the stuff I scan to a file this matter can be considered closed.
EDIT 2
Ok seems like it's printing to a file but it's not separating the words through commas.
Upvotes: 1
Views: 47
Reputation: 4034
The reason your code is printing telephone
and bi
together is because you didn't leave enough space for the '\0' at the end of the string. Strings in C are null terminated. This also indicates your not sanitizing your inputs. Doing scanf("%s", str);
is very dangerous and leads to buffer exploits. You should provide a width to it like scanf("%8s", str);
or consider using something like fgets()
.
Upvotes: 1
Reputation: 16540
a major problem with the posted code is repeatedly calling fopen()
with the same variable on a file that is already open.
Suggest calling fclose()
before every new recursion AND before exiting the program.
Upvotes: 0
Reputation: 29724
char telefone[9];
This buffer can hold a string of up to 8 characters, plus terminating '\0'
as the 9th element. You are experiencing buffer overflow by writing XXX XXX XXX
to it. Enlarge the buffer to at least 10 characters and always check the returned value of scanf
in each call:
char telefone[10];
if (scanf("%s", &a.telefone) != 0)
// handle error
Consider using fgets to avoid buffer overflow:
char buf[10];
fgets(buf, sizeof(buf), stdin);
Upvotes: 0
Reputation: 4877
The telephone string:
char telefone[9];
Is too small to held the phone number you punched in plus the null tereminator of the string. Changing it to something larger as:
char telefone[16];
Will work. But the probllem here is that you don't make any check on the input data for buffer overflow.
Always check for max input.
Upvotes: 0