gpt4 book ai didi

c++ - 需要从输入文件中跳过换行符 (\n)

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:42:05 27 4
gpt4 key购买 nike

我正在将一个文件读入一个数组。它正在读取每个字符,问题出现在它还读取文本文件中的换行符。

这是一个数独板,这是我读取字符的代码:

bool loadBoard(Square board[BOARD_SIZE][BOARD_SIZE])
{
ifstream ins;

if(openFile(ins)){

char c;

while(!ins.eof()){
for (int index1 = 0; index1 < BOARD_SIZE; index1++)
for (int index2 = 0; index2 < BOARD_SIZE; index2++){
c=ins.get();

if(isdigit(c)){
board[index1][index2].number=(int)(c-'0');
board[index1][index2].permanent=true;
}
}
}

return true;
}

return false;
}

就像我说的,它读取文件,显示在屏幕上,只是遇到\n 时顺序不正确

最佳答案

您可以将 ins.get() 置于 do while 循环中:

do { 
c=ins.get();
} while(c=='\n');

关于c++ - 需要从输入文件中跳过换行符 (\n),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2557438/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com