Reading from text file into array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 7
Reputation: stanwaka is an unknown quantity at this point 
Solved Threads: 0
stanwaka stanwaka is offline Offline
Newbie Poster

Reading from text file into array

 
0
  #1
Mar 6th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: Reading from text file into array

 
0
  #2
Mar 6th, 2007
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 7
Reputation: stanwaka is an unknown quantity at this point 
Solved Threads: 0
stanwaka stanwaka is offline Offline
Newbie Poster

Re: Reading from text file into array

 
0
  #3
Mar 6th, 2007
Thats it! Thanks alot Vishesh!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Reading from text file into array

 
0
  #4
Mar 6th, 2007
Problem #1:
  1. while (! myfile.eof() ) //while the end of file is NOT reached
See this (.eof() is identical to feof()


Problem #2:
  1. system("PAUSE");
See this
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Reading from text file into array

 
0
  #5
Mar 6th, 2007
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1
Reputation: hannad is an unknown quantity at this point 
Solved Threads: 0
hannad hannad is offline Offline
Newbie Poster

Re: Reading from text file into array

 
0
  #6
Dec 10th, 2008
#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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC