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

发布于 2024-08-26 23:33:04 字数 625 浏览 11 评论 0原文

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

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

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 时顺序不正确

I am reading in a file into an array. It is reading each char, the problem arises in that it also reads a newline in the text file.

This is a sudoku board, here is my code for reading in the char:

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;
}

like i said, it reads the file, displays on screen, just not in correct order when it encounters the \n

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可是我不能没有你 2024-09-02 23:33:04

您可以将 ins.get() 放入 do while 循环中:

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

You can put you ins.get() in a do while loop:

do { 
    c=ins.get();
} while(c=='\n');
赠我空喜 2024-09-02 23:33:04

那么在您的文件格式中,您可以简单地不保存换行符,或者您可以添加一个 ins.get() for 循环。

您还可以将 c=ins.get() 包装在类似 getNextChar() 的函数中,该函数将跳过任何换行符。

我想你想要这样的东西:

 for (int index1 = 0; index1 < BOARD_SIZE; index1++)
 {
  for (int index2 = 0; index2 < BOARD_SIZE; index2++){

   //I will leave the implementation of getNextDigit() to you
   //You would return 0 from that function if you have an end of file
   //You would skip over any whitespace and non digit char.
   c=getNextDigit();
   if(c == 0)
     return false;

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

Well in your file format you can simply not save newlines, or you can add a ins.get() the for loop.

You could also wrap your c=ins.get() in a function something like getNextChar() which will skip over any newlines.

I think you want something like this:

 for (int index1 = 0; index1 < BOARD_SIZE; index1++)
 {
  for (int index2 = 0; index2 < BOARD_SIZE; index2++){

   //I will leave the implementation of getNextDigit() to you
   //You would return 0 from that function if you have an end of file
   //You would skip over any whitespace and non digit char.
   c=getNextDigit();
   if(c == 0)
     return false;

   board[index1][index2].number=(int)(c-'0');
   board[index1][index2].permanent=true;
  }
 }
 return true;
飞烟轻若梦 2024-09-02 23:33:04

您有一些不错的选择。要么不要将换行符保存在文件中,在循环中显式丢弃它们,要么使用 < 中的 code>std::getline()

例如,使用 getline():

#include <string>
#include <algorithm>
#include <functional>
#include <cctype>

using namespace std;

// ...

string line;
for (int index1 = 0; index1 < BOARD_SIZE; index1++) {
    getline(is, line); // where is is your input stream, e.g. a file
    if( line.length() != BOARD_SIZE )
        throw BabyTearsForMommy();
    typedef string::iterator striter;
    striter badpos = find_if(line.begin(), line.end(),
                             not1(ptr_fun<int,int>(isdigit)));
    if( badpos == line.end() )
        copy(board[index1], board[index1]+BOARD_SIZE, line.begin());
}

You have a few good options. Either don't save the newline in the file, explicitly discard them in your loop, or use std::getline() in <string>.

For example, using getline():

#include <string>
#include <algorithm>
#include <functional>
#include <cctype>

using namespace std;

// ...

string line;
for (int index1 = 0; index1 < BOARD_SIZE; index1++) {
    getline(is, line); // where is is your input stream, e.g. a file
    if( line.length() != BOARD_SIZE )
        throw BabyTearsForMommy();
    typedef string::iterator striter;
    striter badpos = find_if(line.begin(), line.end(),
                             not1(ptr_fun<int,int>(isdigit)));
    if( badpos == line.end() )
        copy(board[index1], board[index1]+BOARD_SIZE, line.begin());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文