0
這是我正在開發的程序的最後一部分。我想輸出一個表格列表中的歌曲。然後我想輸出一個特殊格式的歌曲信息列表到fout(稍後將用作輸入文件)。C++ - 在輸出文件中打印不需要的字符
打印到cout很好。問題在於,當打印到fout時會添加大量額外的字符。
任何想法?
下面的代碼:
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;
}
}
}
下面是輸出的一個樣本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
......或懸掛指針(堆棧,釋放內存,移動到已移動的內存[例如realloc'ed]),或者返回一個由這樣的指針構建的std :: string。 – vladr 2010-03-11 08:29:49
我該如何解決這個問題? – Gabe 2010-03-11 09:13:11
@Gabe只需返回一個有效的以null結尾的char *字符串或使用std :: string即可。 – 2010-03-11 09:51:14