I am practicing with C Functions, malloc, realloc and files. The program is still at the beginning but when complete it should perform different tasks decided by the user: the main function asks the user for a number that connects it to different functions of the program.
My problem is with the function "CaricaFile()" that should let the user enter a file name, load this file that has different temperatures and times recorded, and load these data in a dynamic array; every time the function starts, it won't let me insert any file name and the program continues with an "Invalid argument" error.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define NOME_FILE_MAX 25
typedef struct {
short int ore;
short int minuti;
float temperatura;
}t_rilevazione;
t_rilevazione *rilevazioni;
int i = -1;
int dimrilevazione = sizeof(t_rilevazione);
int CaricaFile() {
char nome_file[NOME_FILE_MAX];
printf("Inserire il nome del file da caricare, estensione compresa: ");
fgets(nome_file, NOME_FILE_MAX, stdin);
for (int q = 0; nome_file[q] != '\0'; q++) {
if (nome_file[q] == '\n') nome_file[q] = '\0';
}
FILE *input_stream = fopen(nome_file, "r");
if (input_stream == NULL) {
perror("Impossibile trovare il file");
return 0;
}
else {
rilevazioni = malloc(dimrilevazione);
if (rilevazioni = NULL) {
perror("Problema con l'allocazione della memoria");
return 0;
}
i = 0;
while (feof(input_stream) != 1) {
fscanf(input_stream, "%d:%d\n", &rilevazioni[i].ore, &rilevazioni[i].minuti);
fscanf(input_stream, "%f", &rilevazioni[i].temperatura);
realloc(rilevazioni, (i + 1 * dimrilevazione));
if (rilevazioni == NULL) {
perror("Problema con la riallocazione della memoria");
return 0;
}
i++;
}
fclose(input_stream);
printf("File caricato con successo.\n");
return 1;
}
}
int StampaVettore() {
if (i >= 0) {
int p;
printf("I parametri registrati sono...\n");
for (p = 0; p != i; p++) {
printf("%d:%d\n%f\n", rilevazioni[p].ore, rilevazioni[p].minuti, rilevazioni[p].temperatura);
}
return 1;
}
else {
printf("Nessun valore registrato.\n");
return 0;
}
}
int OrdinaVettore() {
}
int CalcolaMedia() {
}
int RicercaOrario() {
}
int SalvataggioFile() {
}
int main(void) {
short int op;
do {
printf("Di seguito le operazioni che e' possibile effetuare con il programma:\n");
printf("[1] Carica un file di testo\n");
printf("[2] Stampa i record acquisiti dal file\n");
printf("[3] Ordina il vettore dei record del file\n");
printf("[4] Calcola la media delle temperature\n");
printf("[5] Ricerca un determinato orario\n");
printf("[6] Salva su file i record del programma\n");
printf("[0] Esci dal programma\n");
printf("Digitare il numero corrispondente all'operazione desiderata.\n");
do {
scanf("%d", &op);
if (op < 0 || op > 6) {
printf("Operazione non consentita. Riprovare\n");
}
} while (op < 0 || op > 6);
if (op == 1) CaricaFile();
if (op == 2) StampaVettore();
if (op == 3) OrdinaVettore();
if (op == 4) CalcolaMedia();
if (op == 5) RicercaOrario();
if (op == 6) SalvataggioFile();
if (op == 0) printf("Uscita in corso...");
} while (op != 0);
return 0;
}
Strange thing is that if I take the CaricaFile function alone bringing it to its own program as a Main function, it works without problems (at least, there is some problem but that's because I still can't understand well how to use dynamic arrays and so on)
What could I do? Thank you