Thread: Help please!

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    142

    Help please!

    How do i pass array to int main?


    cheers.


    Code:
    char openR(){
                           
                           
                           ifstream filein;
                           filein.open("stores.ddf");
                           
                           if (!filein) {
                                        
                                        cout<<"\file did not open";
                                        return 0;
                                        
                                        }
                           
                           filein.seekg(0,ios::end);
                           len=filein.tellg();
                           filein.seekg(0,ios::beg);
                           
                           filein.read(array,len);
                           
                           filein.close();
                           
                           return (array[256]);
                           
                           
                          };
    it compiles fine but i just can't see the content of array in int main.
    Last edited by wart101; 01-12-2007 at 12:22 AM.
    WhAtHA hell Is GoInG ON

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    create array (or vector) in main
    and pass it by ref into openR
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Quote Originally Posted by vart
    create array (or vector) in main
    and pass it by ref into openR
    yes ofcourse thankyou.
    WhAtHA hell Is GoInG ON

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    You seem to be passing a char of some kind. You need a container.
    Perhaps you want the array to be accessible in main()?
    Code:
    container_type & openR();
    //return reference to your array
    
    int main()
    {
      container_type my_array(openR());
      //use my_array
    }
    so now...
    Code:
            
    container_type & openR() {
                           
                           container_type ret_val;
                           ifstream filein;
                           filein.open("stores.ddf");
                           
                           if (!filein) {
                                        
                                        cout<<"\file did not open";
                                        //return 0;
                                            //can't return 0 anymore.
                                            //either return a blank array and check for it in main
                                            //or throw an exception
                                        }
                           
                      //...................
                           
                           return ret_val;
                           
                           
                          };
    Personally, I'd use std::string. So you could do your reading operations on
    Code:
    char* array = new char[len];
    and then have
    Code:
    std::string ret_val(array);
    And right before you
    Code:
     return ret_val;
    DON'T FORGET to
    Code:
    delete [] array;
    Hope that helps.
    Last edited by CodeMonkey; 01-12-2007 at 12:36 AM. Reason: clarification
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed