Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "libro.h"
- #define BUFFSIZE 100
- FILE *Fopen(const char *path, const char *mode);
- int countFileLine(FILE *fp);
- void printLista(libro* biblioteca, int n);
- int main(int argc, char *argv[]){
- FILE *fp;
- libro *biblioteca;
- int n = 0;
- int i = 0;
- char tit[100] = "";
- char ed[50] = "";
- char buffer[100];
- float pr;
- int an;
- if (argc < 1){
- printf("Inserisci il nome del file di input");
- exit(1);
- }
- fp = Fopen(argv[1], "r");
- n = countFileLine(fp)/4; //numero libri, ogni 4 linee 1
- fclose(fp);
- biblioteca = malloc(sizeof(libro) * (n+1));
- fp = Fopen(argv[1], "r");
- while (!feof(fp) && !ferror(fp))
- {
- fgets(tit, sizeof(tit), fp);
- tit[strlen(tit)-1] = '\0';
- fgets(ed, sizeof(ed), fp);
- ed[strlen(ed) - 1] = '\0';
- fgets(buffer, sizeof(buffer), fp);
- an = atoi(buffer);
- buffer[0] = '\0';
- fgets(buffer, sizeof(buffer), fp);
- pr = atof(buffer);
- buffer[0] = '\0';
- biblioteca[i] = creaLibro(tit,ed,pr,an);
- i++;
- }
- fclose(fp);
- printLista(biblioteca,n);
- }
- int countFileLine(FILE *fp)
- {
- int lines = 0;
- char ch;
- while (!feof(fp) && !ferror(fp))
- {
- ch = fgetc(fp);
- if (ch == '\n')
- {
- lines++;
- }
- }
- return lines;
- }
- FILE *Fopen(const char *path, const char *mode)
- {
- FILE *f = fopen(path, mode);
- if (f == NULL)
- {
- perror(path);
- printf("Impossibile aprire il file\n");
- exit(EXIT_FAILURE);
- }
- return f;
- }
- void printLista(libro* biblioteca, int n){
- printf("Lista libri:");
- for (int i = 0; i < n; ++i) {
- printf("\nTitolo: %s\nEditore: %s\nPrezzo: %f\nAnno: %d\n\n", titolo(biblioteca[i]),editore(biblioteca[i]),prezzo(biblioteca[i]),anno(biblioteca[i]));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement