You should read about getline here, http://www.cplusplus.com/reference/iostream/istream/getline/
Personally, I'd use std::string-s and getline...
#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;
}
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
>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.
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
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:
for (int i=0; std::getline(inFileStream,line); i++)//reads the next line to the variable line till the end of the file.
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
int i = 0;
while ( std::getline ( inFileStream, line ) )
{
file[i] = line;
//cout <<line<<endl;
i++;
}
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Is it just me but your code doesn't output anything?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
At this point I would like to throw in a new word in the discussion. Vectors .
Sorry 'bout that :icon_wink:
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
At this point I would like to throw in a new word in the discussion. Vectors .
Sorry 'bout that :icon_wink:
Yes, and if I understand this correctly, you have a text file with mp3's on each line. Then you want to mix them up.
A shuffle algo would be better.
[edit] You also have to be careful with rand % N, because it does either one less/one more than you expect.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
Maybe something like...
#include <ctime>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
int main()
{
srand ( unsigned ( time (NULL) ) );
//read in a file
ifstream read("data.txt");
string line;
vector <string> tmp;
while ( getline( read, line, '\n') )
{
if (line.length() > 0 ) //skip blank lines?
{
tmp.push_back(line);
}
}
random_shuffle(tmp.begin(), tmp.end());
vector<string>::iterator it;
cout << "myvector contains:\n";
for (it=tmp.begin(); it!=tmp.end(); ++it)
cout << *it << endl;
read.close();
cin.get();
return 0;
}
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439