Good evening!
I made a check of strings, but then it does not display, only the very first line. That is, it is necessary for me, that he would deduce all from the cities of Tallinn and Tartu .
That can not be done. I would appreciate the help!
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 10 #define Sort1 "Tallinn" #define Sort2 "Tartu" struct data { //Personal data structure char name[21]; //Name of person int mphone; //Mobile phone of person struct ads { //Structure of Address char street[101]; //Street address char city[101]; //City } stads; }; //Function prototypes int readData (char[], struct data[]); //Read file int resCity (char[], struct data[]); //Residents of Tartu //Function to Read Data from file. int readData (char fname[], struct data st[]) { FILE *fopen(), *fData; int size=0; if((fData=fopen("F1.txt", "r")) == NULL){ printf("Error oppening file F1.txt"); exit(1); } while((fscanf(fData, "%s %d %s %s", st[size].name, &st[size].mphone, st[size].stads.street, st[size].stads.city)) == 4) { if(size == N) break; size++; } return size; fclose(fData); } //Function to find residents of Tallinn and Tartu int resCity (char fname[], struct data st[]) { FILE *fopen(), *fData; int size3=0; int k=0; if((fData=fopen("F2.txt", "w")) == NULL){ printf("Error oppening file F1.txt"); exit(1); } //Residents of Tallinn printf("\n\nResidents of Tallinn:\n"); while(strcmp (st[size3].stads.city,Sort1)!=0) { size3++; } puts(st[size3].stads.city); fprintf (fData, "%s %d %s %s\n", st[size3].name, st[size3].mphone, st[size3].stads.street, st[size3].stads.city); //Residents of Tartu printf("\n\nResidents of Tartu:\n"); while(strcmp (st[size3].stads.city,Sort2)!=0) { size3 ++; } puts(st[size3].stads.city); fprintf (fData, "%s %d %s %s\n", st[size3].name, st[size3].mphone, st[size3].stads.street, st[size3].stads.city); return size3; fclose(fData); } int main(void) { FILE *fData; char fname[21]; struct data st[N]; int i; int i3; int size=0; int size3=0; size=readData(fname, st); for(i=0; i < size; i++){ printf("%s %d %s %s\n", st[i].name, st[i].mphone, st[i].stads.street, st[i].stads.city); } size3=resCity(fname, st); return 0; } F1.txt file
Artur 0000000 Vaikne Keila Aleksander 1111111 Ehitajate Tallinn Aisel 2222222 Paekaare Tallinn Aleksandra 3333333 Pae Kunda Sergei 4444444 Kunsti Narva Misha 5555555 Jaama Tartu Dmitri 6666666 Pae Tartu Konstantin 7777777 Paekaare Tallinn Roman 8888888 Linnamae Tallinn Hennadii 9999999 Jaama Tartu