fstream file reading

Started by
1 comment, last by Progames25 14 years, 11 months ago
Hello, I'm trying to read three lines of text from a .txt file for my program. I am using fstream likeso:

char str[1000];
    fstream thefile("File.txt",ios::in);
    while(!thefile.eof()) 
        {
              thefile.getline(str,1000);
              WxStaticText3->SetLabel(wxT(str));        
        }
    thefile.close();


That reads the text file and places the last piece of text in File.txt into my label. But the problem is that in the File.txt is something like: NumberOne = 5 NumberTwo = 1 NumberThree = 9 I will have variables in my program that will equal the values in the .txt file. So I can't figure out how to read the .txt file and have it look at NumberOne,NumberTwo, and NumberThree and then set the values set in the txt file to a variable in the program. In other words...I'm trying to make it reach each line and pull out the number after the = for each of those. Thanks, Progames25
-----------------------------I use C++ and DirectX.I'm new to game programming.
Advertisement
Of course you could write a complex class to read/write variables values in a file, and handle every error cases you will encounter, but what you want to do look very much like an .INI file.

Microsoft provides functions just for reading/writing ini files.
GetPrivateProfileInt is a good start, others function exist for other data type.

Microsoft also decided that INI files are deprecated and shouldn't be used, and recommend using the registry instead to store values. See here for more details.

Finally, I think the best choice is using XML. It's much more powerful and reliable, and there's also a lot of libraries for easy XML manipulation.
Hmm,
ok, i'll try using XML then.
I'm installing something called expat to help me use XML...
-----------------------------I use C++ and DirectX.I'm new to game programming.

This topic is closed to new replies.

Advertisement