Read File .txt in automatic way

Dear All,

i have a problem.
I have a file .txt as in this example:

A = 10;
pippo = 3.2;
nome = TOPOLINO;

So i have in this file variables (A, pippo, nome ...etc) and value (10, 3.2, TOPOLINO .... etc).
So i want create a function that get as input name or names of variables and get out in video the value o values of variables.

Can you help me? Thanks

Francesco
Is what you want something like this?
1
2
3
ifstream MyFile("myfile.txt");
ReadVariable("nome",MyFile);// <- to be developed
//output: TOPOLINO 

ReadVariable Algorithm:
- Try to find the variable name provided
- Check if it has assigned a value
- Read/Print the value
Last edited on
Assuming that you are coding in a Windows Environment, you may want to take a look at these API functions that parse .ini(config) files quite nicely.

1.) GetPrivateProfileString()
http://msdn.microsoft.com/en-us/library/ms724353(VS.85).aspx

2.) GetPrivateProfileInt()
http://msdn.microsoft.com/en-us/library/ms724345(VS.85).aspx

Here is the basic syntax for a .ini file:
1
2
3
4
5
6
7
[section1]
key1=value1
key2=value2

[section2]
key1=value1
key2=value2


So by passing these functions a section and a key pair, you can retrieve the value into a buffer. Plus it's already written for you. I have used this several times and the only complaint that I have is that it requires a full file path.
(C:\Folder\file.ini instead of file.ini)

Hope the project works out, and have fun with the API

SigmaSecurity
Last edited on
You could read a line at a time from the text file, parse the line into two strings: name and value, and store them in a map.

Later, to retrieve the value associated with "key", you could just:
string value = yourMap["key"];
It's so easy~ You have no necessary to care this detail.

GetPrivateProfileString()

GetPrivateProfileInt()

enough~
Topic archived. No new replies allowed.