-
C++ (
http://www.daniweb.com/forums/forum8.html)
| stanwaka | Mar 6th, 2007 6:03 am | |
| Reading from text file into array 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. |
| vishesh | Mar 6th, 2007 6:32 am | |
| 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.
#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;
} |
| stanwaka | Mar 6th, 2007 6:36 am | |
| Re: Reading from text file into array Thats it! Thanks alot Vishesh! |
| WaltP | Mar 6th, 2007 12:18 pm | |
| Re: Reading from text file into array 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 |
| ~s.o.s~ | Mar 6th, 2007 12:47 pm | |
| 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. |
| hannad | Dec 10th, 2008 8:41 am | |
| 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 :) |
| All times are GMT -4. The time now is 10:32 am. | |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC