views:

158

answers:

3

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

A: 

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;
Brian R. Bondy
could you be a bit more specific? pretty much a n00b here.Maybe I didn't explain myself well - I need it to skip the newline whenever it encounters it and not write it to the element of the array...
codefail
@igor: Yes I will be but I have a question first, does the file have a newline after each row?
Brian R. Bondy
Yes, but I imagine (as this will be sent to an "autograder") that the newline can be anywhere?But sure, lets just go with the notion that a newline is only after each row..
codefail
@igor: I'll leave the implementation of getNextDigit() to you
Brian R. Bondy
thanks guys, that helps.(but I also figured out that wasn't what was wrong, lol)but i learned more too. thanks.
codefail
+1  A: 

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

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

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());
}
wilhelmtell