I am using Qt5.2.1 and I made a program that stores some data into a file and then reads the data.

it stores some variables values.

After I restart my program... I want to read those values and re-store the values of those variables... How can I do this? I am fairly new to Qt (programming altogether).

Thank you in advance.

this is some text inside the file -
500 - 1000 : 29
1000 - 1500 : 0
1500 - 2000 : 0
2000 - 2500 : 0

here the 29,0,0,0 are the values and I would like to assign them to the variables.

It' just standard c++ ifstream and ofstream. When the program starts, try to open the file, if it success then read it.

@Ancient Dragon I dont just want to read it. I want to read specific pieces of data, and use that

Override << and >> operators in ifstream/ofstream.

@panqnik what do you mean?
I am sorry if I sound a little dumb but just started programming

Unless it's a binary file where you know the offset to the data you want, the only way to read the pieces you need is to read the whole file then extract the data from that. With text files it's impossible to seek to the data because you never know exactly where it's at.

First read a line of the file into std::string, then use string's find() method to search for the colon. That will give you the position within the string where the colon is found. use substr() to extract the rest of the line and do whatever you want with it.

You might also be able to do that with getline() using the colon as the separator.

std::string str;

ifstream in("filename.txt");
while( getline(in,str,':')
{
    getline(str,'\n');
    int var = atoi(str.c_str());
}
commented: So you're saying that it would be easier in a binary file? +0

If your data file has validated data ... all like this:

500 - 1000 : 29
1000 - 1500 : 0
1500 - 2000 : 0
2000 - 2500 : 0

And, if all that you want is the int value at the end,
(or possibly you want to fill a vector of struct with all the data) :

ifstream fin( "yourFileName.txt" );
if( fin )
{
    string s[5];
    vector< int > myVec;
    while( fin >> s[0] >> s[1] >> s[2] >> s[3] >> s[4] )
    {
        myVec.push_back( atoi( s[4] );
    }
    fin.close();
    // use data in vector
}
else // handle error

@Ancientdragon how do I impliment this in qt ... i mean what functions does qt provide similar to getline and atoi?

Try reading their documentation. I don't use QT, I assumed, maybe wrongly, QT supported standard C++ libraries.

"QT" is just the name given to a set of libraries and related.

The OP is not coding in "QT"; they are coding in C++. I suspect that the OP has downloaded QT Creator and thinks that QT is some kind of separate programming language.

It is not. You're coding in C++. If you absolutely insist on using QT libraries for doing things that the C++ standard libraries can do, take a look at classes such as QFile. You will still be coding in C++, but with additional libraries that are not part of the standard.

I really do recommend that if you're beginning C++, you learn C++ first and then start using other libraries.

@Moschops I come from a consol C++ background ( not much experience but I do know a few things)
The thing is I tried using using the standard C++ libraries but for some reason they didn't work.
I would prefer using Qt libraries just to provide me sense of continuity. :D

What do you mean by they didn't work? You should be able to compile the following program on any standard compliant compiler.

#include <iostream>
#include <string>

int main()
{
    std::string helloWorld = "Hello World!";
    std::cout << helloWorld << std::endl;

    // to pause the program
    std::cin.get();
    return 0;
}

I suspect the reason they didn't work is that you made a mistake. If you want to use an object provided by the QT libraries, QFile might do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.