944,052 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 31674
  • C++ RSS
Mar 6th, 2007
0

Reading from text file into array

Expand Post »
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;
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8. string array[5]; // creates array to hold names
  9. short loop; //short for loop for input
  10. short loop2; //short for loop for output
  11. string line; //this will contain the data read from the file
  12. ifstream myfile ("example.txt"); //opening the file.
  13. if (myfile.is_open()) //if the file is open
  14. {
  15. while (! myfile.eof() ) //while the end of file is NOT reached
  16. {
  17. getline (myfile,line); //get one line from the file
  18. cout << line << endl; //and output it
  19. for (loop=0; loop<5;loop++) //criteria for input loop (as array 0-5)
  20. {
  21. array[loop]=line; //output loop statement
  22. };
  23. for (loop2=0; loop2<5;loop2++) //criteria for output loop (as array 0-5)
  24. {
  25. cout << array[loop2]; //loop statement
  26. };
  27. }
  28. myfile.close(); //closing the file
  29. }
  30. else cout << "Unable to open file"; //if the file is not open output
  31. system("PAUSE");
  32.  
  33.  
  34. system("PAUSE");
  35. return 0;
  36. }

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

Thanks for your time.
Last edited by WaltP; Mar 6th, 2007 at 12:13 pm. Reason: Added CODE tags. Read the words on the input box background
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stanwaka is offline Offline
7 posts
since Mar 2007
Mar 6th, 2007
0

Re: Reading from text file into array

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.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8. string array[5]; // creates array to hold names
  9.  
  10. short loop=0; //short for loop for input
  11.  
  12. string line; //this will contain the data read from the file
  13. ifstream myfile ("example.txt"); //opening the file.
  14. if (myfile.is_open()) //if the file is open
  15. {
  16. while (! myfile.eof() ) //while the end of file is NOT reached
  17. {
  18. getline (myfile,line); //get one line from the file
  19. array[loop] = line;
  20. cout << array[loop] << endl; //and output it
  21. loop++;
  22. }
  23. myfile.close(); //closing the file
  24. }
  25. else cout << "Unable to open file"; //if the file is not open output
  26.  
  27. system("PAUSE");
  28. return 0;
  29. }
Last edited by vishesh; Mar 6th, 2007 at 6:34 am.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Mar 6th, 2007
0

Re: Reading from text file into array

Thats it! Thanks alot Vishesh!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stanwaka is offline Offline
7 posts
since Mar 2007
Mar 6th, 2007
0

Re: Reading from text file into array

Problem #1:
C++ Syntax (Toggle Plain Text)
  1. while (! myfile.eof() ) //while the end of file is NOT reached
See this (.eof() is identical to feof()


Problem #2:
C++ Syntax (Toggle Plain Text)
  1. system("PAUSE");
See this
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Mar 6th, 2007
0

Re: Reading from text file into array

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.
Last edited by ~s.o.s~; Mar 6th, 2007 at 12:47 pm.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Dec 10th, 2008
0

Re: Reading from text file into array

#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
Last edited by hannad; Dec 10th, 2008 at 8:57 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hannad is offline Offline
1 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: error C2259: 'ClassName' : cannot instantiate abstract class
Next Thread in C++ Forum Timeline: Help With Making A High Scores Table





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC