Created
December 7, 2012 12:41
-
-
Save vadimtsushko/4233023 to your computer and use it in GitHub Desktop.
Болванка по задачке
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <conio.h> | |
#include <list> | |
#include <string> | |
using namespace std; | |
struct tCar | |
{ | |
char model [100]; | |
char color [20]; | |
int year; | |
double price; | |
}; | |
list<tCar> cars; | |
void reaInitialdData() | |
{ | |
ifstream fin; | |
fin.open("C:\\dropbox\\input.txt"); | |
cars.clear(); | |
while(!fin.eof()) | |
{ | |
tCar car; | |
fin >> car.model; | |
fin >> car.year; | |
fin >> car.price; | |
fin >> car.color; | |
cars.push_back(car); | |
} | |
fin.close(); | |
} | |
void printData(string header) | |
{ | |
list<tCar>::iterator p = cars.begin(); | |
cout << header << "\n"; | |
while(p != cars.end()) | |
{ | |
tCar car = *p; | |
cout << car.model | |
<< '\t' << car.year | |
<< '\t' << car.price | |
<< '\t' << car.color | |
<<endl; | |
p++; | |
}; | |
} | |
void writeData() | |
{ | |
list<tCar>::iterator p = cars.begin(); | |
FILE *f; | |
f=fopen("cars.dat", "wb"); | |
while(p != cars.end()) | |
{ | |
tCar car = *p; | |
fwrite(&car,sizeof(tCar),1,f); | |
p++; | |
}; | |
fclose(f); | |
} | |
void readBinaryData() | |
{ | |
FILE *f; | |
f=fopen("cars.dat", "rb"); | |
cars.clear(); | |
while(!feof(f)) | |
{ | |
tCar car; | |
int success = fread(&car,sizeof(tCar),1,f); | |
if (success == 1) | |
{ | |
cars.push_back(car); | |
} | |
}; | |
fclose(f); | |
} | |
void main () | |
{ | |
setlocale (LC_ALL, "RUS"); | |
reaInitialdData(); | |
printData("Initial data"); | |
writeData(); | |
readBinaryData(); | |
printData("Data from binary file"); | |
getch(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment