views:

127

answers:

3

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
+6  A: 

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

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

Let_Me_Be
...or a dangling pointer (to stack, to memory that's been freed, to memory that's been moved [e.g. realloc'ed]), or they return a `std::string` built from such a pointer.
vladr
How can I fix that?
Gabe
@Gabe Simply return a valid null terminated char * string or use std::string.
Let_Me_Be
A: 

I still don't understand. Where would the std::string go? And the char *?

Gabe
Do not post updates as answers, update the question instead.
n0rd
A: 

This still isn't working. I don't think it involves null terminated char's since this is C++. I tried instantiating the string variables with std::, but it still didn't work.

Any ideas on how to fix this would be great.

Here's the code:

    void Playlist::printFile(ofstream &fout, LinkedList<Playlist> allPlaylists, LinkedList<Songs*> library)
{
    fout.open("music.txt");                 // Opens fout.
    if(fout.fail())                         // If fout fails...
    {
        cout << "Output file failed. Information was not saved." << endl << endl;       // Output message. Done.
    }
    else                                    // If not...
    {
        if(library.size() > 0)              // If at least one song, printing begins. 
            fout << "LIBRARY" << endl;      // LIBRARY is always header on music.txt
        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, ends line.
            fout << library.at(i)->getArtistName() << endl;     // Prints artist name, ends line.
            fout << library.at(i)->getAlbumName() << endl;      // Prints album name, ends line.
            fout << library.at(i)->getPlayTime() << " " << library.at(i)->getYear() << " ";     // Prints time, year, rating, genre on same line.
            fout << library.at(i)->getStarRating() << " " << library.at(i)->getSongGenre() << endl;         // Ends line.
        }
        if(allPlaylists.size() <= 0)            // If 0 playlists -> Break.
            fout << endl;
        else if(allPlaylists.size() > 0)        // If 1+ playlists -> Enter.
        {
        int j;
        for(j = 0; j < allPlaylists.size(); j++)                        // Loops through all playlists.
        {
            fout << "xxxxx" << endl;                // Begins each playlist information output with "xxxxx".
            fout << allPlaylists.at(j).getPlaylistName() << endl;           // First line below "xxxxx" is playlist name.
            for(int i = 0; i < allPlaylists.at(j).listSongs.size(); i++)    // Loops through songs on each playlsit.
            {
                std::string song = allPlaylists.at(j).listSongs.at(i)->getSongName();
                std::string artist = allPlaylists.at(j).listSongs.at(i)->getArtistName();
                fout << song << endl;    // Prints song name, ends line.
                fout << artist << endl;  // Prints artist name, ends line.
            }   
        }
        fout << endl;               // Adds a blank line at end of file.
        }
    }
    fout.close();
}

The weird thing is, printing from library works fine. But after print xxxx \n, it prints the song then lines of @@@^@@@@@...." then artist and lines of "@@*^**@@@..." and so on.

Gabe