V
V
vvafree2017-10-03 22:49:05
C++ / C#
vvafree, 2017-10-03 22:49:05

C++ How to pick up a variable from a.ini file?

Good evening.
There is a task to get a variable from the a.ini file. There is nothing else in the file other than the value of the variable, just a number.
How can this be implemented? ; does not work.
int a = fopen ("a.ini", "r")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill H, 2017-10-07
@KirillHelm

Well, nothing complicated, let's go the simplest way.

#include <fstream>   //библиотека файловых потоков
#include <string>
using namespace std;

int main()
{
    int value;
    string line;
    fstream* fileStream;

    fileStream = new fstream("c:/file.ini");   //инстанциируем поток и передаем в конструктор путь к файлу
    getline(*fileStream, line);   //читаем строку первую из файла в string
    value = stoi(line);   //конвертируем string to int, тут уже на фаш вкус, можно как угодно конвертировать

    printf("%d\n", value);
    
    system("pause");
    return 0;
}

P.S.
This is the easiest way, it could be harder, just read the file bit by bit and initialize an int like this, but start there. If you are interested in the topic of bitwise reading / writing to a file, then google about file stream flags.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question