我正在从文本文件中读取数独板。木板由9行9位数字表示,如下所示:594632817123478569678159234215346798346897125789215346437561982851924673962783451 编辑这是将while条件更改为(input >> char)时的结果:读取为char的输出:9621248671931369487282543518594767350printArray的输出:962124867193136948728254351859476735�$%w������QȿȔL�`g�Pw���w�这是while(!input.eof())的输出:�94632817123478569678159234215346798346897125789215346437561982851924673962783451 END EDIT 问题是,当我将每个数字放入多维数组时,[0] [0]处的元素显示为带阴影的问号(与g++编译)。仅当我打印出数组的内容时问题才浮出水面,读入的数据似乎很好。对于它的工作,如果我从主要功能中引用任何帮助,将不胜感激!#include <iostream>#include <fstream>#include <string>using namespace std;int createArray(string filename);bool checkRows(char board[][9]);bool checkColumns(char board[][9]);bool checkBoxes(char board[][9]);void printArray(char board[][9]);int main (){ char board [9][9]; int i = 0; int j = 0; int count = 0; ifstream input("board.txt"); char ch; while (input >> ch) { // ch = input.get(); if (ch != '\n') { cout << ch; board[i][j] = ch; j++; if (j % 9 == 0) { i++; } } if (j > 8) j = 0; if (i > 8) i = 0; count++; if (count % 10 == 0) cout << endl; } input.close(); printArray(board); cout << checkRows(board) << endl; cout << checkColumns(board) << endl; return 0;}void printArray(char board[][9]){ for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { cout << board[i][j]; } cout << endl; } cout << board[0][0] << endl; cout << board[0][1] << endl;} 最佳答案 这样,两次读取ch。删除ch = input.get();,您将正确读取每个数字。while (input >> ch){ ch = input.get(); ...}同样,请考虑以下更改条件,以确保正确的endl位置if (count % 10 == 0) cout << endl;至if (count % 9 == 0) cout << endl;关于c++ - 从文件中读取后,数组的第一个元素奇怪,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21947838/ 10-12 15:16