Добрый день! Ломаюсь с этим кодом уже очень давно... У меня все работает как надо, вот только маленькая проблема, по заданию нужно, что бы было 2 функции. Помогите пожалуйста.
1 функции сортирует
2 функция выводит на экран и записывает в файл...
2. First, find the residents of Tallinn, then Tartu and finally the rest in the input order.
3. Then display and write the residents of Tallinn, Tartu and the rest to the file “F2.txt“
Вот весь мой код.
Код:
//Title: Homework I "Files and structures"
//Autor: Artur Hrisanfov, MVEB-STF, 152489
//Supervision: Maria Brik, Sergei Kostin
//Date: 02.03.16
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10
#define Sort1 "Tallinn"
#define Sort2 "Tartu"
#define Str "Jaama"
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 Tallinn and Tartu
int Street (char[], struct data[]); //Asking user for street and find all of same.
//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) {
size++;
if(size == N) break;
}
fclose(fData);
return size;
}
//Function to find residents of Tallinn and Tartu
int resCity (char fname[], struct data st[]) {
FILE *fopen(), *fData;
int current=0;
int current2=0;
if((fData=fopen("F2.txt", "w")) == NULL){
printf("Error rewriting file F2.txt");
exit(1);
}
//Residents of Tallinn;
printf("\n\nResidents of Tallinn:\n");
fprintf(fData, "Residents of Tallinn:\n");
while (current <= N) {
if (strcmp(st[current].stads.city, Sort1) == 0) {
printf("--%s\n", st[current].name);
fprintf(fData, "--%s\n", st[current].name);
}
current++;
}
//Residents of Tartu
printf("\n\nResidents of Tartu:\n");
fprintf(fData, "\n\nResidents of Tartu:\n");
while (current2 <= N) {
if (strcmp(st[current2].stads.city, Sort2) == 0) {
printf("--%s\n", st[current2].name);
fprintf(fData, "--%s\n", st[current2].name);
}
current2++;
}
fclose(fData);
return 0;
}
//Function to display and write to F2.txt residents of Tallinn and Tartu
int Street (char fname[], struct data st[]) {
FILE *fopen(), *fData;
char street[21];
int current3=0;
int i=0;
if((fData=fopen("F3.txt", "w")) == NULL){
printf("Error rewriting file F3.txt");
exit(1);
}
printf("\n\n");
while(i<=0){
if((strcmp(street, "Jaama") != 0)){
printf("Enter street name (Enter 'Jaama'):");
scanf("%s", street);
if((strcmp(street, "Jaama") != 0)){
printf("This street address is not exist in our database! Please try again.\n");
}
}
if((strcmp(street, "Jaama") == 0)){
printf("\nResidents of Tartu in the same street:\n");
fprintf(fData, "Residents of Tartu in the same `Jaama` street:\n");
while (current3 <= N) {
if (strcmp(st[current3].stads.street, street) == 0) {
printf("--%s\n", st[current3].name);
fprintf(fData, "--%s\n", st[current3].name);
}
current3++;
}
i++;
}
}
fclose(fData);
return 0;
}
int main(void)
{
FILE *fData;
char fname[21];
struct data st[N];
int i;
int size=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);
}
resCity(fname, st);
Street(fname, st);
return 0;
}
Файл F1.txt
Цитата:
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
|
Вот эта часть должна быть разделена на 2 функции
Код:
//Residents of Tallinn;
printf("\n\nResidents of Tallinn:\n");
fprintf(fData, "Residents of Tallinn:\n");
while (current <= N) {
if (strcmp(st[current].stads.city, Sort1) == 0) {
printf("--%s\n", st[current].name);
fprintf(fData, "--%s\n", st[current].name);
}
current++;
}