简体   繁体   English

C ++读写二进制模式

[英]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. 我在学校学习C ++,我认为这是一门漂亮的语言,但是我有这个烦人的问题。 In the text book it's written with FILE *text and scanf and printf , and I personally don't like it; 在教科书中,它是用FILE *textscanfprintf编写的,我个人不喜欢它。 I got used to cin and cout or with the << >> better say with the fstream . 我习惯了cincout或者习惯了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. 在写完城市(orasul)后,必须搜索坐标(x和y)并获取这些值。 (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. (在这里,我尝试使用string.find ),但是我必须使用seekg在“二进制模式”中进行搜索,并在结构中将这些值分开。

If you guys can guide me somehow cause I am pretty lost here. 如果你们能以某种方式指导我,因为我在这里很迷路。 And is there a way I can get the sizeof(struct) ? 有没有办法我可以得到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. 这样,当您需要条目X时,您首先在索引文件中查找到正确的位置,读取该位置,然后使用该位置在实际数据文件中进行查找。

This is how many DBM database-managers handle their data. 这就是多少个DBM数据库管理器处理他们的数据。

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. 索引文件中的条目必须固定大小,例如,索引中的每个条目的类型std::ostream::pos_type ,您可以使用write来编写索引,然后使用read来读取索引。

Check https://stackoverflow.com/a/15452958/2156678 . 检查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. 问问者想要一种对二进制文件进行搜索(和更新)的方法,我提出了一种简单的方法来实现这一目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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