Help please!

This is a discussion on Help please! within the C++ Programming forums, part of the General Programming Boards category; How do i pass array to int main? cheers. Code: char openR(){ ifstream filein; filein.open("stores.ddf"); if (!filein) { cout<<"\file did ...

  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-11-2007 at 11:22 PM.
    WhAtHA hell Is GoInG ON

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    create array (or vector) in main
    and pass it by ref into openR
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    902
    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-11-2007 at 11:36 PM. Reason: clarification
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

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