0

i am trying to pass an array between two functions. The _data function is my array. It takes 3 parameters; the array itself, the size of the array and the file name of the array. So this function should get each element from the input file(.txt) and put it into the array, which it seems to do ok.

void _data(string dataArray[], int sizeOfArray, ifstream &fin)
{
    const int CharBuffer=20;
    char linedata[CharBuffer];
    ifstream& getline (char* s, streamsize n );

    for(int x = 0; x < sizeOfArray; x++)
    {
        fin.getline(linedata, CharBuffer);
        dataArray[x]=linedata;
        cout << dataArray[x] << endl;
    }
}

ifstream& operator>>(ifstream &fin, ArrayIntStorage &AIS)
{
    string acwData[10000];
    _data(acwData, 10000, fin);
    return fin;
}

but in the next part I need to output the values from my array to a text file which I must do in a separate function.

So my question is, how can I access the data stored in the acwData array above in the function below?(or any other function)

ofstream& operator<<(ofstream& fout, ArrayIntStorage& AIS)
{
    return fout;
}

do i need to initialize my array function differently to include a return type?

6
  • 1
    What is the ArrayIntStorage& AIS parameter? Is it relevant to your question? Commented May 1, 2011 at 17:09
  • @quamrana no it's not, that is for a different function which I still need to implement Commented May 1, 2011 at 17:11
  • @Marc: I asked because I suspected that the _data function should write its input data into AIS and that would be the way of transferring it to 'any other function'. Commented May 1, 2011 at 17:17
  • @quamrana ok that sounds right. So how would I get the _data function to write the data into a parameter such as AIS? Commented May 1, 2011 at 17:26
  • @Marc: Make AIS a parameter of _data and write, say, an add member for ArrayIntStorage which takes linedata. Commented May 1, 2011 at 17:32

1 Answer 1

1

Several things wrong with this code:

 void _data(string dataArray[], int sizeOfArray, ifstream &fin)

Names that begin with an underscore are reserved for the C++ implementation in most circumstances - don't use them in your own code unless you have read the C++ Standard fairly closely, which I suspect you have not.

 ifstream& getline (char* s, streamsize n );

Don't declare standard library functions yourself. This function is declared in <iostream> so you should #include that header.

The use of an array is suspect. As you are obviously using C++ and the standard library, why not use a vector<string> which you should pass into your function as a reference.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.