Initializomg a global variable from a data file

This is a discussion on Initializomg a global variable from a data file within the C++ Programming forums, part of the General Programming Boards category; (**I am sorry for the spelling mistake in the title**) What's the syntax for initializing a global variable with a ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    30

    Initializomg a global variable from a data file

    (**I am sorry for the spelling mistake in the title**)

    What's the syntax for initializing a global variable with a number in a data file?
    I only know how to change the value of a variable from a data file using an ifstream variable and the >> operator. However I can't (or don't know how to) use the >> operator outside of the main function.

    For instance, if the name of the data name is info.dat and it has only one number - 100 in it. How would you initialize a global variable, say it is called a, from info.dat, the data file?

    Thanks.
    Last edited by cybernike; 06-27-2007 at 12:12 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    The same way as if you were reading from the file into any other variable.

    Do you really mean "initialise", or does early assignment also count?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    30
    Quote Originally Posted by Salem View Post
    The same way as if you were reading from the file into any other variable.

    Do you really mean "initialise", or does early assignment also count?
    I really meant "initialize", i.e., assigning a value to the variable when declaring the variable.

    I should have mentioned that the global variable is also a constant variable; therefore, I need to intialize the value together with the declaration of the variable. The trouble is I don't know how to read data from a file outside of the main function since I can't (or don't know how to) use the >> operation. Any idea?

    Thanks.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    Mmm, this seems to work in g++
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    const int foo ( ) {
        ifstream f("foo.txt");
        int i;
        f >> i;
        return i;
    }
    
    const int foovar = foo();
    
    int main( void ) {
        cout << "Value of var = " << foovar << endl;
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    30
    Quote Originally Posted by Salem View Post
    Mmm, this seems to work in g++
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    const int foo ( ) {
        ifstream f("foo.txt");
        int i;
        f >> i;
        return i;
    }
    
    const int foovar = foo();
    
    int main( void ) {
        cout << "Value of var = " << foovar << endl;
        return 0;
    }
    You're the man. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Doing calculations on a data file
    By bassist11 in forum C Programming
    Replies: 2
    Last Post: 03-30-2009, 07:47 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 02:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21