Advertisement
EduzZiTo

Untitled

Feb 17th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef struct fotos{
  5. int numft;
  6. char local[50];
  7. char maquina[50];
  8. int ano;
  9. char tipo[50];
  10. }estrutura;
  11. estrutura fotos;
  12. FILE *fx;
  13. FILE *fa;
  14. FILE *fn;
  15.  
  16.  
  17. int pesquisan(int num){
  18. fa=fopen("fotos.dat","r");
  19. fread(&fotos,sizeof(fotos),1,fa);
  20. while (!feof(fa) && fotos.numft!=num)
  21. fread(&fotos,sizeof(fotos),1,fa);
  22. int p=ftell(fa);
  23. fclose(fa);
  24. if(fotos.numft==num)
  25. return p;
  26. else
  27. return 0;
  28.  
  29. }
  30.  
  31.  
  32. void insere(){
  33. int c,n;
  34. fx=fopen("fotos.dat","a+");
  35.  
  36.  
  37.  
  38. do{
  39. scanf("%d",&n);
  40.  
  41. if(pesquisan(n)==0)
  42. {
  43. fx=fopen("fotos.dat","a+");
  44. fotos.numft=n;
  45. scanf(" %s",fotos.local);
  46. scanf(" %s",fotos.maquina);
  47. scanf("%d",&fotos.ano);
  48. scanf(" %s",fotos.tipo);
  49. fwrite(&fotos,sizeof(fotos),1,fx);
  50. fclose(fx);
  51. }
  52. else
  53. printf("Ja existe!\n");
  54. printf("Continuar?\n");
  55. scanf("%d",&c);
  56. }while (c==1);
  57.  
  58. fclose(fx);
  59. }
  60. void lista(){
  61. int i,s;
  62. scanf("%d",&i);
  63. scanf("%d",&s);
  64. fx=fopen("fotos.dat","r");
  65. fread(&fotos,sizeof(fotos),1,fx);
  66. while(!feof(fx)){
  67. if(fotos.ano>=i && fotos.ano <= s){
  68. printf("Numero da Foto:%d\nLocal:%s\nMaquina:%s\nAno:%d\nTipo:%s\n\n",fotos.numft,fotos.local,fotos.maquina,fotos.ano,fotos.tipo);
  69. }
  70. fread(&fotos,sizeof(fotos),1,fx);
  71. }
  72. fclose(fx);
  73. }
  74.  
  75. /*void altera(){
  76. char nfx[50];
  77. char nfx2[50];
  78. scanf("%s",nfx2);
  79. scanf("%s",nfx);
  80. if(fopen(nfx,"r")==NULL){
  81. fa=fopen(nfx,"w");
  82. fclose(fa);
  83. }
  84. if(fopen(nfx2,"r")==NULL){
  85. fn=fopen(nfx2,"w");
  86. fclose(fn);
  87. }
  88.  
  89. fn=fopen(nfx2,"r");
  90. fa=fopen(nfx,"a+");
  91. fread(&fotos,sizeof(fotos),1,fn);
  92. while(!feof(fn)){
  93. fwrite(&fotos,sizeof(fotos),1,fa);
  94. fread(&fotos,sizeof(fotos),1,fn);
  95. }
  96. fclose(fx);
  97. fclose(fa);
  98. }*/
  99. void altera(){
  100. char local[30];
  101. char nlocal[30];
  102. scanf(" %s",local);
  103. scanf(" %s",nlocal);
  104. fx=fopen("fotos.dat","r+");
  105. fread(&fotos,sizeof(fotos),1,fx);
  106. while(!feof(fx)){
  107. if(strcmp(fotos.local,local)==0){
  108. strcpy(fotos.local,nlocal);
  109. fseek(fx,-sizeof(fotos),1);
  110. fwrite(&fotos,sizeof(fotos),1,fx);
  111. fseek(fx,sizeof(fotos),1);
  112. }
  113. fread(&fotos,sizeof(fotos),1,fx);
  114. }
  115. fclose(fx);
  116.  
  117. }
  118.  
  119. int main() {
  120. int(op);
  121. if(fopen("fotos.dat","r")==NULL){
  122. fx=fopen("fotos.dat","w");
  123. fclose(fx);
  124. }
  125. do {
  126. printf("1-Inserir fotos\n");
  127. printf("2-consulta entre 2 dados\n");
  128. printf("3-Altera de um dado local\n");
  129. printf("4-Sair\n");
  130. scanf("%d",&op);
  131. switch(op){
  132. case 1:insere();
  133. break;
  134. case 2:lista();
  135. break;
  136. case 3:altera();
  137. break;
  138. }
  139. }while(op!=4);
  140. system("pause");
  141. return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement