Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. /**********************************
  2. * UNIVERSIDADE DE SANTA CRUZ DO SUL
  3. * DEPARTAMENTO DE COMPUTA??O
  4. *
  5. * Programa??o Estruturada
  6. * Turno......: Noite
  7. * Data.......: 03/06/2018
  8. * Respons?vel: Profa. Daniela Bagatini e Prof. Ivan S?ptitz
  9. * Exerc?cio arquivo sequencial
  10. ***********************************/
  11.  
  12.  
  13.  
  14. /*-----------------------
  15. | Definicao das bibliotecas
  16. +----------------------*/
  17. #include <stdio.h >
  18. #include <stdlib.h>
  19. #include <conio.h >
  20.  
  21.  
  22.  
  23. /*-----------------------
  24. | Defini??o do registro
  25. +----------------------*/
  26. typedef struct INFORMACAO
  27. { int cod;
  28. char nome[20];
  29. char mail[30];
  30. int excluido; // controle de exclus?o l?gica
  31. }INFORMACAO;
  32.  
  33.  
  34.  
  35. /*-----------------------
  36. | Cabecalhos de funcoes/procedimentos
  37. +----------------------*/
  38. void inserir (FILE *arq);
  39. int consultar (FILE *arq, char nomearq[25]);
  40. void listar (FILE *arq, char nomearq[25]);
  41. void alterar (FILE *arq, char nomearq[25]);
  42. void excluir (FILE *arq, char nomearq[25]);
  43.  
  44.  
  45.  
  46. /*-----------------------
  47. | Programa principal
  48. +----------------------*/
  49. int main(void)
  50. {
  51. FILE *arq;
  52. char nomeArquivo[25], op; // op??o do menu
  53. //int posicao; // posi??o de um registro no arquivo
  54.  
  55. printf( "\n Informe o nome do arquivo: " );
  56. scanf( "%s", nomeArquivo );
  57.  
  58. arq= fopen( nomeArquivo, "rb+" ); // abre para leitura ou grava??o
  59. if( arq == NULL)
  60. {
  61. arq= fopen( nomeArquivo, "wb+" ); // cria para leitura ou grava??o
  62. if( arq == NULL)
  63. {
  64. printf( "\n O arquivo nao pode ser aberto! " );
  65. exit( 1 );
  66. }
  67. else
  68. printf( "\n Arquivo aberto com sucesso! " );
  69. }
  70.  
  71. do{
  72. printf("\n\n ---------------------------");
  73. printf("\n [ 1 ] inserir dados ");
  74. printf("\n [ 2 ] consultar dados ");
  75. printf("\n [ 3 ] excluir dados ");
  76. printf("\n [ 4 ] alterar dados ");
  77. printf("\n [ 5 ] listar todos os dados");
  78. printf("\n [ 6 ] encerrar ");
  79. printf("\n ---------------------------");
  80. op= getch( );
  81.  
  82. switch( op ){
  83. case '1': inserir( arq );
  84. break;
  85. case '2': consultar( arq, nomeArquivo );
  86. break;
  87. case '3': excluir( arq, nomeArquivo );
  88. break;
  89. case '4': alterar( arq, nomeArquivo );
  90. break;
  91. case '5': listar( arq, nomeArquivo );
  92. break;
  93. case '6': fclose( arq );
  94. break;
  95. default: printf("\n Opcao invalida, digite novamente! ");
  96. }
  97.  
  98. }while( op != '6' );
  99. return 0;
  100. }
  101.  
  102.  
  103.  
  104. /*-----------------------
  105. | Funcao inserir
  106. | Entrada: arquivo
  107. | Saidas : arquivo
  108. +----------------------*/
  109. void inserir( FILE *arq )
  110. {
  111. INFORMACAO reg;
  112. printf("\n Informe o codigo:");
  113. fflush(stdin);
  114. scanf("%i", &reg.cod);
  115. printf("\n Informe o nome:");
  116. fflush(stdin);
  117. gets(reg.nome);
  118. printf("\n Informe o email:");
  119. fflush(stdin);
  120. gets(reg.mail);
  121. reg.excluido=0;
  122.  
  123. fseek(arq, sizeof(reg), SEEK_END); //posicionando no final do arquivo (sequencial)
  124. fwrite(&reg, sizeof(struct INFORMACAO), 1, arq); // escrevendo no último slot
  125. }
  126.  
  127.  
  128.  
  129. /*-----------------------
  130. | Funcao consultar
  131. | Entrada: arquivo
  132. | Saidas : arquivo
  133. +----------------------*/
  134. int consultar( FILE *arq, char nomearq[25] )
  135. {
  136. INFORMACAO reg;
  137.  
  138. printf("\n Informe o codigo:");
  139. fflush(stdin);
  140. scanf("%i", &reg.cod);
  141. rewind(arq);
  142. fread(&reg, sizeof(struct INFORMACAO), 1, arq);
  143. printf( "\n Nome: %s", reg.nome);
  144. printf( "\n Email: %s", reg.mail);
  145. }
  146.  
  147.  
  148.  
  149. /*-----------------------
  150. | Funcao listar
  151. | Entrada: arquivo
  152. | Saidas : arquivo
  153. +----------------------*/
  154. void listar( FILE *arq, char nomearq[25] )
  155. {
  156. INFORMACAO reg;
  157. rewind(arq);
  158. fread(&reg, sizeof(struct INFORMACAO), 1 ,arq);
  159. printf("\n Codigo: %s", reg.cod);
  160. printf("\n Nome: %s", reg.nome);
  161. printf("\n Email: %s", reg.mail);
  162. }
  163.  
  164.  
  165.  
  166. /*-----------------------
  167. | Funcao alterar
  168. | Entrada: arquivo
  169. | Saidas : arquivo
  170. +----------------------*/
  171. void alterar( FILE *arq, char nomearq[25] )
  172. {
  173. INFORMACAO reg;
  174. int posicao=0;
  175.  
  176. printf("\n Nome:");
  177. fflush(stdin);
  178. gets(reg.nome);
  179. printf("\n Email:");
  180. fflush(stdin);
  181. gets(reg.mail);
  182.  
  183. fseek(arq, (posicao - 1)* sizeof(struct INFORMACAO), SEEK_SET);
  184. fwrite (&reg, sizeof(reg),1 ,arq);
  185. }
  186.  
  187.  
  188.  
  189. /*-----------------------
  190. | Funcao excluir
  191. | Entrada: arquivo
  192. | Saidas : arquivo
  193. +----------------------*/
  194. void excluir( FILE *arq, char nomearq[25] )
  195. {
  196.  
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement