funguje
This commit is contained in:
parent
975cc2cec4
commit
c262b5c146
@ -23,36 +23,56 @@ int compare(const void *p1, const void *p2) {
|
|||||||
return strcmp(s1->name, s2->name);
|
return strcmp(s1->name, s2->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
// Načíta študentov zo súboru do pola
|
||||||
// Otvoríme súbor na čítanie
|
int read_students_from_file(struct Student students[], int max_students) {
|
||||||
FILE *file = fopen("nazov_suboru.txt", "r");
|
FILE *file = fopen("nazov_suboru.txt", "r");
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
perror("Chyba pri otváraní súboru");
|
perror("Chyba pri otváraní súboru");
|
||||||
return 1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pole študentov
|
|
||||||
struct Student students[MAX_STUDENTS];
|
|
||||||
int num_students = 0;
|
int num_students = 0;
|
||||||
|
|
||||||
// Načítame každý riadok zo súboru
|
|
||||||
char line[MAX_SIZE];
|
char line[MAX_SIZE];
|
||||||
while (fgets(line, sizeof(line), file) != NULL) {
|
while (fgets(line, sizeof(line), file) != NULL) {
|
||||||
// Načítame počet hlasov a meno
|
|
||||||
int votes;
|
int votes;
|
||||||
char name[MAX_SIZE];
|
char name[MAX_SIZE];
|
||||||
if (sscanf(line, "%d %99[^\n]", &votes, name) == 2) {
|
if (sscanf(line, "%d %99[^\n]", &votes, name) == 2) {
|
||||||
// Uložíme študenta do pola študentov
|
|
||||||
strcpy(students[num_students].name, name);
|
strcpy(students[num_students].name, name);
|
||||||
students[num_students].votes = votes;
|
students[num_students].votes = votes;
|
||||||
num_students++;
|
num_students++;
|
||||||
} else {
|
if (num_students >= max_students) {
|
||||||
fprintf(stderr, "CHYBA: Neplatný zápis na riadku\n");
|
break; // Prekročený maximálny počet študentov
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Uzavrieme súbor
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
return num_students;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pridá nového študenta do súboru
|
||||||
|
void add_student_to_file(const struct Student *student) {
|
||||||
|
FILE *file = fopen("nazov_suboru.txt", "a");
|
||||||
|
if (file == NULL) {
|
||||||
|
perror("Chyba pri otváraní súboru");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
fprintf(file, "%d %s\n", student->votes, student->name);
|
||||||
|
fclose(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
struct Student students[MAX_STUDENTS];
|
||||||
|
int num_students = read_students_from_file(students, MAX_STUDENTS);
|
||||||
|
if (num_students == -1) {
|
||||||
|
return 1; // Chyba pri čítaní zo súboru
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pridáme nového študenta
|
||||||
|
struct Student new_student;
|
||||||
|
strcpy(new_student.name, "Novy Student");
|
||||||
|
new_student.votes = 5;
|
||||||
|
add_student_to_file(&new_student);
|
||||||
|
|
||||||
// Zoradíme študentov podľa počtu hlasov
|
// Zoradíme študentov podľa počtu hlasov
|
||||||
qsort(students, num_students, sizeof(struct Student), compare);
|
qsort(students, num_students, sizeof(struct Student), compare);
|
||||||
|
Loading…
Reference in New Issue
Block a user