Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //PRVI
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX_STUDENTS 100
- void write_students_to_file(char* filename, char** names, char** surnames, int count) {
- FILE* file = fopen(filename, "w");
- if (file == NULL) {
- printf("Failed to open file %s for writing.\n", filename);
- return;
- }
- for (int i = 0; i < count; i++) {
- fprintf(file, "Student broj: %d.\tIme: %s\t Prezime: %s\n", i + 1, names[i], surnames[i]);
- }
- fclose(file);
- }
- int main() {
- char* filename = "dat.txt";
- FILE* file = fopen(filename, "r");
- if (file == NULL) {
- printf("Failed to open file %s for reading.\n", filename);
- return 1;
- }
- int num_students;
- if (fscanf(file, "%d", &num_students) != 1) {
- printf("Failed to read number of students from file %s.\n", filename);
- fclose(file);
- return 1;
- }
- fclose(file);
- if (num_students < 1 || num_students > MAX_STUDENTS) {
- printf("Invalid number of students: %d (must be between 1 and %d).\n", num_students, MAX_STUDENTS);
- return 1;
- }
- char* names[MAX_STUDENTS];
- char* surnames[MAX_STUDENTS];
- for (int i = 0; i < num_students; i++) {
- char name[100], surname[100];
- printf("Unesite ime i prezime za studenta broj %d:\n", i + 1);
- scanf("%s %s", name, surname);
- names[i] = malloc(strlen(name) + 1);
- surnames[i] = malloc(strlen(surname) + 1);
- if (names[i] == NULL || surnames[i] == NULL) {
- printf("Failed to allocate memory for student names.\n");
- return 1;
- }
- strcpy(names[i], name);
- strcpy(surnames[i], surname);
- }
- write_students_to_file("studenti.txt", names, surnames, num_students);
- for (int i = 0; i < num_students; i++) {
- free(names[i]);
- free(surnames[i]);
- }
- return 0;
- }
- //DRUGI
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- // Funkcija za ispis matrice
- void print_matrix(double **mat, int rows, int cols) {
- printf("Matrica:\n");
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- printf("%10.2f", mat[i][j]);
- }
- printf("\n");
- }
- }
- int main() {
- // Ucitavanje brojeva iz datoteke
- FILE *in_file = fopen("C:\\path\\to\\in.txt", "r");
- int n, m;
- fscanf(in_file, "%d %d", &n, &m);
- fclose(in_file);
- // Alociranje memorije za matricu
- double **mat = (double **) malloc(n * sizeof(double *));
- for (int i = 0; i < n; i++) {
- mat[i] = (double *) malloc(m * sizeof(double));
- }
- // Popunjavanje matrice pseudo-slucajnim brojevima
- srand(time(NULL)); // Postavljanje sjemena za generator pseudo-slucajnih brojeva
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- mat[i][j] = -125.5 + (double) rand() / RAND_MAX * (65.8 - (-125.5));
- }
- }
- // Ispisivanje matrice
- print_matrix(mat, n, m);
- // Alociranje memorije za izlaznu matricu
- double **out_mat = (double **) malloc((n-1) * sizeof(double *));
- for (int i = 0; i < n-1; i++) {
- out_mat[i] = (double *) malloc((m-1) * sizeof(double));
- }
- // Popunjavanje izlazne matrice
- for (int i = 1; i < n; i++) {
- for (int j = 0; j < m-1; j++) {
- out_mat[i-1][j] = mat[i][j+1];
- }
- }
- // Ispisivanje izlazne matrice
- print_matrix(out_mat, n-1, m-1);
- // Zatvaranje izlazne datoteke
- fclose(out_file);
- // Oslobadanje memorije
- for (int i = 0; i < n; i++) {
- free(mat[i]);
- }
- free(mat);
- for (int i = 0; i < n-1; i++) {
- free(out_mat[i]);
- }
- free(out_mat);
- return 0;
- }
- //TRECI
- #include <stdio.h>
- #include <ctype.h>
- void copyFile(FILE* source, FILE* destination) {
- char ch;
- while ((ch = fgetc(source)) != EOF) {
- fputc(toupper(ch), destination);
- }
- }
- int main() {
- FILE* sourceFile = fopen("C:/putanja/do/prva.txt", "r");
- FILE* destinationFile = fopen("druga.txt", "w");
- if (sourceFile == NULL || destinationFile == NULL) {
- printf("Nemoguće otvoriti datoteke!\n");
- return 1;
- }
- copyFile(sourceFile, destinationFile);
- printf("Kopiranje završeno!\n");
- fclose(sourceFile);
- fclose(destinationFile);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement