2

我在学校学习 C++,在我看来它是一门漂亮的语言,但我有这个烦人的问题。FILE *text课本上是用andscanf和写的printf,我个人不喜欢;我习惯了cinand coutor with<< >> 更好的说法fstream

所以这是我的问题:

  1. 我必须制作一个以二进制模式写入数据的应用程序(我已经完成了一半,但由于某种原因它没有以二进制模式写入)

  2. 在我写下城市(orasul)坐标(x 和 y)后,我必须搜索它们并获取这些值。(这里我尝试使用string.find)但我必须使用seekg“二进制模式”搜索并将这些值在结构中分开。

如果你们能以某种方式指导我,因为我在这里很迷失。有没有办法让我得到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;
    }
}
4

2 回答 2

1

就像我在评论中所说的那样,您不能真正寻找特定的条目,因为所有条目的大小都不同。

它可以通过拥有一个单独的索引文件来解决,其中每个索引条目都包含该条目在真实文件中的位置。这样,当您需要条目 X 时,您首先在索引文件中寻找正确的位置,读取该位置,然后使用该位置在实际数据文件中寻找。

这就是有多少DBM数据库管理器处理他们的数据。

索引文件中的条目必须是固定大小的,例如索引中的每个条目的类型都是std::ostream::pos_type,您可以使用它write来编写索引和read读取它。

于 2013-03-17T11:00:25.303 回答
0

检查https://stackoverflow.com/a/15452958/2156678。有询问者想要一种对二进制文件进行搜索(和更新)的方法,我提出了一种实现相同目的的简单方法。

于 2013-03-17T14:18:31.283 回答