Title: How do I read a text file into to my main function and store it in an array?
Tags: c++
Question: I made a text file in my function I created and What I'm trying to do is read that text file in the main function then store the text file into an array. After I figure out how to do both I will compare the text file answers with the correct answers.

```#include <iostream&gt;
#include <fstream&gt;
#include <string&gt;

using namespace std;

void Get_Answers();

int main()
{   
    const int NUM_ANSWERS = 20;
    char answers[NUM_ANSWERS] = { 'A', 'D', 'B', 'B', 'C', 'B', 'A', 'B', 'C', 'D', 'A', 'C', 'D', 'B', 'D', 'C', 'C', 'A', 'D', 'B' };
    string textName;

    //Fuction for the users answers
    Get_Answers();

    ifstream textIn;
    textIn.open(textName);

    if (textIn.fail())
    {
        cout << "\nFile not found; program will end";
        return (0);
    }

    cout << endl << endl;
    system("pause");
    return(0);
}

void Get_Answers()
{
    const int NUM_ANSWERS = 20;
    char userAnswers[NUM_ANSWERS];
    string textName;

    cout << "\nEnter a text file name ";
    cin &gt;&gt; textName;
    ofstream textFileOut;
    textFileOut.open(textName);

    for (int i = 0; i < NUM_ANSWERS; i++)
    {
        cout << "\nEnter the test answers ";
        cin &gt;&gt; userAnswers[i];

        textFileOut << userAnswers[i] << '\n';
    }
    textFileOut.close();
}
```

Comment: How do I read that text file I made in the "Get_Answers" function to the main function.... or should I say how do I input the text file into my main function?

Comment: Ok... so what's the question?


Here is another answer: Ok, judging by what you wrote as your question and by my debug results, the problem you have is that you correctly read the file inside the function, but you don't pass this data outside the Get_Answers() function.

I think the C++ way of doing this would be:

1) Initialize a pointer to empty char array in main() function:

```char* userAnswersPointer = new char[NUM_ANSWERS];
```

2) Parametrize your Get_Answer() function to pass a pointer to this array:

```void Get_Answers(char* userAnswers_Ptr, int N_ANS) {

//...

for (int i = 0; i < N_ANS; i++)
    {
        cout << "\nEnter the test answers ";
        cin &gt;&gt; userAnswers_Ptr[i];

        textFileOut << userAnswers[i] << '\n';
    }

//...

}
```

3) Use it properly in main():

```int main() {

    //...

    Get_Answers(userAnswersPointer, NUM_ANSWERS);

    //...

    return 0;
}
```

I didn't compile it, just wrote out of my head, so feel free to check this out. It should give you an idea how this should be done.

Comment for this answer: @simplegamer: If any of the answer helped you, vote it up or mark it as an answer so the other users, who may stumble upon this question could easier get to the correct answer of your question.

Here is another answer: You can do it following this code:

```  st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16ifstream is ("test.txt", st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16ifstream181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16inary);
  if (is) {
    // get length of file:
    is.seekg (0, is.end);
    int length = is.tellg();
    is.seekg (0, is.beg);

    char * buffer = new char [length];

    st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16cout << "Reading " << length << " characters... ";
    // read data as a block:
    is.read (buffer,length);

    if (is)
      st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16cout << "all characters read successfully.";
    else
      st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16cout << "error: only " << is.gcount() << " could be read";
    is.close();

    // ...buffer contains the entire file...

    delete[] buffer;
  }
```

Found this snippet of code here: http://www.cplusplus.com/reference/istream/istream/read/

Replace "test.txt" with the name of your file.  However your main code has no way of knowing what the file name is created in Get_Answers().


Here is another answer: If you mean store each line to an array, a for loop and getline should work well.

Edit: judging by your previous answer, I would say yes this is how to do it.

To elaborate a little more, a simple way of doing this would be to make a for loop to count how many lines there are, then go to the beginning of the file and make another for loop: for(int i = 0; i != lines; i++) 

Then in the for loop make it array[i] = getline(file,string);

You can use while(!file.EOF()) instead of the for loop and the while loop. I just prefer getting a line count for some reason.

Comment for this answer: Basically the user enters 20 answers in the Get_answers function. The answers are then written on to a text file. My problem is idk how to get the text file answers into my main function. Sorry if I'm confusing anyone I'm a beginner.
Comment for this answer: Well one thing you can do then if you want to compare is save both to files then open both and save both to arrays using the information  I posted. After that, you can make another for loop that uses the counter data and compares the elements of the array. For example, If arr[i] != arr1[i], the answer is wrong.

Maybe have a variable for wrong answers and right answers and each time it is wrong, it increases wrong, otherwise it increases right.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:04/29/15', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<c++>', 'Title': 'How do I read a text file into to my main function and store it in an array?', 'ViewCount': 339, 'id': 29951273, 'user_AboutMe': None, 'user_AccountId': 6230613.0, 'user_DisplayName': 'simplegamer', 'user_Reputation': 13.0, 'user_Views': 1.0, 'user_id': 4847523.0}}