Untitled
unknown
plain_text
7 months ago
2.5 kB
3
Indexable
Never
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_STUDENTS 100 int main() { char students[MAX_STUDENTS][100]; int n; printf("Introduceti numarul de studenti: "); scanf("%d", &n); printf("Introduceti numele studentilor:\n"); for (int i = 0; i < n; i++) { scanf("%s", students[i]); } FILE *f = fopen("STUDENTI.TXT", "w"); if (f == NULL) { printf("Eroare la deschiderea fisierului!\n"); exit(1); } for (int i = 0; i < n; i++) { fprintf(f, "%s\n", students[i]); } fclose(f); printf("\nContinutul fisierului STUDENTI.TXT este:\n"); f = fopen("STUDENTI.TXT", "r"); if (f == NULL) { printf("Eroare la deschiderea fisierului!\n"); exit(1); } char line[100]; while (fgets(line, 100, f)) { printf("%s", line); } fclose(f); return 0; } #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_STUDENTS 100 int main() { char students[MAX_STUDENTS][100]; int n; FILE *f = fopen("STUDENTI.TXT", "a"); if (f == NULL) { printf("Eroare la deschiderea fisierului!\n"); exit(1); } printf("Introduceti numarul de studenti de adaugat: "); scanf("%d", &n); printf("Introduceti numele studentilor de adaugat:\n"); for (int i = 0; i < n; i++) { scanf("%s", students[i]); fprintf(f, "%s\n", students[i]); } fclose(f); // Sorteaza studentii in ordine alfabetica int count = 0; char line[100]; char sorted_students[MAX_STUDENTS][100]; f = fopen("STUDENTI.TXT", "r"); while (fgets(line, 100, f)) { strcpy(sorted_students[count], line); count++; } fclose(f); for (int i = 0; i < count - 1; i++) { for (int j = i + 1; j < count; j++) { if (strcmp(sorted_students[i], sorted_students[j]) > 0) { char temp[100]; strcpy(temp, sorted_students[i]); strcpy(sorted_students[i], sorted_students[j]); strcpy(sorted_students[j], temp); } } } // Scrie studentii sortati in alt fisier f = fopen("STUDENTI_SORTATI.TXT", "w"); for (int i = 0; i < count; i++) { fprintf(f, "%s", sorted_students[i]); } fclose(f); return 0; }