Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading variables from file 2

Status
Not open for further replies.

math

Programmer
Mar 21, 2001
56
0
0
BE
Hi,

Is there a good way to read variables from a file...
Maybe a file like:

var1=tekst1
var2=tekst2
...

and use the variables var1 and var2 in the program with the values tekst1 and tekst2 ? And if this is possible can I read arrays in? maybe like array1=(bla,bla,bla) ??

Thanx to anyone who can help...
math
 
Hi.

A lot of whether you can do that depends on (1) the type of file and (2) the type of data you're storing in the file. If you're using a regular .txt file, like you would open up in notepad, then the code is pretty easy. Actually, you can open and close just about any type of file through <fstream.h>, but I'm going to assume a .txt.

The other thing you have to worry about is the type of data. If you use C++ to store the integers to the file, it defaults to a \n-delimited list; that is,

5
6
7

would be the contents of the file. The code to read this from a file into three variables would be something like this:

Code:
#include<fstream.h>

int main()
{
   int var1, var2, var3;
   ifstream filevar;
   filevar.open(&quot;path\filename&quot;); 
     // path is not required if
     // file is in same directory
     // as .exe
   filevar>>var1;
   filevar>>var2;
   filevar>>var3;
   filevar.close();
   return 0;
}
or, if you had an array of type int:
Code:
int vars[3];

for(int i=0; i<3;i++)
{
  filevar>>vars[i];
}
Both of these assume that the values were stored in the .txt as I showed above. If this doesn't answer your question, let me know and I'll see if I can help you out more.

benlinkknilneb
 
Thanx benlinkknilneb,

that's a really good start for me !!
But the problem is that the file I need to read is a file that should and could be changed by the user... sort of a configuration-file... And for that, I think It would be usefull if the file contains the variable-names aswell... Each integer and/or text should have a name so that the user knows what it stands for... Or that's the idea anyway :) The program should be able to read it, but so does the user, and that's my problem... I don't know how to make it readable for both :(

Thanx anyway !!
math
 
Well, C++ will automatically handle the variable type... that is, if you want to read in text, just change the variable type that you're reading it into...

Code:
char x;

filevar>>x;

will pull a single character from the file stream.

The way I read your problem is like this; the text file looks something like

Code:
NAME: Joe Smith
USERID: jsmith
PASSWORD: asdfgh

My values are just for example, but I think that's the general format you're talking about.

What you'll probably want to do is read in a line at a time and parse it (lots of work, but effective). The command to get the line is as follows:

Code:
filevar.getline(buffer,int,char);

where buffer is a character array, int is an integer value for the maximum number of characters, and char is the delimiter... that is, getline inputs to the buffer array either &quot;int&quot; times or until it reads a character &quot;char&quot;, whichever comes first. After this, you can sort out the characters that are in the file, and take the information you need.

As for the fact that users can change the values... that's certainly a problem with this method. My personal philosophy is that if they change a data file and it hangs the program, it's their fault. I'm not writing high-security stuff or anything, just run-of-the-mill kinds of programs, so I let the burden fall on their shoulders instead. If you don't feel brave enough to try parsing the data out of a buffer, you might consider including a readme or something that tells them how to change it.
 
If I'm reading your question correctly, you want to be able to do something like this:

Text File:
Code:
var1=7
var2=25

Source File:
Code:
#include <iostream>
#include <fstream>

int main()
{
    /* read in data from file */
    std::cout << var1 + var2 << std::endl;

    return 0;
}

If that's the case, then no, that's not going to work. The variables need to be named at compilation time, not execution time.

What I'd suggest is that you make a class that emulates a symbol table (i.e. holds variable names and addresses) for your data file.

You would initialize a SymbolTable by having it read your data file and store the names of the variables along with their line number (&quot;address&quot;). You might also want it to store a value for each variable, too, so you don't have to keep looking back at the data file to get it.

You could then have code that works like this:

Text File:
Code:
var1=7
var2=25

Source File:
Code:
#include <iostream>
#include <fstream>
#include &quot;symbtab.h&quot; // for symbol table

int main()
{
    ifstream fin( &quot;datafile.txt&quot; );
    SymbolTable st( fin );  // read in data

    std::cout << st.get( &quot;var1&quot; ) +   // get data
                 st.get( &quot;var2&quot; )     // ditto
              << std::endl;

    return 0;
}

If any changes are made to the variables during the program, you could change them immediately, wait until a &quot;save&quot; command is called, or wait until your symbol table is being destroyed to write the data to the file. I suppose it depends on what you're doing and how often these changes will be made.

There are obviously better and easier ways to implement the class, but this was just to make it look as much like what you're trying to do as possible. If you get the general idea and this is something like what you need, you'll probably want to modify it to something that's easier to make.

If, on the other hand, I didn't understand your question correctly, then I've wasted enough of your time without really helping, so I'll just stop now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top