C++ 读取文件C++;

C++ 读取文件C++;,c++,arrays,file,C++,Arrays,File,当我保存playingBoard数组时,save会正确打印它,但是当我尝试导入用import创建的save文件时,我会得到奇怪的输出-空格被删除,并替换为没有明显逻辑的1s。(示例如下所示) 最小可复制示例: #include <iostream> #include <fstream> class Board { public: char playingBoard[9][9]; Board() { for (unsigned char i =

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

最小可复制示例:

#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");
}
  • 第二次运行时的输出:

    1| | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
    
    1| | | | | | | | |
     |1| | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
    
  • 第三次运行时的输出:

    1|1|1| | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
    
    1|1|1| | | | | | |
     |1| | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
    

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


    filename
    的内容是什么?“该文件以无符号字符值开头,该值表示写入了多少块板。”您将文件中的第一个数字设置为
    playingBoard[0][0]
    。这正是
    save
    写入的内容。在首次调用函数之前,该函数不存在。请从文件导入开始。在运行程序之前,文件中有什么?我从程序中删除了
    无符号字符
    ,但忘了从描述中删除它。
    1|1|1| | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
    
    1|1|1| | | | | | |
     |1| | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
    
      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;
        }
    
    $ ./a.out 
     | | | | | | | | |
     |1| | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
    
     | | | | | | | | |
     |1| | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |