Question

This is the last part of the program I am working on. I want to output a tabular list of songs to cout. And then I want to output a specially formatted list of song information into fout (which will be used as an input file later on).

Printing to cout works great. The problem is that tons of extra character are added when printing to fout.

Any ideas?

Here's the code:

    void Playlist::printFile(ofstream &fout, LinkedList<Playlist> &allPlaylists, LinkedList<Songs*> &library)
{
 fout.open("music.txt"); 
 if(fout.fail())   
 {
  cout << "Output file failed. Information was not saved." << endl << endl;
 }
 else
 {
  if(library.size() > 0)
   fout << "LIBRARY" << endl;
  for(int i = 0; i < library.size(); i++)           // For Loop - "Incremrenting i"-Loop to go through library and print song information.
  { 
   fout << library.at(i)->getSongName() << endl;     // Prints song name.
   fout << library.at(i)->getArtistName() << endl;     // Prints artist name.
   fout << library.at(i)->getAlbumName() << endl;     // Prints album name.
   fout << library.at(i)->getPlayTime() << " " << library.at(i)->getYear() << " ";
   fout << library.at(i)->getStarRating() << " " << library.at(i)->getSongGenre() << endl;   
  }
  if(allPlaylists.size() <= 0)
   fout << endl; 
  else if(allPlaylists.size() > 0)  
  {
  int j;
  for(j = 0; j < allPlaylists.size(); j++)           // Loops through all playlists.
  {
   fout << "xxxxx" << endl;
   fout << allPlaylists.at(j).getPlaylistName() << endl;
   for(int i = 0; i < allPlaylists.at(j).listSongs.size(); i++)          
   {
    fout << allPlaylists.at(j).listSongs.at(i)->getSongName();
    fout << endl;
    fout << allPlaylists.at(j).listSongs.at(i)->getArtistName();
    fout << endl;
   } 
  }
  fout << endl;
  }
 }
}

Here's a sample of the output to music.txt (fout):

LIBRARY
sadljkhfds
dfgkjh
dfkgh
3 3333 3 Rap
sdlkhs
kjshdfkh
sdkjfhsdf
3 33333 3 Rap
xxxxx
PayröÈöè÷÷(÷H÷h÷÷¨÷È÷èøø(øHøhøø¨øÈøèùù(ùHùhùù¨ùÈùèúú(úHúhúú¨úÈúèûû(ûHûhûû¨ûÈûèüü(üHühüü¨üÈüèýý(ýHýhý
! sdkjfhsdf!õüöýÄõ¼5!
sadljkhfds!þõÜö|ö\
 þx þ  þÈ þð ÿ ÿ@ ÿh ÿ ÿ¸ ÿà  0 X  ¨ Ð ø
    enter code here
    enter code here
Was it helpful?

Solution

Most likely, one of your methods returns an improper char * string (not null terminated).

Edit: actually, not just one: getPlaylistName(), getSongName() and getArtistName().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top