Hi i'm pretty new to C++ (well very new!) and i need to read the information from a text file into an array. The kind of data i'll be using is a simple list of names. This is my first attempt at;

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () 
{
  string array[5];                          // creates array to hold names
  short loop;                               //short for loop for input
  short loop2;                               //short for loop for output
  string line;                              //this will contain the data read from the file
  ifstream myfile ("example.txt");          //opening the file.
  if (myfile.is_open())                     //if the file is open
  {
    while (! myfile.eof() )                 //while the end of file is NOT reached
    {
      getline (myfile,line);                //get one line from the file
      cout << line <<  endl;                //and output it
        for (loop=0; loop<5;loop++)         //criteria for input loop (as array 0-5)
      {
      array[loop]=line;                     //output loop statement
      };
        for (loop2=0; loop2<5;loop2++)      //criteria for output loop (as array 0-5)
      {
      cout << array[loop2];                     //loop statement
      };
    }
    myfile.close();                         //closing the file
  }
  else cout << "Unable to open file";       //if the file is not open output 
  system("PAUSE");
    

       system("PAUSE");
  return 0; 
}

The data appears in a file that looks like this:
john
dave
paul
jack
adam

Thanks for your time.

Recommended Answers

All 6 Replies

Member Avatar for GreenDay2001

Use [ code ] [/ code ] (without spaces) tags to post your code.

Is this what you want....
You can simultaneously read line and put it into array[] and line, so you dont need two loops.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
    string array[5]; // creates array to hold names

    short loop=0; //short for loop for input

    string line; //this will contain the data read from the file
    ifstream myfile ("example.txt"); //opening the file.
    if (myfile.is_open()) //if the file is open
    {
        while (! myfile.eof() ) //while the end of file is NOT reached
        {
            getline (myfile,line); //get one line from the file
            array[loop] = line;
            cout << array[loop] << endl; //and output it
            loop++;
        }
        myfile.close(); //closing the file
    }
    else cout << "Unable to open file"; //if the file is not open output

    system("PAUSE");
    return 0;
}

Thats it! Thanks alot Vishesh!

Problem #1:

while (! myfile.eof() )     //while the end of file is NOT reached

See this ( .eof() is identical to feof() Problem #2:

system("PAUSE");

See this

Vishesh, like Walter said, don't use EOF or you just end up writing an additonal condition to prevent the fetching of the last record, one too many times.

Instead use, while ( getline (file_stream, my_string ).good () ) And as for system ( "PAUSE" ) read the article posted.

#include <iostream> //For Input/Output
#include <fstream> //For File Input/Output
#include <string> //For Saving The Line Into The Array
using namespace std;
int main ()
{
	string array[5]; //Array to save the line on
	int loop=0; //Index of array
	int loop2;
	string line; //The line taken from the *.txt source
	ifstream myfile ("example.txt"); //To read from the *.txt File
	if (myfile.is_open()) //Checking if the file can be opened
	{
		while (! myfile.eof() ) //Runs while the file is NOT at the end
		{
			getline (myfile,line); //Gets a single line from example.txt
			array[loop]=line; //Saves that line in the array
			loop++; //Does an increment to the variable 'loop'
		}
		myfile.close(); //Closes the file
	}
	else cout << "Unable to open file"<<endl; //Gives that sentence if the file can't be opened
	for(loop2=0;loop2<=loop;loop2++) //For loop make to cout the lines stored
		cout<<array[loop2]<<endl;    //inside of the variable 'array'
	return 0;
}

Your code made the 5 lines as output on the command prompt, but only the first line was saved in the variable array.

My code as I edited yours, saves the 5 lines from the file into the array and then it outputs them. Hope you benefit from this.

=EDIT= I'm sorry, I didn't see that you posted a corrected code before me. I didn't continue the rest of the thread. So sorry :)

hiii guys im really new about c++ i got a lot of problem...
and i've an home work how to open a text in array ???
and the out should be like this :

you
he
she
him
her
continue until 50 lines....

help please ?????????

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.