Skip to content

Instantly share code, notes, and snippets.

@mik-laj
Last active January 30, 2016 18:25
Show Gist options
  • Save mik-laj/e5fefe562c79bff58639 to your computer and use it in GitHub Desktop.
Save mik-laj/e5fefe562c79bff58639 to your computer and use it in GitHub Desktop.
Sortowanie struktur w plikach
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
int cmp_char_arr(const char* a, const char* b, int size_a, int size_b) {
int min_size = size_a < size_b ? size_a : size_b;
for (int i = 0; i < min_size; i++) {
if (a[i] > b[i]) {
return -1;
} else if (a[i] < b[i]) {
return 1;
}
}
return 0;
}
struct my_struct {
char name[80];
};
void write_initial_data() {
fstream file("my_database.dat", ios::binary | ios::out | ios::in | ios::app);
my_struct a;
strncpy(a.name, "BAA", sizeof(a.name));
file.write(reinterpret_cast<char*>(&a), sizeof(a));
strncpy(a.name, "AAA", sizeof(a.name));
file.write(reinterpret_cast<char*>(&a), sizeof(a));
strncpy(a.name, "CCC", sizeof(a.name));
file.write(reinterpret_cast<char*>(&a), sizeof(a));
strncpy(a.name, "BBB", sizeof(a.name));
file.write(reinterpret_cast<char*>(&a), sizeof(a));
strncpy(a.name, "AAB", sizeof(a.name));
file.write(reinterpret_cast<char*>(&a), sizeof(a));
strncpy(a.name, "ABA", sizeof(a.name));
file.write(reinterpret_cast<char*>(&a), sizeof(a));
strncpy(a.name, "ABA", sizeof(a.name));
file.write(reinterpret_cast<char*>(&a), sizeof(a));
strncpy(a.name, "BAA", sizeof(a.name));
file.write(reinterpret_cast<char*>(&a), sizeof(a));
strncpy(a.name, "AAA", sizeof(a.name));
file.write(reinterpret_cast<char*>(&a), sizeof(a));
file.close();
}
void display_file() {
fstream file("my_database.dat", ios::binary | ios::out | ios::in);
file.seekg(0);
if (!file.eof()) {
for (;;) {
my_struct a;
file.read(reinterpret_cast<char*>(&a), sizeof(a));
if (file.eof()) break;
cout << a.name << endl;
}
}
}
void sort_struct_in_file() {
fstream file("my_database.dat", ios::binary | ios::out | ios::in | ios::ate);
my_struct a;
my_struct b;
file.seekg (0, file.end);
int length = file.tellg();
file.seekg (0, file.beg);
int size = length / sizeof(a);
for ( int i = 0; i < size; i++ ) {
for ( int j = 0; j < size - 1; j++ ) {
file.seekg(sizeof(struct my_struct) * ( j ));
file.read(reinterpret_cast<char*>(&a), sizeof(a));
file.seekg(sizeof(struct my_struct) * ( j + 1));
file.read(reinterpret_cast<char*>(&b), sizeof(b));
int cmp_result = cmp_char_arr(a.name, b.name, sizeof(a.name), sizeof(b.name));
// cout << a.name << " = " << b.name << " = " << cmp_result << endl;
if (cmp_result < 0) {
// cout << a.name << "<=>" << b.name << endl;
file.seekp(sizeof(struct my_struct) * ( j ));
file.write(reinterpret_cast<char*>(&b), sizeof(b));
file.seekp(sizeof(struct my_struct) * ( j + 1));
file.write(reinterpret_cast<char*>(&a), sizeof(a));
}
}
}
file.close();
}
int main() {
write_initial_data();
cout << "Plik: " << endl;
display_file();
sort_struct_in_file();
cout << "Plik po:" << endl;
display_file();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment