I want to know how to seek to a point in a file.
Example I have a database program with a text file named "info.txt"
There are two lines in it:
Password = user makes password
Activated = true/false

How do I make the program find if they have activated the program yet, and then find the password.

Thanks for your help.

Dani AI

Generated

As suggested, reading the file line-by-line is the simplest, and was right to point out the equals sign as the delimiter — but pointer arithmetic is brittle and error-prone. A safer, clearer pattern is: read each line, ignore blanks/comments, split at the first = and trim whitespace. Store results in a small key→value map so lookups (password, activation flag, etc.) are trivial and robust.

A compact, practical implementation idea:

#include <fstream>
#include <string>
#include <unordered_map>
#include <algorithm>

static inline std::string trim(const std::string& s) {
    auto b = s.find_first_not_of(" \t\r\n");
    if (b == std::string::npos) return "";
    auto e = s.find_last_not_of(" \t\r\n");
    return s.substr(b, e - b + 1);
}

std::unordered_map<std::string,std::string> parseKeyValueFile(const std::string& path) {
    std::unordered_map<std::string,std::string> m;
    std::ifstream in(path);
    std::string line;
    while (std::getline(in, line)) {
        auto comment = line.find('#');
        if (comment != std::string::npos) line.erase(comment);
        if (line.find_first_not_of(" \t\r\n") == std::string::npos) continue;
        auto eq = line.find('=');
        if (eq == std::string::npos) continue;
        auto key = trim(line.substr(0, eq));
        auto val = trim(line.substr(eq + 1));
        m[key] = val;
    }
    return m;
}

Once parsed, fetch Password from the map and convert Activated to a bool by lowercasing the value and checking "true", "1", or "yes". Handle missing keys by checking existence first.

Notes and pitfalls: treat keys case-insensitively if the file may vary; trim CR/LF on Windows; allow comment characters; and avoid seekg for simple text parsing — it only helps if you build and persist a byte-offset index for fast random access in very large files.

Recommended Answers

All 4 Replies

if your file looks just like the example you give, why not read each line into separate strings. If the second string has the value you interpret as activated, then use the password found in the first.

Thanks for your help,
How exactly would you do that.
I am new to finding parts of files.
Could you post a little example, thanks.

"finding parts of files."

No, not finding parts of the file but read the whole file, while
analyzing it in the process.

Assuming the file is the something like this :

Info.txt
---------------------------------------------
Password = hello_world
Active t = false
---------------------------------------------
1) open up a file with fstream
2) read first line into string
3) increment the string until it reaches the equal sign, then the password something like this :

while( *stringVariable++ != '=' ) ; //increment until the = sign
while( ! (isalpha (*stringVariable++) ) ); //increment untill you reached the first letter in the password.

4) Now you have got the password. save it.
5) read the next line. do the same process like in # 3
6) compare stringVariable to flase or true. Set bool variable
accordingly.

7) Continue with the rest of your program.

Thanks I will try that, it makes sense now.

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.