Advertisement
Jvsierra

Ex 19/02

Feb 19th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5.  
  6. #define LIM 101
  7.  
  8. void LeArquivo(char NomeArq[LIM])
  9. {
  10. FILE *PtrTxt;
  11. char L;
  12.  
  13. system("cls");
  14.  
  15. printf("Leitura de arquivo\n\n");
  16.  
  17. PtrTxt = fopen(NomeArq, "r");
  18.  
  19. if(PtrTxt == NULL)
  20. printf("Arquivo %s inexistente\n\n", NomeArq);
  21. else
  22. {
  23. printf("---------------------------------------\n");
  24.  
  25. L = fgetc(PtrTxt);
  26.  
  27. while(!feof(PtrTxt))
  28. {
  29. printf("%c", L);
  30.  
  31. L = fgetc(PtrTxt);
  32. }
  33.  
  34. printf("\n---------------------------------------\n\n");
  35.  
  36. printf("Leitura concluida\n\n");
  37.  
  38. fclose(PtrTxt);
  39. }
  40.  
  41. getch();
  42. }
  43.  
  44. int ComparaArquivos(char NomeArq1[LIM], char NomeArq2[LIM])
  45. {
  46. int ret;
  47. char L1, L2;
  48. FILE *PtrTxt;
  49.  
  50. PtrTxt = fopen(NomeArq1, "r");
  51.  
  52. if(PtrTxt == NULL)
  53. ret = -1;
  54. else
  55. {
  56. FILE *PtrAux = fopen(NomeArq2, "r");
  57.  
  58. if(PtrAux == NULL)
  59. ret = -1;
  60. else
  61. {
  62. L1 = fgetc(PtrTxt);
  63. L2 = fgetc(PtrAux);
  64.  
  65. while(!feof(PtrTxt) && L1 == L2)
  66. {
  67. L1 = fgetc(PtrTxt);
  68. L2 = fgetc(PtrAux);
  69. }
  70.  
  71. if(L1 == L2)
  72. ret = 1;
  73. else
  74. ret = 0;
  75. }
  76. }
  77.  
  78. return ret;
  79. }
  80.  
  81. void ArquivoMaiusculas(char NomeArq[LIM])
  82. {
  83. char Letra;
  84. FILE *PtrTxt;
  85.  
  86. system("cls");
  87.  
  88. printf("Transformar letras de um arquivo para maiusculas\n\n");
  89.  
  90. PtrTxt = fopen(NomeArq, "r");
  91.  
  92. if(PtrTxt == NULL)
  93. printf("Arquivo %s inexistente\n\n", NomeArq);
  94. else
  95. {
  96. FILE *PtrAux = fopen("ARQUIVO_NOVO.txt", "w+");
  97.  
  98. Letra = toupper(getc(PtrTxt));
  99.  
  100. while(!feof(PtrTxt))
  101. {
  102. fputc(Letra, PtrAux);
  103.  
  104. Letra = toupper(getc(PtrTxt));
  105. }
  106.  
  107. fclose(PtrTxt);
  108. fclose(PtrAux);
  109.  
  110. printf("Processo concluido\n\n");
  111. }
  112.  
  113. getch();
  114. }
  115.  
  116. void ContaPalavra(char NomeArq[LIM], char Letra)
  117. {
  118. FILE *PtrTxt;
  119. int Cont, i;
  120. char LetraArq;
  121.  
  122. Letra = toupper(Letra);
  123.  
  124. system("cls");
  125.  
  126. printf("Contador de palavras\n\n");
  127.  
  128. PtrTxt = fopen(NomeArq, "r");
  129.  
  130. if(PtrTxt == NULL)
  131. printf("Arquivo %s inexistente\n\n", NomeArq);
  132. else
  133. {
  134. Cont = 0;
  135.  
  136. LetraArq = toupper(fgetc(PtrTxt));
  137.  
  138. if(LetraArq == Letra)
  139. Cont++;
  140.  
  141. while(!feof(PtrTxt))
  142. {
  143. if(LetraArq == ' ')
  144. {
  145. LetraArq = toupper(fgetc(PtrTxt));
  146.  
  147. if(LetraArq == Letra)
  148. Cont++;
  149. }
  150.  
  151. LetraArq = toupper(fgetc(PtrTxt));
  152. }
  153.  
  154. printf("%d palavras que comecam com %c\n", Cont, Letra);
  155.  
  156. fclose(PtrTxt);
  157. }
  158.  
  159. getch();
  160. }
  161.  
  162. void RemovePalavras(char NomeArq[100], char Letra)
  163. {
  164. FILE *PtrTxt;
  165. char LetraArq;
  166. int flag;
  167.  
  168. system("cls");
  169.  
  170. printf("Remover palavras do arquivo\n\n");
  171.  
  172. PtrTxt = fopen(NomeArq, "r");
  173.  
  174. if(PtrTxt == NULL)
  175. printf("Arquivo %s inexistente\n\n", NomeArq);
  176. else
  177. {
  178. FILE *PtrAux = fopen("NOVO_ARQUIVO.txt", "a+");
  179.  
  180. flag = 0;
  181.  
  182. LetraArq = fgetc(PtrTxt);
  183.  
  184. if(toupper(LetraArq) != toupper(Letra))
  185. flag = 1;
  186.  
  187. while(!feof(PtrTxt))
  188. {
  189. if(LetraArq == ' ')
  190. {
  191. LetraArq = fgetc(PtrTxt);
  192.  
  193. if(toupper(LetraArq) != toupper(Letra))
  194. {
  195. flag = 1;
  196. fputc(' ', PtrAux);
  197. }
  198. else
  199. flag = 0;
  200. }
  201.  
  202. if(flag == 1)
  203. fputc(LetraArq, PtrAux);
  204.  
  205. LetraArq = fgetc(PtrTxt);
  206. }
  207.  
  208. fclose(PtrTxt);
  209. fclose(PtrAux);
  210.  
  211. printf("Palavras com a letra %c removidas\n\n", Letra);
  212. }
  213.  
  214. getch();
  215. }
  216.  
  217. void QuebraArquivo(char NomeArq[100])
  218. {
  219. int TL, i;
  220. char linha[100], aux[100];
  221. FILE *PtrTxt;
  222.  
  223. system("cls");
  224.  
  225. PtrTxt = fopen(NomeArq, "r");
  226.  
  227. if(PtrTxt == NULL)
  228. printf("Arquivo %s inexistente\n", NomeArq);
  229. else
  230. {
  231. FILE *PtrAux = fopen("Novo.txt", "w");
  232.  
  233. TL = 0;
  234. strcpy(aux, "\0");
  235.  
  236. fgets(linha, 100, PtrTxt);
  237.  
  238. while(!feof(PtrTxt))
  239. {
  240. for(i = 0; linha[i] != '\0'; i++)
  241. if(toupper(linha[i]) >= 65 && toupper(linha[i]) <= 90)
  242. aux[TL++] = linha[i];
  243. else if(linha[i] == ' ' || linha[i] == 10)
  244. {
  245. aux[TL] = '\0';
  246. fputs(aux, PtrAux);
  247. fputs("\n", PtrAux);
  248.  
  249. TL = 0; strcpy(aux, "\0");
  250. }
  251.  
  252. fgets(linha, 100, PtrTxt);
  253. }
  254.  
  255.  
  256. fclose(PtrTxt);
  257. fclose(PtrAux);
  258.  
  259. printf("\n\nProcesso concluido\n\n");
  260.  
  261. getch();
  262.  
  263. LeArquivo("Novo.txt");
  264. }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement