views:

67

answers:

3

I know its possible to get a part of a .txt, then convert it to an integer, then store it in a variable, but is it possible to to that in a single declaration. (The variable needs to be global).

Ie:

[data.txt]
1020
[convert_data.cpp]
#include<fstream>
fstream convert("data.txt");
//way to declare something equal to A PARTICULAR POINT in data.txt

int main()
{
    //how would I take this block of code and simplify it to two DECLARATIONS (not
    //function calls), or, if that's not possible or not practical, how would I make
    //n and m (or var1 and var2) global AND CONSTANT?
    char var1[5];
    convert.getline(var1,2);
    char var2[5];
    convert.getline(var2,2);
    const int n=atoi(var1);
    const int m=atoi(var2);
    return 0;
}
A: 

From the point of view of the compiler, a constant is something that has a fixed value at compile time. Since you're reading your data from a text file (which can be changed after the program has been compiled), the numbers you read from the text file cannot be constants in your program.

Having said that, you could do something like this:

class ConstInteger {
public:
    ConstInteger(fstream &f) {
        char s[5];
        f.getline(s,2);
        value = atoi(s);
    }
    public operator int() const { return value; }
private:
    int value;
};

fstream convert("data.txt");
ConstInteger n(convert);

The above relies on the initialisation the C++ compiler does for global classes. As a consequence of this, you are largely at the mercy of the C++ compiler with respect to class initialisation order. Also, error handling in this arrangement may be troublesome. (For example, what happens if data.txt does not exist?)

Greg Hewgill
Not entirely true, as the above code had n and m bothe being constants, and both being read from a file. I'm just looking for a way to shorten that into a single declaration, and make it global.
Keand64
I realise that; I gave an example for one integer. You are free to adapt my example to two integers if you like.
Greg Hewgill
Within one translation unit, it is guaranteed by the C++ standard that global constructors are called in the order in which they are declared.
Adam Rosenfield
A: 

No, you can't do it in a single declaration. You can write a function to do that, and then you can initialize local variables with that function, but you can't initialize global variables with it. If you want to initialize global variables, you'll need to explicitly do so during your program's initialization routine, or you could do it in the constructor of a global object (not recommended).

Adam Rosenfield
Actually, the making of a function to use in a global declaration is probable exactly what I need (oxymoron not intended).
Keand64
A: 

If I understand correctly you want to read in part of a text file and store the value as a constant? Well you cannot have an uninitialized constant - so a global declaration to which you assign at runtime.

Perhaps the way to do it is encapsulate that functionality that you have in a global function called say getConstant, this could contain a static variable and the call to getConstant would check if the var has been read from the file, if it has then return the constant, if not read in from the file. Lazy evaluation. Of course this doesn't solve the issue of it needing (?) to be constant.

Stinomus