C++ - 输出文件中打印不需要的字符

发布于 2024-08-25 02:39:27 字数 2208 浏览 4 评论 0原文

这是我正在进行的计划的最后一部分。我想输出要计算的歌曲表格列表。然后我想将特殊格式的歌曲信息列表输出到fout中(稍后将用作输入文件)。

打印到 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事与诗 2024-09-01 02:39:27

最有可能的是,您的方法之一返回不正确的 char * 字符串(不是以 null 结尾)。

编辑:实际上,不仅仅是一个:getPlaylistName()、getSongName() 和 getArtistName()。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文