| | |
Extract Lines of text from File
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 147
Reputation:
Solved Threads: 7
Hi
My program is rather simple, or I thought it would be rather simple
My program reads a text file & saves each line of text into an array called buffer.
The problem is; each line of text is not in a separate array, the whole text file is in one array.
Any suggestions on how to save each line of text in a different array element?
My program is rather simple, or I thought it would be rather simple

My program reads a text file & saves each line of text into an array called buffer.
The problem is; each line of text is not in a separate array, the whole text file is in one array.
Any suggestions on how to save each line of text in a different array element?

C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> #include <string> using namespace std; int main () { int counter = 0; char* buffer[999]; ifstream myfile ("music.txt.windows"); while (myfile) { myfile.getline(buffer[counter],'\n'); counter++; } cout << counter << endl << endl; for (int i=0; i< counter; i++) { cout << buffer[i] << endl; } return 0; }
You should read about getline here, http://www.cplusplus.com/reference/i...tream/getline/
Personally, I'd use std::string-s and getline...
Personally, I'd use std::string-s and getline...
cpp Syntax (Toggle Plain Text)
#include <string> #include <fstream> #include <iostream> int main() { std::ifstream inFileStream( "filename.txt" ); std::string line, file; while( std::getline(inFileStream,line) ) file += line + "\n"; std::cout<< file; return 0; }
•
•
Join Date: Apr 2009
Posts: 147
Reputation:
Solved Threads: 7
Thanks but I am trying to save each line in its own individual array element with the code below, it saves each character in its owns element.
Any advice how to put each line in its own array element?
Any advice how to put each line in its own array element?
•
•
•
•
You should read about getline here, http://www.cplusplus.com/reference/i...tream/getline/
Personally, I'd use std::string-s and getline...cpp Syntax (Toggle Plain Text)
#include <string> #include <fstream> #include <iostream> int main() { std::ifstream inFileStream( "filename.txt" ); std::string line, file; while( std::getline(inFileStream,line) ) file += line + "\n"; std::cout<< file; return 0; }
>Any advice how to put each line in its own array element?
Not really but it would be better to apply brains and re-factor the code provided by twomers.
Declare an array of N std::strings and then iterate with a forloop to read one line by one with help of std::getline.
Try it out yourself first. If it doesn't work, come back here with the source-code of what you tried.
Not really but it would be better to apply brains and re-factor the code provided by twomers.
Declare an array of N std::strings and then iterate with a forloop to read one line by one with help of std::getline.
Try it out yourself first. If it doesn't work, come back here with the source-code of what you tried.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
•
•
Join Date: Apr 2009
Posts: 147
Reputation:
Solved Threads: 7
Thanks for the help so far guys but I am really hitting a wall with this. This isn't homework by the way, it just for a program I would like to make, a random playlist maker, & I've done this before but now its not working.
As per siddhant3s said I have given it another go. My code below I believe should work but its the exact same occurence, all of the text gets stored in one array element, not each line in an element
Any advice would be really helpful
As per siddhant3s said I have given it another go. My code below I believe should work but its the exact same occurence, all of the text gets stored in one array element, not each line in an element
Any advice would be really helpful
C++ Syntax (Toggle Plain Text)
#include <string> #include <fstream> #include <iostream> using namespace std; int main() { std::ifstream inFileStream( "music.txt.windows" ); std::string line, file[250]; while( std::getline(inFileStream,line) ) { for (int i=0; i<67; i++) { file[i] = line + "\n"; } } for (int i=0; i<67; i++) { cout << file[i] << endl; } cout << endl << endl << file[4]; return 0; }
Last edited by gretty; Jul 20th, 2009 at 12:01 am.
Have a close look on what are you doing.
In the loop at line 12, you read each line in one iteration to the std::string line.
Now you iterate a for loop nested inside the while loop and assign all the element of file[] the same content: line
You do not need two loops. You can eliminate the while loop. A for loop is enough.
Heres a hint:
In the loop at line 12, you read each line in one iteration to the std::string line.
Now you iterate a for loop nested inside the while loop and assign all the element of file[] the same content: line
You do not need two loops. You can eliminate the while loop. A for loop is enough.
Heres a hint:
cpp Syntax (Toggle Plain Text)
for (int i=0; std::getline(inFileStream,line); i++)//reads the next line to the variable line till the end of the file.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
•
•
Join Date: Apr 2009
Posts: 147
Reputation:
Solved Threads: 7
hmmm yeah seems to not work. I dont know why this is so difficult for me to do, if I have a file with words separated by commers I can grab each word & put it into an array element easily but grabing each line is like rocket science & pulling teeth
int i = 0; while ( std::getline ( inFileStream, line ) ) { file[i] = line; //cout <<line<<endl; i++; }
*Voted best profile in the world*
•
•
Join Date: Apr 2009
Posts: 147
Reputation:
Solved Threads: 7
ok its all solved, Thanks for the help.
The problem had nothing to do with my original code tho, it was the text file i was reading. Its name was music.txt.windows & would'nt allow me to save each line in an array element, I dont know why.
But I just copied the text into a notepad text file & it worked fine, it had something to do with the weird file, maybe '\n' isn't how line breaks work in that file?
Anyway this is the final result of a program creates a Random Song Playlist
The problem had nothing to do with my original code tho, it was the text file i was reading. Its name was music.txt.windows & would'nt allow me to save each line in an array element, I dont know why.
But I just copied the text into a notepad text file & it worked fine, it had something to do with the weird file, maybe '\n' isn't how line breaks work in that file?
Anyway this is the final result of a program creates a Random Song Playlist
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <iomanip> #include <string> #include <cstdlib> #include <ctime> using namespace std; void random_playlist(string playlist[], int playlist_size, string song_list[]); int main() { int p_size; // input variable for size of playlist int counter=0; string array[100]; ifstream infile; ////// Read song database contents + store each song details in array ////////////////// infile.open("data.txt"); while(infile) { getline(infile,array[counter],'\n'); counter++; } infile.close(); counter = counter-1; //cout << counter; ////// End read song database ////////////////////////////////////////////////////////// ////// Create playlist of random songs ///////////////////////////////////////////////// cout << "Enter Playlist size: " << flush; cin >> p_size; cout << endl; string playlist[p_size]; random_playlist(playlist, p_size, array); for(int i=0; i<p_size; i++) { cout << playlist[i] << endl; } return 0; } void random_playlist(string playlist[], int playlist_size, string song_list[]) { srand(time(NULL)); for(int i=0; i<playlist_size; i++) { playlist[i] = song_list[(rand()%62)]; // 62 is size of song database } }
![]() |
Similar Threads
- Counting lines in a text file and further operations (C++)
- Extract part of text file into 2 dimensional string array (Java)
- How to extract value from a text file (Python)
- Reading Lines from Text File (C)
- Script to Remove duplicate lines from a text file. (Perl)
- Newbie: problems deleting lines from text file (large) (Python)
- read-delete lines from a text file (PHP)
- how to remove a number of lines from a text file ? (Python)
- # of lines in a text file (Java)
Other Threads in the C++ Forum
- Previous Thread: Writing a Heap
- Next Thread: C2676 and C2228 errors
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






