I' ll be glad if anyone could help...my problem is the following :
Question_1:
when i run the program i press 1 see some stats, then i press 2 so, i typed name, email and telephone.
after that i typed again 1 and doesn' t respond. So in the 2nd if i closed and opened the infile...i am not sure if it is ok, but i runs as i wish...is it correct?
Question_2:
having the r+ mod in the fclose fuction allows you to read and write and NOT append. Append means write something in the end of the file, whereas write erases everything into the file.txt and writes again in the begining. In this program everything into the file.txt does not erase...why?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Details{
char l_name[81];
char f_name[81];
char email[81];
int tel;
};
int main(void){
FILE *infile;
struct Details det;
int cont_num, cont_stats, ch;
infile=fopen("lala.txt","r+");
if(infile==NULL){
printf("file didn' t found.\n");
exit(1);
}
printf("file found.\n");
fscanf(infile,"%d %d", &cont_num, &cont_stats);
printf("There are %d contacts.\n", cont_num);
printf("There are %d statistics in every contact.\n", cont_stats);
printf("To just see the contacts type '1'.\n");
printf("To apend them type '2',\n");
printf("To exit type '0'.\n");
scanf("%d", &ch);
do{
if(ch==1){
while(fscanf(infile,"%s %s %s %d", det.f_name, det.l_name, det.email, &det.tel)!=EOF){
printf("first name: %s\tlast name: %s\te-mail: %s\ttelephone: %d\t\n", det.f_name, det.l_name, det.email, det.tel);
}
}else if(ch==2){
printf("Type your first name:\n");
scanf("%s", det.f_name);
printf("Type your last name:\n");
scanf("%s", det.l_name);
printf("Type your e-mail:\n");
scanf("%s", det.email);
printf("Type your telephone:\n");
scanf("%d", &det.tel);
fprintf(infile,"%s %s %s %d", det.f_name, det.l_name, det.email, det.tel);
fclose(infile); // close the file and ->
infile=fopen("lala.txt","r+"); // open again the file....
}else{
printf("\nThank you for choosing my program.\n\n");
exit(0);
}
printf("To just see the contacts type '1'.\n");
printf("To apend them type '2',\n");
printf("To exit type '0'.\n");
scanf("%d", &ch);
}while(ch!=0);
return 0;
}