Advertisement
lashrone1

pr.7.1

Apr 15th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <ctime>
  4. #include <stdlib.h>
  5. #include <Windows.h>
  6. using namespace std;
  7.  
  8. int main() {
  9. SetConsoleOutputCP(1251);
  10. SetConsoleCP(1251);
  11.  
  12. int max = 0;
  13. int temp;
  14. int min = 100000;
  15. ofstream outfile("file_in.dat");
  16. ifstream infile("file_in.dat");
  17. for (int i = 0; i < 10; i++) {
  18.  
  19. outfile << rand() % 100 << endl;
  20. }
  21.  
  22.  
  23. for (int i = 0; i < 10; i++) {
  24. infile >> temp;
  25. if (temp > max) {
  26. max = temp;
  27. }if (temp < min) {
  28. min = temp;
  29. }
  30. }
  31. cout << "Максимальне число: "<< max << endl;
  32. cout << "Мінімальне число: "<< min << endl;
  33. outfile << "Max: " << max << endl;
  34. outfile << "Min: " << min << endl;
  35. outfile.close();
  36. infile.close();
  37.  
  38. }
  39.  
  40. #include <stdio.h>
  41. #include <iostream>
  42. #include <string>
  43. #include <Windows.h>
  44. using namespace std;
  45. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  46. const int N = 2;
  47.  
  48. struct product {
  49. char name [10];
  50. int count;
  51. double price;
  52. } products [N];
  53.  
  54. int save(char * filename, struct product *st, int n);
  55.  
  56. int main(int argc, char** argv) {
  57. SetConsoleOutputCP(1251);
  58. SetConsoleCP(1251);
  59.  
  60. for(int i = 0; i < N; i++){
  61. cout << "Введите название продукта:" << endl;
  62. cin >> products[i].name;
  63. cout << endl << "Количество:" << endl;
  64. cin >> products[i].count;
  65. cout << endl << "Цена:" << endl;
  66. cin >> products[i].price;
  67. }
  68. for (int i = 0; i <N; i++) {
  69. cout << endl << i + 1 << ". Продукт" << " ";
  70. cout << "Название: " << products[i].name << " ";
  71. cout << "Кол-во: " << products[i].count << " ";
  72. cout << "Цена: " << products[i].price << endl;
  73.  
  74. }
  75. char * filename = "my_file.dat";
  76. int n = sizeof(products) / sizeof(products[0]);
  77. save(filename, products, n);
  78.  
  79.  
  80. /*FILE* fp = fopen("my_file.txt","wb");
  81. fwrite(&products[0], sizeof(product),1,fp); //запись о первой книге
  82. fclose(fp);
  83. system("pause"); // открыть файл my_file.txt в блокноте
  84. fp = fopen("my_file.txt","ab");
  85. fwrite(&products[1], sizeof(product),1,fp); //запись о второй книге
  86. fclose(fp); */
  87.  
  88. return 0;
  89. }
  90.  
  91. int save(char * filename, struct product * st, int n)
  92. {
  93. FILE * fp;
  94. char *c;
  95.  
  96. // число записываемых байтов
  97. int size = n * sizeof(struct product);
  98.  
  99. if ((fp = fopen(filename, "wb")) == NULL)
  100. {
  101. perror("Error occured while opening file");
  102. return 1;
  103. }
  104. // записываем количество структур
  105. c = (char *)&n;
  106. for (int i = 0; i<sizeof(int); i++)
  107. {
  108. putc(*c++, fp);
  109. }
  110.  
  111. // посимвольно записываем в файл все структуры
  112. c = (char *)st;
  113. for (int i = 0; i < size; i++)
  114. {
  115. putc(*c, fp);
  116. c++;
  117. }
  118. fclose(fp);
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement