B
beginner10
How can i changhe this code showing how many persons i saved to the file?
And it prints pretty much rubbish. Where is the problem?
#include <stdio.h>
int main()
{
int i;
FILE *data_file;
char list[] = "list.txt";
struct person {
char firstname[20];
char lastname[25];
int phonenumber;
};
struct person person_list[50];
for(i=0; i<1; i++) {
printf("Give your firstname:");
scanf("%s", person_list.firstname);
printf("Give your lastname:");
scanf("%s", person_list.lastname);
printf("Give your phone number:");
scanf("%d", &person_list.phonenumber);
}
if ((data_file = fopen(list, "w")) == NULL) {
printf("Error opening file");
return 0;
} else {
for(i=0; i<=2; i++)
{
fprintf(data_file, "%s %s %d\n", person_list.firstname,
person_list.lastname, person_list.phonenumber);
}
}
fclose(data_file);
printf("Data saved succesfully!");
return 0;
}
And it prints pretty much rubbish. Where is the problem?
#include <stdio.h>
int main()
{
int i;
FILE *data_file;
char list[] = "list.txt";
struct person {
char firstname[20];
char lastname[25];
int phonenumber;
};
struct person person_list[50];
for(i=0; i<1; i++) {
printf("Give your firstname:");
scanf("%s", person_list.firstname);
printf("Give your lastname:");
scanf("%s", person_list.lastname);
printf("Give your phone number:");
scanf("%d", &person_list.phonenumber);
}
if ((data_file = fopen(list, "w")) == NULL) {
printf("Error opening file");
return 0;
} else {
for(i=0; i<=2; i++)
{
fprintf(data_file, "%s %s %d\n", person_list.firstname,
person_list.lastname, person_list.phonenumber);
}
}
fclose(data_file);
printf("Data saved succesfully!");
return 0;
}