How to declare a global input stream

This is a discussion on How to declare a global input stream within the C++ Programming forums, part of the General Programming Boards category; Hi, After many many hours of agonising debugging, I've found I need to declare one of my input streams globally. ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    9

    Question How to declare a global input stream

    Hi,

    After many many hours of agonising debugging, I've found I need to declare one of my input streams globally.

    I'm loading up a .txt file and need to have 2 functions have access to it. I would rather pass a pointer (to the stream) to my function but I'm not very confident the pointers and my brain has been jellyfied.

    So any tips on how to declare it globally, so far I have:

    Code:
    void function()
    {
        char ch;
    
        ifstream instream(buffer);
    
        instream.get(ch);
    
        switch(process_stream(ch))
         {
           case 1:
           case 2:
           case 3:
         }
    }
    Code:
    int process_stream(char ch)
    {
           string word;
           while(instream.get(ch) && isalnum(ch))
           {
                   word += ch;
           }
            return identifier(word);
    }
    Any help for either method would really, really be appreciated

    Pete C

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    To declare it globally, you'd put the declaration outside (and above) the functions that use it.

    A better choice is to pass by reference. The syntax of the reference is the same as an object, so the only part you have to know how to do is the function parameter:
    Code:
    int process_stream(ifstream& instream)
    Note that I took out the char from the parameter list, as it makes no sense there.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    9
    Thanks loads.

    Everything works perfectly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 08:12 PM
  2. flushall()
    By salvadoravi in forum C Programming
    Replies: 21
    Last Post: 12-24-2007, 11:39 AM
  3. Help loading a vector with input from input stream
    By Bnchs in forum C++ Programming
    Replies: 9
    Last Post: 11-07-2006, 02:53 PM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM

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