Hi

My program is rather simple, or I thought it would be rather simple :P

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? :)

#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/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;
}
commented: Agreed :) +16

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?

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;
}

>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.

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

#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;
}

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.

it doesn't work....

commented: no sht sherlock -4

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 :P

Member Avatar for iamthwee
int i = 0;

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

   file[i] = line;
   //cout <<line<<endl;
   i++;
}

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

#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
	}

}
Member Avatar for iamthwee

Is it just me but your code doesn't output anything?

At this point I would like to throw in a new word in the discussion. Vectors.
Sorry 'bout that :icon_wink:

commented: *nods* +22
Member Avatar for iamthwee

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.

Member Avatar for iamthwee

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.