Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <ctime>
- #include <stdlib.h>
- #include <Windows.h>
- using namespace std;
- int main() {
- SetConsoleOutputCP(1251);
- SetConsoleCP(1251);
- int max = 0;
- int temp;
- int min = 100000;
- ofstream outfile("file_in.dat");
- ifstream infile("file_in.dat");
- for (int i = 0; i < 10; i++) {
- outfile << rand() % 100 << endl;
- }
- for (int i = 0; i < 10; i++) {
- infile >> temp;
- if (temp > max) {
- max = temp;
- }if (temp < min) {
- min = temp;
- }
- }
- cout << "Максимальне число: "<< max << endl;
- cout << "Мінімальне число: "<< min << endl;
- outfile << "Max: " << max << endl;
- outfile << "Min: " << min << endl;
- outfile.close();
- infile.close();
- }
- #include <stdio.h>
- #include <iostream>
- #include <string>
- #include <Windows.h>
- using namespace std;
- /* run this program using the console pauser or add your own getch, system("pause") or input loop */
- const int N = 2;
- struct product {
- char name [10];
- int count;
- double price;
- } products [N];
- int save(char * filename, struct product *st, int n);
- int main(int argc, char** argv) {
- SetConsoleOutputCP(1251);
- SetConsoleCP(1251);
- for(int i = 0; i < N; i++){
- cout << "Введите название продукта:" << endl;
- cin >> products[i].name;
- cout << endl << "Количество:" << endl;
- cin >> products[i].count;
- cout << endl << "Цена:" << endl;
- cin >> products[i].price;
- }
- for (int i = 0; i <N; i++) {
- cout << endl << i + 1 << ". Продукт" << " ";
- cout << "Название: " << products[i].name << " ";
- cout << "Кол-во: " << products[i].count << " ";
- cout << "Цена: " << products[i].price << endl;
- }
- char * filename = "my_file.dat";
- int n = sizeof(products) / sizeof(products[0]);
- save(filename, products, n);
- /*FILE* fp = fopen("my_file.txt","wb");
- fwrite(&products[0], sizeof(product),1,fp); //запись о первой книге
- fclose(fp);
- system("pause"); // открыть файл my_file.txt в блокноте
- fp = fopen("my_file.txt","ab");
- fwrite(&products[1], sizeof(product),1,fp); //запись о второй книге
- fclose(fp); */
- return 0;
- }
- int save(char * filename, struct product * st, int n)
- {
- FILE * fp;
- char *c;
- // число записываемых байтов
- int size = n * sizeof(struct product);
- if ((fp = fopen(filename, "wb")) == NULL)
- {
- perror("Error occured while opening file");
- return 1;
- }
- // записываем количество структур
- c = (char *)&n;
- for (int i = 0; i<sizeof(int); i++)
- {
- putc(*c++, fp);
- }
- // посимвольно записываем в файл все структуры
- c = (char *)st;
- for (int i = 0; i < size; i++)
- {
- putc(*c, fp);
- c++;
- }
- fclose(fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement