Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib.h>
- #include<stdio.h>
- #include<string.h>
- //error in the end and sapce
- #define MAX 256
- typedef struct{
- char code[10];
- char *name;//dinamic allocation
- }Book;
- typedef struct{
- char *name;
- int num_books;
- Book *array;//dinamic
- }Library;
- void Get_Lost(char* str);
- void input_book(Book* B, FILE *in);
- void input_library(Library *L, FILE *in);
- void output_book(Book* B, FILE *out);
- void output_library(Library* L, FILE *out);
- int main()
- {
- FILE *in, *out;
- Library Libr;
- int i;
- in = fopen("input.txt", "r");
- input_library(&Libr, in);
- fclose(in);
- out = fopen("output.txt", "w");
- output_library(&Libr, out);
- fclose(out);
- for (i = 0; i < Libr.num_books; i++)//why?
- free(&Libr.array[i]);
- return 0;
- }
- //free
- void Get_Lost(char* str)
- {
- printf("\n%s", str);
- exit(1);
- }
- void input_book(Book* B, FILE *in)
- {
- int i;
- char temp[MAX];
- fscanf(in, "%s",&B->code);
- fscanf(in, "%s", temp);
- B->name = (char*)malloc(strlen(temp)*sizeof(char));
- if (B->name == NULL)
- {
- printf("Allocation failed");
- exit(1);//check
- }
- strcpy(B->name, temp);
- }
- void input_library(Library *L, FILE *in)
- {
- int i;
- char temp[MAX];
- fscanf(in, "%s", temp);
- L->name = (char*)malloc(strlen(temp)*sizeof(char));
- if (L->name == NULL)
- {
- printf("Allocation failed");
- exit(1);//check
- }
- strcpy(L->name, temp);
- fscanf(in, "%d", &L->num_books);
- L->array = (Library*)malloc(L->num_books*sizeof(Library));
- if (L->array == NULL)
- {
- printf("Allocation failed");
- exit(1);//check
- }
- //fill book struct array
- for (i = 0; i < L->num_books; i++)
- input_book(&L->array[i], in);//WHY?
- }
- void output_book(Book* B, FILE *out)
- {
- fprintf(out, "\n%s %s",B->code, B->name);//HOW TO SPACE LIKE THAT?
- }
- void output_library(Library* L, FILE *out)
- {
- int i;
- fprintf(out, "%s, %d",L->name, L->num_books);
- for (i = 0; i < L->num_books; i++)
- output_book(&L->array[i], out);
- }
- /*
- FILE *p;
- p=fopen("out", "w");
- fclose(p);*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement