سؤال

I am using the bellow struct 'Menu' to store menu data; after i am able to read properly from a file i will use for loops to load all of my menu data for each menu. variable is a vector of char* used to store any options in each menu. it is then used with the freetype library to display the menu options on menu buttons created using opengl.

char n = 0,
 tempBuffer[32];
FILE * menu = fopen ("MenuDatum.txt","r");
fgets (tempBuffer , 32 , menu);
Menue[n].variable.push_back(tempBuffer);//"text here");  <<
fclose (menu);

String literal where the commented '<<' is will work but the buffer outputs weird characters based on the size of tempbuffer when used (as is needed): if it's size 32 I get 4c, or if it's 16 I get lk, etc; but if i just output using cout it displays what is in the file properly... so I have neither a problem with input or output but i do? Also, 'char* tempBuffer = "text here";' will properly display '"text here"' with the library through my struct;etc. like a string literal does.

The text in the file is merely 'text here'

Here is the menu struct as well; i am not pasting all of the code because it would be too extensive

struct Drawings::menues
{
    std::vector<char*> variable;
    bool orrient;
    float xPos, xPosF, yPos, yPosF, CR, CB, CG, CA, sliceWidth, sliceSpacing;
    unsigned char options;
} Menue[3];
هل كانت مفيدة؟

المحلول

Without seeing the code, it's hard to tell. My Crystal Ball(R) tells me that you have e.g. a vector where you store these "strings". Since they are only pointers and they use a local buffer, they can't store the data properly and it gets corrupted later on.

std::ifstream in("MenuDatum.txt");
std::string line;
if(!getline(in, line))
    throw std::runtime_error("failed to read MenuDatum.txt");

std::vector<std::string> variable;
variable.push_back(line);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top