Extract Lines of text from File

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2009
Posts: 147
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Extract Lines of text from File

 
0
  #1
Jul 19th, 2009
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?

  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8. int counter = 0;
  9. char* buffer[999];
  10. ifstream myfile ("music.txt.windows");
  11.  
  12. while (myfile)
  13. {
  14. myfile.getline(buffer[counter],'\n');
  15. counter++;
  16. }
  17.  
  18. cout << counter << endl << endl;
  19.  
  20. for (int i=0; i< counter; i++) {
  21. cout << buffer[i] << endl;
  22. }
  23.  
  24. return 0;
  25. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,868
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Extract Lines of text from File

 
1
  #2
Jul 19th, 2009
You should read about getline here, http://www.cplusplus.com/reference/i...tream/getline/

Personally, I'd use std::string-s and getline...
  1. #include <string>
  2. #include <fstream>
  3. #include <iostream>
  4.  
  5. int main() {
  6. std::ifstream inFileStream( "filename.txt" );
  7. std::string line, file;
  8.  
  9. while( std::getline(inFileStream,line) )
  10. file += line + "\n";
  11.  
  12. std::cout<< file;
  13.  
  14. return 0;
  15. }
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 147
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Re: Extract Lines of text from File

 
0
  #3
Jul 19th, 2009
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?

Originally Posted by twomers View Post
You should read about getline here, http://www.cplusplus.com/reference/i...tream/getline/

Personally, I'd use std::string-s and getline...
  1. #include <string>
  2. #include <fstream>
  3. #include <iostream>
  4.  
  5. int main() {
  6. std::ifstream inFileStream( "filename.txt" );
  7. std::string line, file;
  8.  
  9. while( std::getline(inFileStream,line) )
  10. file += line + "\n";
  11.  
  12. std::cout<< file;
  13.  
  14. return 0;
  15. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 792
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Extract Lines of text from File

 
0
  #4
Jul 19th, 2009
>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.
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 147
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Re: Extract Lines of text from File

 
0
  #5
Jul 20th, 2009
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

  1. #include <string>
  2. #include <fstream>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.  
  9. std::ifstream inFileStream( "music.txt.windows" );
  10. std::string line, file[250];
  11.  
  12. while( std::getline(inFileStream,line) ) {
  13. for (int i=0; i<67; i++) {
  14. file[i] = line + "\n";
  15. }
  16. }
  17.  
  18.  
  19. for (int i=0; i<67; i++) {
  20. cout << file[i] << endl;
  21. }
  22.  
  23. cout << endl << endl << file[4];
  24.  
  25.  
  26. return 0;
  27. }
Last edited by gretty; Jul 20th, 2009 at 12:01 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 792
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Extract Lines of text from File

 
0
  #6
Jul 20th, 2009
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:
  1. 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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 5
Reputation: thEhAckEr has a little shameless behaviour in the past 
Solved Threads: 2
thEhAckEr's Avatar
thEhAckEr thEhAckEr is offline Offline
Newbie Poster

Re: Extract Lines of text from File

 
-1
  #7
Jul 20th, 2009
it doesn't work....
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 147
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Re: Extract Lines of text from File

 
0
  #8
Jul 20th, 2009
Originally Posted by thEhAckEr View Post
it doesn't work....
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Extract Lines of text from File

 
0
  #9
Jul 20th, 2009
int i = 0;

while ( std::getline ( inFileStream, line ) )
{

   file[i] = line;
   //cout <<line<<endl;
   i++;
}
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 147
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Re: Extract Lines of text from File

 
0
  #10
Jul 20th, 2009
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
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <cstdlib>
  6. #include <ctime>
  7.  
  8. using namespace std;
  9.  
  10. void random_playlist(string playlist[], int playlist_size, string song_list[]);
  11.  
  12. int main() {
  13.  
  14. int p_size; // input variable for size of playlist
  15.  
  16. int counter=0;
  17. string array[100];
  18. ifstream infile;
  19.  
  20. ////// Read song database contents + store each song details in array //////////////////
  21. infile.open("data.txt");
  22.  
  23. while(infile) {
  24. getline(infile,array[counter],'\n');
  25. counter++;
  26. }
  27.  
  28. infile.close();
  29.  
  30. counter = counter-1;
  31.  
  32. //cout << counter;
  33.  
  34. ////// End read song database //////////////////////////////////////////////////////////
  35.  
  36. ////// Create playlist of random songs /////////////////////////////////////////////////
  37. cout << "Enter Playlist size: " << flush;
  38. cin >> p_size;
  39. cout << endl;
  40.  
  41. string playlist[p_size];
  42.  
  43. random_playlist(playlist, p_size, array);
  44.  
  45. for(int i=0; i<p_size; i++) {
  46. cout << playlist[i] << endl;
  47. }
  48.  
  49.  
  50. return 0;
  51. }
  52.  
  53. void random_playlist(string playlist[], int playlist_size, string song_list[]) {
  54.  
  55. srand(time(NULL));
  56.  
  57. for(int i=0; i<playlist_size; i++) {
  58. playlist[i] = song_list[(rand()%62)]; // 62 is size of song database
  59. }
  60.  
  61. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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