943,597 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1016
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 19th, 2009
0

Extract Lines of text from File

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 7
Junior Poster
gretty is offline Offline
158 posts
since Apr 2009
Jul 19th, 2009
1

Re: Extract Lines of text from File

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)
  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. }
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Jul 19th, 2009
0

Re: Extract Lines of text from File

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?

Click to Expand / Collapse  Quote originally posted by twomers ...
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)
  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. }
Reputation Points: 10
Solved Threads: 7
Junior Poster
gretty is offline Offline
158 posts
since Apr 2009
Jul 19th, 2009
0

Re: Extract Lines of text from File

>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.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Jul 20th, 2009
0

Re: Extract Lines of text from File

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

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 7
Junior Poster
gretty is offline Offline
158 posts
since Apr 2009
Jul 20th, 2009
0

Re: Extract Lines of text from File

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:
cpp Syntax (Toggle Plain Text)
  1. for (int i=0; std::getline(inFileStream,line); i++)//reads the next line to the variable line till the end of the file.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Jul 20th, 2009
-1

Re: Extract Lines of text from File

it doesn't work....
Reputation Points: 6
Solved Threads: 2
Newbie Poster
thEhAckEr is offline Offline
5 posts
since Jul 2009
Jul 20th, 2009
0

Re: Extract Lines of text from File

Click to Expand / Collapse  Quote originally posted by thEhAckEr ...
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
Reputation Points: 10
Solved Threads: 7
Junior Poster
gretty is offline Offline
158 posts
since Apr 2009
Jul 20th, 2009
0

Re: Extract Lines of text from File

int i = 0;

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

   file[i] = line;
   //cout <<line<<endl;
   i++;
}
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jul 20th, 2009
0

Re: Extract Lines of text from File

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
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 7
Junior Poster
gretty is offline Offline
158 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Writing a Heap
Next Thread in C++ Forum Timeline: C2676 and C2228 errors





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC