Новичок
Профиль
Группа: Участник
Сообщений: 6
Регистрация: 10.8.2010
Репутация: нет Всего: нет
|
Код работает правильно только с комментрированием. Если расскоментировать, то первая запись портится. Может быть подскажет что нужно делать. | Код | #include "stdafx.h" #include <fstream> #include <iostream> #include <Windows.h> #include <stdio.h>
#define NTBK_BLK 74 #define SNF_BLK 8
using namespace std;
struct size_inf { int npos; int size; };
struct NOTEBOOK { char model[21]; // наименование, пробелы допускаются unsigned short price; // цена unsigned short weight; // масса в граммах unsigned short size_x; // длина в мм unsigned short size_y; // ширина в мм unsigned short size_z; // высота в мм unsigned short diagonal; // размер диагонали в миллиметрах unsigned short refresh_rate; // частота регенерации дисплея unsigned short rsltn_width; // разрешающая способность по длине в пикселах ===== unsigned short rsltn_height; // разрешающая способность по ширине в пикселах unsigned __int64 cpu_frequency; // частота CPU в герцах unsigned __int64 ram_capacity; // объем памяти RAM в байтах unsigned __int64 vga_capacity; // обьем видеопамяти в байтах unsigned __int64 hdd_capacity; // объем HDD в байтах };
char* Save(const NOTEBOOK& ntbk) { char *buf = new char[NTBK_BLK]; for(int i = 0; i < 21; ++i) buf[i] = ntbk.model[i];
*(unsigned short int*)(&buf[22]) = ntbk.price; *(unsigned short int*)(&buf[24]) = ntbk.weight; *(unsigned short int*)(&buf[26]) = ntbk.size_x; *(unsigned short int*)(&buf[28]) = ntbk.size_y; *(unsigned short int*)(&buf[30]) = ntbk.size_z; *(unsigned short int*)(&buf[32]) = ntbk.diagonal; *(unsigned short int*)(&buf[34]) = ntbk.refresh_rate; *(unsigned short int*)(&buf[36]) = ntbk.rsltn_width; *(unsigned short int*)(&buf[38]) = ntbk.rsltn_height; *(unsigned __int64*)(&buf[40]) = ntbk.cpu_frequency; *(unsigned __int64*)(&buf[48]) = ntbk.ram_capacity; *(unsigned __int64*)(&buf[56]) = ntbk.vga_capacity; *(unsigned __int64*)(&buf[64]) = ntbk.hdd_capacity; return buf; }
void Load(const char *buf, NOTEBOOK& ntbk) { for(int i = 0; i < 21; ++i) ntbk.model[i] = buf[i]; ntbk.price = *(unsigned short int*)(&buf[22]); ntbk.weight = *(unsigned short int*)(&buf[24]); ntbk.size_x = *(unsigned short int*)(&buf[26]); ntbk.size_y = *(unsigned short int*)(&buf[28]); ntbk.size_z = *(unsigned short int*)(&buf[30]); ntbk.diagonal = *(unsigned short int*)(&buf[32]); ntbk.refresh_rate = *(unsigned short int*)(&buf[34]); ntbk.rsltn_width = *(unsigned short int*)(&buf[36]); ntbk.rsltn_height = *(unsigned short int*)(&buf[38]); ntbk.cpu_frequency = *(unsigned __int64*)(&buf[40]); ntbk.ram_capacity = *(unsigned __int64*)(&buf[48]); ntbk.vga_capacity = *(unsigned __int64*)(&buf[56]); ntbk.hdd_capacity = *(unsigned __int64*)(&buf[64]); }
void GetFromFile(const char *pch, NOTEBOOK& ntbk, const unsigned int npos, bool& bErr) { size_inf sinf; fstream File; File.open(pch, ios::in); if(File.is_open()){ File.read((char *)(&sinf), SNF_BLK); if(sinf.npos < npos){ cerr << "Ошибка! Записи с позицией " << npos << " не существует!" << endl; bErr = true; return; }
char *buf = new char[NTBK_BLK]; File.close(); File.open(pch, ios::in | ios::out); File.seekp(SNF_BLK + npos * NTBK_BLK, ios::beg); File.read((char *)buf, NTBK_BLK); Load(buf, ntbk); delete[] buf; File.close(); bErr = false; } else{ cerr << "Ошибка! Не удалось открыть файл " << pch << "!" << endl; bErr = true; } }
void disp(const NOTEBOOK& ntbk) { cout << "*************************************************" << endl; cout << "ntbk.model: " << ntbk.model << endl; cout << "ntbk.price: " << ntbk.price << endl; cout << "ntbk.weight: " << ntbk.weight << endl; cout << "ntbk.size_x: " << ntbk.size_x << endl; cout << "ntbk.size_y: " << ntbk.size_y << endl; cout << "ntbk.size_z: " << ntbk.size_z << endl; cout << "ntbk.diagonal: " << ntbk.diagonal << endl; cout << "ntbk.refresh_rate: " << ntbk.refresh_rate << endl; cout << "ntbk.rsltn_width: " << ntbk.rsltn_width << endl; cout << "ntbk.rsltn_height: " << ntbk.rsltn_height << endl; cout << "ntbk.cpu_frequency: " << ntbk.cpu_frequency << endl; cout << "ntbk.ram_capacity: " << ntbk.ram_capacity << endl; cout << "ntbk.vga_capacity: " << ntbk.vga_capacity << endl; cout << "ntbk.hdd_capacity: " << ntbk.hdd_capacity << endl; cout << "*************************************************" << endl; }
void WriteToFile(const char *pch, const NOTEBOOK& ntbk) { size_inf sinf; fstream File; File.open(pch, ios::in); if(File.is_open()){ File.read((char *)(&sinf), SNF_BLK); File.close(); File.open(pch, ios::in | ios::out); if(!(sinf.npos % 16) && sinf.npos){ sinf.size += sinf.npos; int blks = sinf.npos * NTBK_BLK; char *buf = new char[blks]; File.seekp(0, ios::end); File.write((char *)buf, blks); delete[] buf; }
++sinf.npos; File.seekp(0, ios::beg); File.write((char *)(&sinf), SNF_BLK); char *buf = Save(ntbk); File.seekp(SNF_BLK + sinf.npos * NTBK_BLK, ios::beg); File.write((char *)buf, NTBK_BLK); delete[] buf; } else{ File.close(); File.clear(); File.open(pch, ios::out); sinf.npos = 0; sinf.size = 20; char *buf = Save(ntbk); File.write((char *)(&sinf), SNF_BLK); File.write((char *)buf, NTBK_BLK); delete[] buf; int blks = 19 * NTBK_BLK; buf = new char[blks]; File.write((char *)buf, blks); delete[] buf; } }
int _tmain(int argc, _TCHAR* argv[]) { SetConsoleCP(1251); SetConsoleOutputCP(1251);
NOTEBOOK nt1, nt2, nt3, nt4, nt5, nt6, nt7, nt8, nt9, nt10, nt11, nt12, nt13, nt14, nt15, nt16, nt17, nt18; string str; bool bErr; ifstream inFile("notebook.dat"); if(inFile.is_open()){ inFile.close(); remove("notebook.dat"); }
str = "Acer Note Light"; strcpy_s(nt1.model, 21, str.c_str()); nt1.price = 2699; nt1.weight = 5600; nt1.size_x = 51; nt1.size_y = 300; nt1.size_z = 211; nt1.diagonal = 264; nt1.refresh_rate = 60; nt1.rsltn_width = 1024; nt1.rsltn_height = 768; nt1.cpu_frequency = 100000; nt1.ram_capacity = 41943040; nt1.vga_capacity = 1048576; nt1.hdd_capacity = 831076172; str = "ASW ND5123T"; strcpy_s(nt2.model, 21, str.c_str()); nt2.price = 3489; nt2.weight = 7200; nt2.size_x = 58; nt2.size_y = 300; nt2.size_z = 256; nt2.diagonal = 307; nt2.refresh_rate = 70; nt2.rsltn_width = 1024; nt2.rsltn_height = 768; nt2.cpu_frequency = 133000; nt2.ram_capacity = 33554432; nt2.vga_capacity = 2097152; nt2.hdd_capacity = 1395864371;
str = "ARMNote TS80CD"; strcpy_s(nt3.model, 21, str.c_str()); nt3.price = 3699; nt3.weight = 7200; nt3.size_x = 51; nt3.size_y = 292; nt3.size_z = 223; nt3.diagonal = 287; nt3.refresh_rate = 75; nt3.rsltn_width = 1024; nt3.rsltn_height = 768; nt3.cpu_frequency = 133000; nt3.ram_capacity = 67108864; nt3.vga_capacity = 1048576; nt3.hdd_capacity = 1395864371;
str = "AST Ascentia P50"; strcpy_s(nt4.model, 21, str.c_str()); nt4.price = 4499; nt4.weight = 7500; nt4.size_x = 58; nt4.size_y = 287; nt4.size_z = 229; nt4.diagonal = 287; nt4.refresh_rate = 70; nt4.rsltn_width = 800; nt4.rsltn_height = 600; nt4.cpu_frequency = 133000; nt4.ram_capacity = 41943040; nt4.vga_capacity = 1048576; nt4.hdd_capacity = 831076172;
str = "BSI NP8657D"; strcpy_s(nt5.model, 21, str.c_str()); nt5.price = 2605; nt5.weight = 8000; nt5.size_x = 58; nt5.size_y = 300; nt5.size_z = 236; nt5.diagonal = 287; nt5.refresh_rate = 60; nt5.rsltn_width = 1024; nt5.rsltn_height = 768; nt5.cpu_frequency = 133000; nt5.ram_capacity = 41943040; nt5.vga_capacity = 1048576; nt5.hdd_capacity = 869730877;
str = "BSI NP5265A"; strcpy_s(nt6.model, 21, str.c_str()); nt6.price = 3765; nt6.weight = 8200; nt6.size_x = 63; nt6.size_y = 305; nt6.size_z = 229; nt6.diagonal = 307; nt6.refresh_rate = 70; nt6.rsltn_width = 1024; nt6.rsltn_height = 768; nt6.cpu_frequency = 150000; nt6.ram_capacity = 33554432; nt6.vga_capacity = 2097152; nt6.hdd_capacity = 1395864371;
str = "Dell Xpi PIOOSD"; strcpy_s(nt7.model, 21, str.c_str()); nt7.price = 3459; nt7.weight = 6000; nt7.size_x = 58; nt7.size_y = 279; nt7.size_z = 223; nt7.diagonal = 262; nt7.refresh_rate = 60; nt7.rsltn_width = 1024; nt7.rsltn_height = 768; nt7.cpu_frequency = 100000; nt7.ram_capacity = 41943040; nt7.vga_capacity = 1048576; nt7.hdd_capacity = 830002430;
str = "Digital HiNote"; strcpy_s(nt8.model, 21, str.c_str()); nt8.price = 4799; nt8.weight = 4000; nt8.size_x = 33; nt8.size_y = 279; nt8.size_z = 223; nt8.diagonal = 264; nt8.refresh_rate = 56; nt8.rsltn_width = 800; nt8.rsltn_height = 600; nt8.cpu_frequency = 120000; nt8.ram_capacity = 41943040; nt8.vga_capacity = 1048576; nt8.hdd_capacity = 1073741824;
str = "Gateway Solo S5"; strcpy_s(nt9.model, 21, str.c_str()); nt9.price = 4499; nt9.weight = 5600; nt9.size_x = 51; nt9.size_y = 302; nt9.size_z = 223; nt9.diagonal = 287; nt9.refresh_rate = 60; nt9.rsltn_width = 1024; nt9.rsltn_height = 768; nt9.cpu_frequency = 133000; nt9.ram_capacity = 41943040; nt9.vga_capacity = 2097152; nt9.hdd_capacity = 736586891;
str = "Hertz Z-Optima NB"; strcpy_s(nt10.model, 21, str.c_str()); nt10.price = 3995; nt10.weight = 8000; nt10.size_x = 58; nt10.size_y = 302; nt10.size_z = 229; nt10.diagonal = 284; nt10.refresh_rate = 75; nt10.rsltn_width = 1024; nt10.rsltn_height = 768; nt10.cpu_frequency = 150000; nt10.ram_capacity = 41943040; nt10.vga_capacity = 2097152; nt10.hdd_capacity = 1073741824;
str = "HP Omni Book 5500"; strcpy_s(nt11.model, 21, str.c_str()); nt11.price = 6120; nt11.weight = 7100; nt11.size_x = 51; nt11.size_y = 292; nt11.size_z = 229; nt11.diagonal = 290; nt11.refresh_rate = 75; nt11.rsltn_width = 1024; nt11.rsltn_height = 768; nt11.cpu_frequency = 133000; nt11.ram_capacity = 67108864; nt11.vga_capacity = 1048576; nt11.hdd_capacity = 1395864371;
str = "IBM ThinkPad 560"; strcpy_s(nt12.model, 21, str.c_str()); nt12.price = 3749; nt12.weight = 4100; nt12.size_x = 33; nt12.size_y = 300; nt12.size_z = 223; nt12.diagonal = 307; nt12.refresh_rate = 85; nt12.rsltn_width = 1024; nt12.rsltn_height = 768; nt12.cpu_frequency = 120000; nt12.ram_capacity = 41943040; nt12.vga_capacity = 2097152; nt12.hdd_capacity = 831076172;
str = "NEC Versa 4080H"; strcpy_s(nt13.model, 21, str.c_str()); nt13.price = 4780; nt13.weight = 6600; nt13.size_x = 57; nt13.size_y = 300; nt13.size_z = 241; nt13.diagonal = 264; nt13.refresh_rate = 70; nt13.rsltn_width = 800; nt13.rsltn_height = 600; nt13.cpu_frequency = 120000; nt13.ram_capacity = 50331648; nt13.vga_capacity = 1048576; nt13.hdd_capacity = 833223655;
str = "Polywell Poly 500"; strcpy_s(nt14.model, 21, str.c_str()); nt14.price = 3300; nt14.weight = 7900; nt14.size_x = 58; nt14.size_y = 302; nt14.size_z = 229; nt14.diagonal = 264; nt14.refresh_rate = 72; nt14.rsltn_width = 1024; nt14.rsltn_height = 768; nt14.cpu_frequency = 120000; nt14.ram_capacity = 41943040; nt14.vga_capacity = 1048576; nt14.hdd_capacity = 1073741824; str = "Samsung SENS 810"; strcpy_s(nt15.model, 21, str.c_str()); nt15.price = 3667; nt15.weight = 8700; nt15.size_x = 58; nt15.size_y = 292; nt15.size_z = 241; nt15.diagonal = 289; nt15.refresh_rate = 75; nt15.rsltn_width = 1024; nt15.rsltn_height = 768; nt15.cpu_frequency = 100000; nt15.ram_capacity = 33554432; nt15.vga_capacity = 2097152; nt15.hdd_capacity = 830002430;
str = "Twinhead Slimnote"; strcpy_s(nt16.model, 21, str.c_str()); nt16.price = 2965; nt16.weight = 7400; nt16.size_x = 51; nt16.size_y = 292; nt16.size_z = 203; nt16.diagonal = 264; nt16.refresh_rate = 70; nt16.rsltn_width = 1024; nt16.rsltn_height = 768; nt16.cpu_frequency = 75000; nt16.ram_capacity = 67108864; nt16.vga_capacity = 1048576; nt16.hdd_capacity = 828928688;
WriteToFile("notebook.dat", nt1); WriteToFile("notebook.dat", nt2); WriteToFile("notebook.dat", nt3); WriteToFile("notebook.dat", nt4); WriteToFile("notebook.dat", nt5); WriteToFile("notebook.dat", nt6); WriteToFile("notebook.dat", nt7); WriteToFile("notebook.dat", nt8); WriteToFile("notebook.dat", nt9); WriteToFile("notebook.dat", nt10); // WriteToFile("notebook.dat", nt11); // WriteToFile("notebook.dat", nt12); // WriteToFile("notebook.dat", nt13); // WriteToFile("notebook.dat", nt14); // WriteToFile("notebook.dat", nt15); // WriteToFile("notebook.dat", nt16);
cout << "nt1:" << endl; disp(nt1); int npos = 0; GetFromFile("notebook.dat", nt17, npos, bErr); cout << "bErr: " << bErr << endl; disp(nt17); return 0; }
|
Это сообщение отредактировал(а) fprg - 12.8.2010, 20:36
|