| | |
Reading from text file into array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2007
Posts: 7
Reputation:
Solved Threads: 0
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;
The data appears in a file that looks like this:
john
dave
paul
jack
adam
Thanks for your time.
c Syntax (Toggle Plain Text)
#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.
Last edited by WaltP; Mar 6th, 2007 at 12:13 pm. Reason: Added CODE tags. Read the words on the input box background
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.
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)
#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; }
Last edited by vishesh; Mar 6th, 2007 at 6:34 am.
Problem #1:
See this (
Problem #2:
See this
C++ Syntax (Toggle Plain Text)
while (! myfile.eof() ) //while the end of file is NOT reached
.eof() is identical to feof()Problem #2:
C++ Syntax (Toggle Plain Text)
system("PAUSE");
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
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,
And as for
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.
•
•
Join Date: Dec 2008
Posts: 1
Reputation:
Solved Threads: 0
#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.
![]() |
Similar Threads
- Reading numbers from a text file (Java)
- Help with a 2D array from a text file (C++)
- Need Help in Reading characters from a text file (C++)
- Inputting text file data into an array, please help! (C++)
- Help Reading Info in Text File Into an Array (C++)
Other Threads in the C++ Forum
- Previous Thread: error C2259: 'ClassName' : cannot instantiate abstract class
- Next Thread: Help With Making A High Scores Table
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






