fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5.  
  6. #define CLEAR_INPUT while(getchar() != '\n') /*void*/
  7. #define BUFFER 15
  8.  
  9. typedef struct dadosMoeda{
  10. int NiM;
  11. char Metal[BUFFER];
  12. float Valor;
  13. int Stock;
  14. }imoeda;
  15.  
  16. //Abrir stream para um ficheiro
  17. FILE* fileOpen(FILE* fp, char* fileName){
  18. if((fp = fopen(fileName, "a+")) == NULL){
  19. printf("Erro ao abrir ficheiro!\n");
  20. exit(-1);
  21. }
  22. return fp;
  23. }
  24.  
  25. //Mostrar info de uma moeda
  26. void MostraMoeda(imoeda moeda){
  27. printf("NiM da moeda: %d\n", moeda.NiM);
  28. printf("Metal da moeda: %s\n", moeda.Metal);
  29. printf("Valor da moeda: %.2f\n", moeda.Valor);
  30. printf("Stock da moeda: %d\n", moeda.Stock);
  31. }
  32.  
  33. //Mostrar menu do programa
  34. void mostraMenu(void){
  35. printf("Menu Principal\n");
  36. printf("\tInserir Moeda\n");
  37. printf("\tAtualizar Stock\n");
  38. printf("\tSair\n");
  39. }
  40.  
  41. //Actualiza stock de uma moeda
  42. int ActualizaStock(int nim, imoeda** moeda){
  43. int i = 0;
  44. while(moeda[i] != NULL){
  45. if (moeda[i]->NiM == nim)
  46. if((--(moeda[i]->Stock)) == 0){
  47. moeda[i]->Stock = 0;
  48. printf("Ruptura de Stock!\n");
  49. return 0;
  50. }
  51. }
  52. return moeda[i]->Stock;
  53. }
  54.  
  55. //Contar registos presentes no ficheiro
  56. int ContaRegistos(FILE* fp){
  57. imoeda tmpMoeda;
  58. int regCounter = 0;
  59. fseek(fp, 0, SEEK_SET);
  60. while(fread(&tmpMoeda, sizeof(imoeda), 1, fp)){
  61. regCounter++;
  62. }
  63. return regCounter;
  64. }
  65.  
  66. //Alocar memória para dados de moedas
  67. imoeda* AlocaMemoria(int RegCount){
  68. imoeda* tmpmoeda = NULL;
  69. if((tmpmoeda = malloc(sizeof(imoeda) * RegCount)) == NULL){
  70. printf("Erro de memória!\n");
  71. exit(-2);
  72. }
  73. return tmpmoeda;
  74. }
  75.  
  76. //Pede dados para moeda
  77. imoeda PedeInfoMoeda(){
  78. imoeda moeda = {0};
  79. char op;
  80. int check = 0;
  81.  
  82. printf("Isnsira o NiM:\n");
  83. while(scanf("%d", &moeda.NiM) != 1 || moeda.NiM < 0){
  84. printf("NiM inválido! Tente de novo:\n");
  85. }
  86. CLEAR_INPUT;
  87. printf("Insira o metal: [o]uro, [p]rata, [c]obre, o[u]tros\n");
  88. while(scanf("%c", &op) != 1 || !check)
  89. switch (tolower(op)) {
  90. case 'o': strcpy(moeda.Metal, "Ouro");
  91. check = 1;
  92. break;
  93. case 'p': strcpy(moeda.Metal, "Prata");
  94. check = 1;
  95. break;
  96. case 'c': strcpy(moeda.Metal, "Cobre");
  97. check = 1;
  98. break;
  99. case 'u': strcpy(moeda.Metal, "Outros");
  100. check = 1;
  101. break;
  102. default: printf("Metal inválido! Tente de novo:\n");
  103. check = 0;
  104. CLEAR_INPUT;
  105. }
  106. printf("Insira o valor:\n");
  107. while(scanf("%f", &moeda.Valor) != 1 || moeda.Valor < 0.0)
  108. printf("Valor inválido! Tente de novo:\n");
  109. printf("Insira o stock:\n");
  110. while(scanf("%d", &moeda.Stock) != 1 || moeda.Stock < 0)
  111. printf("Stock inválido! Tente de novo:\n");
  112.  
  113. return moeda;
  114. }
  115.  
  116. int main(int argc, char** argv){
  117. imoeda *tmpbasedados = NULL;
  118. FILE* fpointer = NULL;
  119. int RegFileCount = 0, i = 0;
  120.  
  121. fpointer = fileOpen(fpointer, "moedas.dat");
  122. RegFileCount = ContaRegistos(fpointer);
  123. tmpbasedados = AlocaMemoria(RegFileCount);
  124. printf("imoeda size: %zu\n", sizeof(imoeda));
  125. printf("tmpbasedados size: %zu\n", sizeof(tmpbasedados));
  126. printf("tmpbasedados size 1: %zu\n", sizeof(imoeda) * RegCount);
  127. for(i = 0; i < RegFileCount; i++){
  128. fread(&tmpbasedados[i], sizeof(imoeda), 1, fpointer);
  129. }
  130. // for(i = 0; i < RegFileCount; i++){
  131. // MostraMoeda(tmpbasedados[i]);
  132. // }
  133. // tmpbasedados = PedeInfoMoeda();
  134. // fwrite(&tmpbasedados, sizeof(imoeda), 1, fpointer);
  135. // tmpbasedados = PedeInfoMoeda();
  136. // fwrite(&tmpbasedados, sizeof(imoeda), 1, fpointer);
  137. // fseek(fpointer, 0, SEEK_SET);
  138. // fread(&tmpbasedados, sizeof(imoeda), 1, fpointer);
  139. // MostraMoeda(tmpbasedados);
  140. // fread(&tmpbasedados, sizeof(imoeda), 1, fpointer);
  141. // MostraMoeda(tmpbasedados);
  142. free(tmpbasedados);
  143. fclose(fpointer);
  144. return 0;
  145. }
  146.  
  147.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function 'main':
prog.c:126:59: error: 'RegCount' undeclared (first use in this function)
     printf("tmpbasedados size 1: %zu\n", sizeof(imoeda) * RegCount);
                                                           ^
prog.c:126:59: note: each undeclared identifier is reported only once for each function it appears in
stdout
Standard output is empty