读取文件 C++ 时的奇怪错误

bea*_*own 3 c++ arrays file

当我保存playingBoard数组时,save正确打印它,但是当我尝试导入用save创build 的文件时import,我得到奇怪的输出 - 空格被删除并替换为1s 没有明显的逻辑。(下面提供了示例)

最小可重现示例:

#include <iostream>
#include <fstream>

class Board
{
public:
  char playingBoard[9][9];
  
  Board()
  {
    for (unsigned char i = 0; i < 9; ++i)
      for (unsigned char j = 0; j < 9; ++j)
        playingBoard[i][j] = ' ';
  }

  bool import(std::string filename)
  {
    std::ifstream ifs {filename};
    if (!ifs.is_open())
      return false;

    for (unsigned char i = 0; i < 9; ++i) {
      for (unsigned char j = 0; j < 9; ++j) {
        ifs >> playingBoard[i][j];
        std::cout << playingBoard[i][j] << "|";
      }
      std::cout << std::endl;
    }
  
    ifs.close();
    return true;
  }

  bool save(std::string filename) const
  {
    std::ofstream ofs {filename, std::ios::app};
    if (!ofs.is_open())
      return false;
  
    for (unsigned char i = 0; i < 9; ++i) {
      for (unsigned char j = 0; j < 9; ++j) {
        ofs << playingBoard[i][j];
        std::cout << playingBoard[i][j] << "|";
      }
      std::cout << std::endl;
    }
  
    ofs.close();
    return true;
  }
};

int main()
{
  Board board;
  board.import("filename");
  
  std::cout << std::endl;
  board.playingBoard[1][1] = '1';
  board.save("filename");
}
Run Code Online (Sandbox Code Playgroud)
  • 第一次运行时的输出(文件之前不存在,因此只有一个输出):

     | | | | | | | | |
     |1| | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
    
    Run Code Online (Sandbox Code Playgroud)
  • 第二次运行的输出:

    1| | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
    
    1| | | | | | | | |
     |1| | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
    
    Run Code Online (Sandbox Code Playgroud)
  • 第三次运行的输出:

    1|1|1| | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
    
    1|1|1| | | | | | |
     |1| | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
    
    Run Code Online (Sandbox Code Playgroud)

Ste*_*ell 6

您的问题是默认情况下,空格会被跳过operator >>。您需要使用另一种从文件中提取字符的方法,例如get成员函数(下面的示例,使用 gcc-9.3.0 进行了测试)。

  bool import(std::string filename)
  {
    std::ifstream ifs {filename};
    if (!ifs.is_open())
      return false;

    for (unsigned char i = 0; i < 9; ++i) {
      for (unsigned char j = 0; j < 9; ++j) {
        playingBoard[i][j] = ifs.get();
        std::cout << playingBoard[i][j] << "|";
      }
      std::cout << std::endl;
    }
Run Code Online (Sandbox Code Playgroud)

输出:

$ ./a.out 
 | | | | | | | | |
 |1| | | | | | | |
 | | | | | | | | |
 | | | | | | | | |
 | | | | | | | | |
 | | | | | | | | |
 | | | | | | | | |
 | | | | | | | | |
 | | | | | | | | |

 | | | | | | | | |
 |1| | | | | | | |
 | | | | | | | | |
 | | | | | | | | |
 | | | | | | | | |
 | | | | | | | | |
 | | | | | | | | |
 | | | | | | | | |
 | | | | | | | | |
Run Code Online (Sandbox Code Playgroud)