开发者

std::vector weird

Is it important in which order I put objects to vector? Because right now I have a structure which has so开发者_C百科me integers, some char array and ID number(unique) and when I try to find that one with specific ID, I can find only that one which I putted as first to vector, but when I put ID of for example second element I get No found but I can still get needed information. And when I put ID that doesn't exist at all program exit(At least that work).


The code block

else
{
    printf("Trigger could not be found.\n");
}

is the problem I think. If you have IDs 1, 2 and 3 in your vector and search for 2, it will print out "Trigger could not be found the first time round the loop, and then match on the second.

You probably want to return immediately if the file can't be opened, and print out the "Trigger could not be found" after the end of the loop (since if the trigger had been found you would already have returned from the search function)


I will post it here, the most important parts of code: Structure:

struct Trigger {
    unsigned short int ID;
    char Nazwa_Map_Do_Wczytania[30];
    unsigned Offset_Triggera;
    unsigned x1,y1,x2,y2;
    char Identyfikator[30];
};

Code which is suppose to add new trigger to global vector of that structure(damnit == that global vector)

    bool UtworzDat(std::vector<std::string> pliki,std::vector<Trigger> trigg, char* sciezka,Naglowek* nagl)
{
Trigger chujew;
    nagl->Ilosc_Triggerow = trigg.size();
        for (unsigned int i = 0; i < trigg.size(); i++)
        {
            memset(&chujew,0,sizeof(Trigger));
            chujew.ID = trigg[i].ID;
            strcpy(chujew.Identyfikator,trigg[i].Identyfikator);
            strcpy(chujew.Nazwa_Map_Do_Wczytania,trigg[i].Nazwa_Map_Do_Wczytania);
            chujew.Offset_Triggera = 0;
            chujew.x1 = trigg[i].x1;
            chujew.x2 = trigg[i].x2;
            chujew.y1 = trigg[i].y1;
            chujew.y2 = trigg[i].y2;
            damnit.push_back(chujew);
        }

Then I set right offsets:

long prawdziwy_offset = 0;
    prawdziwy_offset += sizeof(Naglowek); <--Header of file
    for (unsigned int i = 0; i < trigg.size(); i++)
    {
        damnit[i].Offset_Triggera = prawdziwy_offset;
        prawdziwy_offset += sizeof(Trigger);
    }
    prawdziwy_offset += nagl->Ilosc_Obrazow * sizeof(Obraz); <--It's saving structures golding information about each file inside 
    for (unsigned int i = 0; i < wszystkie.size(); i++)
    {
        wszystkie[i].Offset_Obrazu = prawdziwy_offset;
        prawdziwy_offset += wszystkie[i].Rozmiar_Obrazu;
    }

Then it's being save to .dat file all together with other graphics, then I read header from file and process data. I am sure that saving is good.

Anyway that simple finding:

Trigger WyciagnijTriggera(unsigned short int ID, Naglowek* nagl)
{
    Trigger temp;
    memset(&temp,0,sizeof(Trigger));
    FILE* wejscie;
    for (int i = 0; i < damnit.size(); i++)
    {
        if (ID == damnit[i].ID) <--here
        {
            wejscie = fopen(nazwa,"rb"); //<--opening file to find it
            if (wejscie != NULL)
            {
                fseek(wejscie,damnit[i].Offset_Triggera,SEEK_SET); //<--Going to right offset
                fread(&temp,sizeof(Trigger),1,wejscie);
                fclose(wejscie);
                return temp;
            }
            else
            {
                printf("Could not open file.\n");
            }
        }
        else
        {
            printf("Trigger could not be found.\n");
        }
    }
}

In main it looks like:

Naglowek nowy;
    Trigger kut, kutzapis,kutzapis2,kutzapis3;
    memset(&kutzapis,0,sizeof(Trigger));
    memset(&kutzapis2,0,sizeof(Trigger));
    memset(&kutzapis3,0,sizeof(Trigger));
    memset(&kut,0,sizeof(Trigger));
    memset(&nowy,0,sizeof(Naglowek));
    std::vector<Trigger> pedal;
    strcpy(kutzapis.Identyfikator,"pedal");
    strcpy(kutzapis.Nazwa_Map_Do_Wczytania, "tet");
    kutzapis.x1 = 5050;
    kutzapis.x2 = 9090;
    kutzapis.y1 = 2323;
    kutzapis.y2 = 2341;
    kutzapis.ID = 1;
    strcpy(kutzapis2.Identyfikator,"pedal2");
    strcpy(kutzapis2.Nazwa_Map_Do_Wczytania, "tet2");
    kutzapis2.x1 = 50502;
    kutzapis2.x2 = 90902;
    kutzapis2.y1 = 23232;
    kutzapis2.y2 = 23412;
    kutzapis2.ID = 2;
    strcpy(kutzapis3.Identyfikator,"pedal3");
    strcpy(kutzapis3.Nazwa_Map_Do_Wczytania, "tet3");
    kutzapis3.x1 = 505023;
    kutzapis3.x2 = 909023;
    kutzapis3.y1 = 232323;
    kutzapis3.y2 = 234123;
    kutzapis3.ID = 3;
    pedal.push_back(kutzapis3);
    pedal.push_back(kutzapis2);
    pedal.push_back(kutzapis);
    UtworzDat(toto,pedal,"maslo.dat",&nowy); //Creating file
    CzytajDat("maslo.dat",&nowy);
    kut = WyciagnijTriggera(2,&nowy); //Tring to find trigger with ID: 2
    std::cout << kut.ID << std::endl << kut.Identyfikator << std::endl << kut.Nazwa_Map_Do_Wczytania << std::endl << kut.Offset_Triggera << std::endl << kut.x1 << std::endl << kut.x2 << std::endl << kut.y1 << std::endl << kut.y2 << std::endl;

Sorry for this long ass code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜