c++ - 读取文件 C++ 时出现奇怪的错误

标签 c++ arrays file

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

最小可重现示例:

#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| | | | | | |
     |1| | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
     | | | | | | | | |
    

最佳答案

您的问题是,默认情况下,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;
    }

输出:

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

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

关于c++ - 读取文件 C++ 时出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62639242/

相关文章:

c++ - 从字符串文件 c++ 创建一个节点迷宫

c++ - Eigen C++;就地矩阵乘法

c++ - 'new' 在相对较小的分配上导致 std::bad_alloc

arrays - 如何显示错误,包括元胞数组的内容

c# - 如何从包含多个对象的 JSON 文件中反序列化

c++ - 是否可以 random_shuffle 一个 int 元素数组?

javascript - 我如何比较两个在javascript中长度不同的数组?

python - 将数据保存到文本文件时遇到问题

c++ - 在 C++ 上设置文件颜色

c - 程序复制文件不正确