简体   繁体   中英

C++ reading/writing binary mode

I'm learning C++ at school and in my opinion it's a beautiful language, but I have this annoying problem. In the text book it's written with FILE *text and scanf and printf , and I personally don't like it; I got used to cin and cout or with the << >> better say with the fstream .

So here is my problem:

  1. I have to make an application that writes data in binary mode (I have done it on half of it but it doesn't write in binary mode for some reason)

  2. After I write the city (orasul) the coordinates (x and y) I have to search for them and get those values. (Here I tried to use string.find ) but I have to use seekg to search in "binary mode" and get those values separate in a structure.

If you guys can guide me somehow cause I am pretty lost here. And is there a way I can get the sizeof(struct) ?

#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include <limits>

using namespace std;

struct oras {
    std::string orasul;
    int x;
    int y;
} ora;


void functiaPrincipala();
void calculator(float coordonate_x1, float coordonate_y1, float coordonate_x2, float coordonate_y2);
void adaugaOras();
void stergeLocatie();
void repetare();

void main() {
    functiaPrincipala();
}

void functiaPrincipala() {
    // variabile
    int obtiune;
    // ofstream fisierOut;
    // ifstream fisierIn;


    cout << "1) Adauga localitate: " << endl;
    cout << "2) Stergerea unei localitati existente: " << endl;
    cout << "3) Stergerea tuturor localitatilor existente: " << endl;
    cout << "4) Afisarea tuturor localitatilor existente: " << endl;
    cout << "5) Calculul distantei a doua localitati: " << endl;
    cout << "Introduceti obtiunea: " << endl;
    cin >> obtiune;

    switch (obtiune) {
        case 1:
            adaugaOras();
            break;
        case 2:
            stergeLocatie();
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            break;
    }

    getch();
}

void calculator(float coordonate_x1, float coordonate_y1, float coordonate_x2, float coordonate_y2) {
    float rezultat;


    rezultat = sqrt((coordonate_x2 * coordonate_x1) - (coordonate_x2 * coordonate_x1) + (coordonate_y2 * coordonate_y1) - (coordonate_y2 * coordonate_y1));

    cout << "Distanta de la orasul 1 la orasul 2 este de: " << rezultat;

}

void adaugaOras() {
    int n;
    ofstream fisierOutt("textttt.txt", ios::app | ios::binary);

    //  fisierOutt.open("textttt.txt");
    cout << "Cate orase doresti sa introduci: ";
    cin >> n;
    if (fisierOutt.is_open()) {

        for (int i = 0; i < n; i++) {
            cout << "Introdu numele orasului: ";
            cin >> ora.orasul;
            cout << "Introdu coordonatele x: ";
            cin >> ora.x;
            cout << "Introdu coordonatele y: ";
            cin >> ora.y;
            fisierOutt << ora.orasul << " " << ora.x << " " << ora.y << endl;
            cout << endl << endl;




        }

    } else {
        cout << "Nu am putut deschide fisierul";
    }
    fisierOutt.close();
    cout << endl;
    // repetare();
}

void stergeLocatie() {

}

void repetare() {
    char obtiune;
    cout << "Doriti sa mai adaugati ceva sau sa iesiti?(d/n)";
    cin >> obtiune;
    if (obtiune == 'd') {
        functiaPrincipala();
    } else {
        exit;
    }
}

Like I said in my comment, you can't really seek to a specific entry as all entries are of different sizes.

It can be solved by having a separate index file, where each index entry contains of the position of the entry in the real file. This way, when you need entry X you first seek in the index file to the correct position, read the position, and then use that to seek in the real data file.

This is how many DBM database-managers handle their data.

The entries in the index-file has to be fixed size, for example each entry in the index is of type std::ostream::pos_type , and you use write to write the index, and read to read it.

Check https://stackoverflow.com/a/15452958/2156678 . There asker wanted a way to do search (and update) on binary file and I proposed a simple way to achieve the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM