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 

    1 answer 1

    First, why did you announce some new FILE *fopen() function that does not accept parameters? I personally have not compiled your code because of this, remove it.

    Secondly, your file will not close, after the return code in the function is not executed.

     return size3; fclose(fData); 

    Thirdly, why did you decide that your code will output all where the string matches? You find the first one for which this is how you output it, once, outside the loop. If you want the output to be for everyone, you need to put it in a loop that runs through the entire array.

    • And on the contrary, I don’t compile without it)) I tried to add cycles, either nothing, or the first one, so I turned here, it doesn’t reach me ... - Artur Butkevitsus
    • @Yuriy Orlov: The FILE *fopen() declaration is, of course, unnecessary. However, in C, it does not declare the fopen function as "not accepting parameters". It declares the function fopen to accept an unknown number of parameters. From this ad code can not stop compiling. - AnT
    • gcc gave me the error: too many arguments to function 'FILE* fopen()' - Yuriy Orlov