daily pastebin goal
95%
SHARE
TWEET

Untitled

a guest Mar 21st, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <locale.h>
  3. #include <stdlib.h>
  4. #pragma warning(disable : 4996)
  5. #define M 128
  6. int create(char*name)
  7. {
  8.  
  9. int a, i, n;
  10. FILE*f;
  11. printf("n=");
  12. scanf_s("%d", &n);
  13. if ((f = fopen(name, "w")) != NULL)
  14. {
  15. for (i = 0;i < n;i++)
  16. {
  17. printf("x=");
  18. scanf_s("%d", &a);
  19. fwrite(&a, (sizeof(int)), 1, f);
  20. }
  21. fclose(f);
  22. }
  23. else printf("error1\n");
  24. return n;
  25. }
  26.  
  27. void print(char*name)
  28. {
  29. int a;
  30. FILE*f;
  31. if ((f = fopen(name, "r+")) != NULL)
  32. {
  33. while (!feof(f))
  34. {
  35. fread(&a, (sizeof(int)), 1, f);
  36. if (!feof(f))
  37. printf("%d ", a);
  38.  
  39. }
  40. fclose(f);
  41. }
  42. else printf("error2\n");
  43. }
  44.  
  45. void zad(char*name, int kol)
  46. {
  47. long int srez, p;
  48. int n = 0, a, chet = 0,nechet=0;
  49. FILE*f;
  50. if ((f = fopen(name, "r+")) != NULL)
  51. {
  52. while (!feof(f))
  53. {
  54. fread(&a, (sizeof(int)), 1, f);
  55. if (!feof(f))
  56. if (a % 2 == 0)
  57. chet++;
  58. else
  59. nechet++;
  60. }
  61. fclose(f);
  62. }
  63. else
  64. {
  65. printf("error3\n");
  66. }
  67. if ((f = fopen(name, "r+")) != NULL)
  68. {
  69.  
  70. fseek(f, 0, SEEK_END);
  71. fwrite(&chet, (sizeof(int)), 1, f);
  72. fwrite(&nechet, (sizeof(int)), 1, f);
  73. fclose(f);
  74. }
  75. else
  76. {
  77. printf("error4\n");
  78. }
  79. }
  80. int create1(char *filename)
  81. {
  82. FILE *file;
  83. char **b;
  84. int n;
  85. int i, j;
  86. printf("kol-vo strok:");
  87. scanf_s("%d", &n);
  88. getchar();
  89. b = (char**)malloc(n * sizeof(char*));
  90. for (i = 0; i<n; i++)
  91. b[i] = (char*)malloc(M * sizeof(char));
  92. file = fopen(filename, "wt");
  93. for (j = 0;j <n;j++) {
  94. printf("stroka:");
  95. gets(b[j]);
  96. fprintf(file, b[j]);
  97. fprintf(file, "\n");
  98. }
  99. fclose(file);
  100. return n;
  101. }
  102. void print1(char *filename)
  103. {
  104. FILE *file;
  105. int j = 0, i;
  106. char buffer;
  107. file = fopen(filename, "r");
  108. while (!feof(file))
  109. {
  110. while (fscanf(file, "%c", &buffer) != EOF) {
  111. printf("%c", buffer);
  112. }
  113. }
  114. fclose(file);
  115. }
  116. void print2(char *names)
  117. {
  118. FILE *file;
  119. int j = 0, i;
  120. char buffer;
  121. file = fopen(names, "rt");
  122. while (!feof(file))
  123. {
  124. while (fscanf(file, "%c", &buffer) != EOF) {
  125. printf("%c", buffer);
  126. }
  127. }
  128. fclose(file);
  129. }
  130. void zadanie(char *filename, char *names, int n)
  131. {
  132. FILE *file;
  133. FILE *nfile;
  134. int str = 0, g, kol_b = 0, na4 = -1, kol, k, j = 0, i;
  135. char buffer[128];
  136. char c[12];
  137. file = fopen(filename, "rt");
  138. nfile = fopen(names, "w");
  139. for (g = 0;g < n;g++) {
  140. fgets(buffer, 126, file);
  141. kol = strlen(buffer);
  142. while (j <kol-2)
  143. {
  144. if ((isalpha(buffer[j])) && (isdigit(buffer[j + 1])))
  145. {
  146. strncpy(&c[0], &buffer[j], 1);
  147. c[1] = '\0';
  148. fprintf(nfile,c);
  149. fprintf(nfile, " ");
  150.  
  151. }
  152. j++;
  153. }
  154. j = 0;
  155.  
  156. }
  157.  
  158. fclose(nfile);
  159. fclose(file);
  160. }
  161.  
  162.  
  163.  
  164. int main()
  165. {
  166.  
  167. int no, k, min, max;
  168. setlocale(LC_ALL, "rus");
  169. printf("vvedi nomer");
  170. scanf_s("%d", &no);
  171. if (no == 1) {
  172. char filename[] = "mybd.dat", name[20];
  173. int kol = create(filename);
  174. zad(filename, kol);
  175. print(filename);
  176. }
  177. if (no == 2) {
  178. int n;
  179. char filename[] = "mybd.txt";
  180. n = create1(filename);
  181. print1(filename, n);
  182. char names[] = "new.txt";
  183. zadanie(filename, names, n);
  184. print2(names);
  185. }
  186. return 0;
  187. }
RAW Paste Data
Top