• Register
3 votes
40 views

Problem:

Hello,

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.

12 7 7
15,250 points

1 Answer

1 vote
 
Best answer

Solution:

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; 
            loop++;
        }
        myfile.close(); 
    }
    else cout << "can't open the text file"; 
    system("PAUSE");
    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. 

13 9 6
94,240 points

Related questions

0 votes
1 answer 8 views
8 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 game 4.3k points
0 votes
1 answer 5 views
5 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 game 4.3k points
0 votes
1 answer 14 views
0 votes
1 answer 12 views
12 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 sasha 1.2k points
0 votes
1 answer 22 views
22 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 game 4.3k points