• Register
3 votes



This is a novice of c++ programming. I am trying to take input some data from a text file and store it into an array. So, my question is very simple, “How to open a text file in c++ and input into an array

Your effort and solution is must apprecitaed.

15.3k points
reopened by

Please log in or register to answer this question.

1 Answer

1 vote
Best answer


Hello Gavin,
This is not a noob question at all but an interesting one. Yeah, you take input from a text file and save it into an array.

I am trying to shed some light on this but let me show you the sample code first:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
    string array[2]; 
    short loop=0; 
    string line; 
    ifstream myfile ("Test.txt"); 
    if (myfile.is_open()) 
        while (! myfile.eof() ) 
            getline (myfile,line); 
            array[loop] = line;
            cout << array[loop] << endl; 
    else cout << "can't open the text file"; 
    return 0;

Well, here it is. Now, let's try to decode our program. The three header files are used for the following:

input-output stream, input, and output operations and use to create files, and saving the line into the array.

Dive into the main function, we created a string array to hold the names inside the array and the string line will store the data from the text file up to two values using the for-loop. 

I hope, this little explanation will help you to get the program well. 

94.3k points
selected by

Related questions

0 votes
1 answer 63 views
Extract information from text file in C ++ I have to read a text file with the following structure AREA I: PHYSICS MATHEMATICS AND EARTH SCIENCES ♣ 1-12, i-60,100,300, i-90, AREA II: BIOLOGY AND CHEMISTRY ♣ 123-8,30-80, The number of folios is variable after the clover, each one is separated ... fol (folio, 0, folio.length () - cont2);      indSec [1] [cont2] .folio = fol;      cont2 ++;    } }
asked Aug 15, 2020 game 4.6k points
0 votes
1 answer 203 views
I have to read a file that has this format: Id: 23 Name: Roberto Last Name: My Last Name Id: 24 Name: Roberto2 Last Name: My Last Name2 And I have to read that file to be able to work with that data but I don't know how to read it. I have a struct created as follows: struct Data {      int Id;      string Name;      string Surname; }; How do I go about having that data in an array?
asked Aug 17, 2020 game 4.6k points
0 votes
1 answer 54 views
Problem: My code asks for data and fills it to the array. Then check if they are even. And it should print the data which is even. The data filling if you do it correctly. But at the time of printing I only get this - The number 0 in position [0] is even #include <iostream> using namespace std; int ... { cout << "The number" << * p ++ << "in position [" << i << "] is even \ n"; } } return 0; }
asked Dec 5, 2020 sasha 16.2k points
0 votes
1 answer 41 views
Problem: The program deals with an ATM, which has 5 options: 1 check balance. 2 Withdraw money. 3 Deposit money. 4 show last 5 movements. 5 exit. My problem is in the last 5 movements, since I must save the data, that is, the inquiries withdrawals and deposits that the user makes, with ... in a moment ...." << endl; Sleep (10000); system ("cls"); menu_of_operations (); break; } } while (opc <5); }
asked Nov 5, 2020 sasha 16.2k points
0 votes
1 answer 55 views
How do I delete / empty a C ++ array? There is , but it seems like it's only C ++ 11? I am using VC ++ 2010. How do I empty it (reset to all 0)?array::fill
asked Aug 31, 2020 sasha 16.2k points
0 votes
1 answer 21 views
Problem How can i read number from a file in c++
asked Mar 26 Subhendu 4.5k points
0 votes
1 answer 35 views
I am entering passing a char to an array of char and when inserting it does it well, because I enter and print everything little by little. Then I made another for to show only the arrangement and it turns out that it only awaits the last 5 letters of my char in everything. It is that the arrangement is not maintained. ... {         Phrase [i] = '*';         ++ i;     }     Phrase [i] = '\ 0'; }
asked Aug 16, 2020 game 4.6k points
0 votes
1 answer 30 views
I have a simple C ++ program, I just want to add some numbers several times between them but the problem is that it throws me this error: [program_name] .exe has triggered a breakpoint. And the program code is this: #include <iostream> int main () {     char key = ... if I didn't do it before. PS: I do not ask for anything in terms of code optimization, that surely there will be details that yes.
asked Aug 15, 2020 game 4.6k points
0 votes
1 answer 34 views
I have the following code: struct person {      string name;      int AmountMonth [5] = {0}; }; And I get the following warnings: [Warning] non-static data member initializers only available with -std = c ++ 11 or -std = gnu ++ 11 [Warning] extended initializer lists ... -std = gnu ++ 11 And mark the line where is int AmountMes [5] = {0}; I've already tried everything and can't solve anything.
asked Aug 15, 2020 game 4.6k points
0 votes
1 answer 41 views