Commit a8b661cb by trkall

teine prax

parent 21fc9dfc
asf Kala 12
fgs Loom 13
gsa Lind 13
sal Putukas 83
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "functions.c"
typedef struct // https://www.tutorialspoint.com/cprogramming/c_structures.htm
{
char Nimi[50];
char Liik[50];
int Vanus;
}Ryhm;
/*void faili_lugemine(int pikkus, char *str, FILE *F2, Ryhm *n)
{
F2 = fopen("F2.txt", "a");
int i;
int compare;
for(i=0;i<pikkus;i++)
{
compare = strcmp((n+i)->Liik, str);
if(compare == 0)
{
fprintf(F2, "%s %s %d\n", (n+i)->Nimi, (n+i)->Liik, (n+i)->Vanus);
}
}
fclose(F2);
}*/
//chkfile
int main(void)
{
FILE *F1;
FILE *F2;
int i=0;
int pikkus;
char *str1 = "Loom";
char *str2 = "Lind";
char *str3 = "Kala";
char *str4 = "Putukas";
Ryhm *n = (Ryhm*)malloc(sizeof(Ryhm)); // http://stackoverflow.com/questions/11280523/c-reading-from-file-into-structure
F1 = fopen("F1.txt", "r");
//n = (Ryhm*)malloc(sizeof(Ryhm));
while(!feof(F1))
{
n = (Ryhm*)realloc(n, 35);
fscanf(F1,"%s %s %d", (n+i)->Nimi, (n+i)->Liik, &(n+i)->Vanus);
printf("%s %s %d", (n+i)->Nimi, (n+i)->Liik, (n+i)->Vanus);
i++;
}
i=0;
printf("-%s-%s-%d-", (n+i)->Nimi, (n+i)->Liik, (n+i)->Vanus);
pikkus = i;
fclose(F1);
faili_lugemine(pikkus, str1, F2, n);
faili_lugemine(pikkus, str2, F2, n);
faili_lugemine(pikkus, str3, F2, n);
faili_lugemine(pikkus, str4, F2, n);
return 0;
}
// http://stackoverflow.com/questions/16997141/writing-structs-to-a-file-in-c
Karl 22.5 00
Teet 34.5 01
Siim 125.7 01
Kertu -325.6 01
Kai 174 01
Timo 444 00
Lucifer 666 11
Allah 555.9 25
Kalle 123453.53 01
Mai 55.23 41
Tiina 154 55
Urve 1035 44
Peet 22.5 00
Teet 34.5 01
Erki 125.7 01
Nool -325.6 01
Vai 174 01
Tigo 444 00
Stiffler 0.01 11
Allah 555.9 25
Mai 55.23 -41
Tiina 154 55
Urve 1055535 44
Sepo -52.63 01
Kaspar 23.52 01
Karl 22.5 00
Teet 34.5 01
Siim 125.7 01
Kertu -325.6 01
Kai 174 01
Timo 444 00
Lucifer 666 11
Allah 555.9 25
Kalle 123453.53 01
Mai 55.23 41
Tiina 154 55
Urve 1035 44
Peet 22.5 00
Teet 34.5 01
Erki 125.7 01
Nool -325.6 01
Vai 174 01
Tigo 444 00
Stiffler 0.01 11
Allah 555.9 25
Mai 55.23 -41
Tiina 154 55
Urve 1055535 44
Sepo -52.63 01
Kaspar 23.52 01
void faili_lugemine(int pikkus, char *str, FILE *F2, Ryhm *n)
{
F2 = fopen("F2.txt", "a");
int i;
int compare;
for(i=0;i<pikkus;i++)
{
compare = strcmp((n+i)->Liik, str);
if(compare == 0)
{
fprintf(F2, "%s %s %d\n", (n+i)->Nimi, (n+i)->Liik, (n+i)->Vanus);
}
}
fclose(F2);
}
int chkFile(FILE *andmed)
{
if(andmed == NULL)
{
return 0;
}
return 1;
}
#include <stdio.h>
#include <string.h>
//#include <stdlib.h>
//Andmefaili lugemis yl
char *CHKCODE = "01";
float CHKMONEY = 50.0;
int ROW = 25;
typedef struct{
char nimi[30];
float money;
char code[3];
}Inimesed;
int chkFile(FILE *andmed);
void sisse(FILE *andmed, Inimesed *Isik);
void v2lja(Inimesed *Uus, int);
int t66tlus(Inimesed *Isik, Inimesed *Uus);
int main(void)
{
printf("TEST");
Inimesed *Isik;
Inimesed *Uus;
FILE *andmed;
andmed = fopen("andmed.txt", "r");
int uuspikkus;
if (chkFile(andmed) == 0)
{
printf("ERROR");
return 0;
}
sisse(andmed, Isik);
fclose(andmed);
uuspikkus = t66tlus(Isik, Uus);
v2lja(Uus, uuspikkus);
return 0;
}
int chkFile(FILE *andmed)
{
if(andmed == NULL)
{
return 0;
}
return 1;
}
void sisse(FILE *andmed, Inimesed *Isik)
{
int i = 0;
//Isik = (Inimesed*)malloc(sizeof(Inimesed));
while(!feof(andmed))
{
//Isik = (Inimesed*)realloc(Isik, 500);
fscanf(andmed, "%s %lf %s\n\n", (Isik+i)->nimi, &(Isik+i)->money, (Isik+i)->code);/// VIGA
i++;
}
}
int t66tlus(Inimesed *Isik, Inimesed *Uus)
{
int i;
int j=0;
for(i=0;i<ROW;i++)
{
if((Isik+i)->money > CHKMONEY && strcmp((Isik+i)->code, CHKCODE)== 0)
{
strcpy((Uus+j)->nimi, (Isik+i)->nimi);
(Uus+j)->money = (Isik+i)->money;
j++;
}
}
return j-1;
}
void v2lja(Inimesed *Uus, int pikkus)
{
FILE *tulemused;
int i;
tulemused = fopen("tulemused.txt", "w");
for(i=0;i<pikkus;i++)
{
fprintf(tulemused, "%s %lf %s\n", (Uus+i)->nimi, (Uus+i)->money, CHKCODE);
}
fclose(tulemused);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment