Advertisement
Guest User

4.1

a guest
Apr 26th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1.  
  2. #include<stdlib.h>
  3. #include<stdio.h>
  4. #include<string.h>
  5. //error in the end and sapce
  6. #define MAX 256
  7.  
  8.  
  9.  
  10. typedef struct{
  11. char code[10];
  12. char *name;//dinamic allocation
  13.  
  14. }Book;
  15.  
  16. typedef struct{
  17. char *name;
  18. int num_books;
  19. Book *array;//dinamic
  20. }Library;
  21.  
  22.  
  23. void Get_Lost(char* str);
  24. void input_book(Book* B, FILE *in);
  25. void input_library(Library *L, FILE *in);
  26. void output_book(Book* B, FILE *out);
  27. void output_library(Library* L, FILE *out);
  28.  
  29. int main()
  30. {
  31. FILE *in, *out;
  32. Library Libr;
  33. int i;
  34.  
  35. in = fopen("input.txt", "r");
  36. input_library(&Libr, in);
  37. fclose(in);
  38.  
  39. out = fopen("output.txt", "w");
  40. output_library(&Libr, out);
  41. fclose(out);
  42.  
  43. for (i = 0; i < Libr.num_books; i++)//why?
  44. free(&Libr.array[i]);
  45. return 0;
  46. }
  47. //free
  48.  
  49. void Get_Lost(char* str)
  50. {
  51. printf("\n%s", str);
  52. exit(1);
  53. }
  54.  
  55. void input_book(Book* B, FILE *in)
  56. {
  57. int i;
  58. char temp[MAX];
  59. fscanf(in, "%s",&B->code);
  60. fscanf(in, "%s", temp);
  61. B->name = (char*)malloc(strlen(temp)*sizeof(char));
  62. if (B->name == NULL)
  63. {
  64. printf("Allocation failed");
  65. exit(1);//check
  66. }
  67. strcpy(B->name, temp);
  68.  
  69. }
  70. void input_library(Library *L, FILE *in)
  71. {
  72. int i;
  73. char temp[MAX];
  74. fscanf(in, "%s", temp);
  75. L->name = (char*)malloc(strlen(temp)*sizeof(char));
  76. if (L->name == NULL)
  77. {
  78. printf("Allocation failed");
  79. exit(1);//check
  80. }
  81. strcpy(L->name, temp);
  82. fscanf(in, "%d", &L->num_books);
  83. L->array = (Library*)malloc(L->num_books*sizeof(Library));
  84. if (L->array == NULL)
  85. {
  86. printf("Allocation failed");
  87. exit(1);//check
  88. }
  89. //fill book struct array
  90. for (i = 0; i < L->num_books; i++)
  91. input_book(&L->array[i], in);//WHY?
  92. }
  93. void output_book(Book* B, FILE *out)
  94. {
  95.  
  96.  
  97. fprintf(out, "\n%s %s",B->code, B->name);//HOW TO SPACE LIKE THAT?
  98.  
  99. }
  100. void output_library(Library* L, FILE *out)
  101. {
  102. int i;
  103. fprintf(out, "%s, %d",L->name, L->num_books);
  104. for (i = 0; i < L->num_books; i++)
  105. output_book(&L->array[i], out);
  106.  
  107. }
  108. /*
  109. FILE *p;
  110. p=fopen("out", "w");
  111.  
  112. fclose(p);*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement