Not printing file correctly

So I am trying to read and print a file for an assignment but it doesn't print exactly how it is in the file.

Can someone help?

//file
/*r bqk nr
pppppp p
n p

P
P
P PP PPP
RNBQKB R*/





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  void loadBoard(string file, char board[][SIDE]){

    ifstream infile;

    cin >> file;

    infile.open(file);

    if(!infile.is_open()){

        cerr << "File not found\n";

    }


    while (infile){

        for(int i = 0; i < SIDE; i++){

            for (int j = 0; j < SIDE; j++){

           infile.get(board[i][j]);

            cout << board[i][j];

            }

        }
        infile.close();
    }

}
Some of the characters that you are reading are the newline characters - you don't want those in your board array. Also, you are expecting to read 8 characters per line, but there is no guarantee that the input file maintains trailing blanks.

It is unclear from your question what the format of the file is. Are the /* and */ part of the file or not? You would be better putting the original in output tags (second item in the format menu) so that it preserves the spaces.

You don't need the while{ } loop - you've only got one board.

Personally, I would have used string board[SIZE], rather than char board[SIZE][SIZE], as you could then read and write more easily.

Please don't double-space unnecessarily, and try to indent consistently (which usually means using spaces rather than invisible tabs - or an unholy mix of the two).
Last edited on
Yes the /**/ is the actual input file I am reading from. For the assignment I am meant to use char [][SIZE], and have the input file read from and printed out.
Could you post the actual input file and place it within output tags so that we can see any spacing. (Just look at your original post online - it is rather ambiguous - THAT IS NOT A VALID CHESSBOARD)

Obviously if you put '/', '*', '\' in the file, and it also contains newline character(s), then they will end up getting read into the board array.
Last edited on
Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <fstream>
#include <iostream>
#include <string>
#include <cctype>

const size_t SIDE = 8;

bool loadBoard(const std::string& file, char board[][SIDE]) {
	std::ifstream infile;

	infile.open(file);
	if (!infile.is_open())
		return false;

	infile.ignore(2);	//Ignore /*
	for (size_t i = 0; i < SIDE; ++i) {
		std::string row;

		getline(infile, row);	// Read each row as a string
		for (size_t j = 0; j < SIDE; ++j)
			board[i][j] = j < row.size() && (std::isalpha(row[j]) || std::isspace(row[j])) ? std::toupper(row[j]) : ' ';
	}

	infile.close();
	return true;
}

void displayBoard(char board[][SIDE])
{
	for (int i = 0; i < SIDE; ++i) {
		for (int j = 0; j < SIDE; ++j)
			std::cout << board[i][j];

		std::cout << '\n';
	}
}

int main()
{
	char board[SIDE][SIDE] = {0};

	std::string file;
	std::cout << "Enter filo name: ";
	std::getline(std::cin, file);

	if (loadBoard(file, board))
		displayBoard(board);
	else
		std::cout << "Cannot open file " << file << '\n';
}


It's usually better to have a function to do just one job - read or display etc. Your loadBoard() obtains the name of the file, reads it and displays the array. I'd suggest this is split as per above.

Given a file of:


/*r bqk nr
pppppp p
n p

P
P
P PP PPP
RNBQKB R*/


displays:


R BQK NR
PPPPPP P
N P

P
P
P PP PPP
RNBQKB R


as required with all input converted to uppercase.

Last edited on
Sorry everyone, this is my first time using this website so I am unfamiliar on posting. But could someone tell me on how to post the actual input file?
One way is to up-load the file to cloud-storage (such as drop-box), set for sharing and provide a link in a post.
Hello karkand,

As lastchance pointed out you could use the output tag http://www.cplusplus.com/articles/z13hAqkS/ or not. If you do not use the output tag just copy and paste.

You start by saying that this is an assignment. It is helpful to post the actual requirements of the assignment , so everyone will know what you have to do.

It also helps to post enough code that can be compiled and run for testing. Do not make people guess at what is missing.

I am guessing that your imput file might look like this:

/*
r_bqk_nr
pppppp_p
n_p_____
________
P_______
P_______
P_PP_PPP
RNBQKB_R*/


I used the underscore to denote spaces. This would make it easy to read.

If your file would look like this:

/*
r_bqk_nr
pppppp_p
n_p

P
P
P_PP_PPP
RNBQKB_R*/


This would be more involved to read the file and put it into the array, but it could be done. seeplus's idea of using a string would work.

Andy
Hey guys I thought I sent this already but here's the link

https://drive.google.com/file/d/1EuDXS98EBy4MlrXOUV_piUAASJRdZZUu/view?usp=sharing
Hello karkand,

Nice link, but it says access denied.

Andy
Link requires an authenticated google account.
@karkand

Please change the input filename in the below to that of your own, run the code, then upload the output.txt file to this forum.

What you have posted so far is an invalid chessboard.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
   string line;
   ifstream in ( "input.txt"  );    // replace with the name of your input file
   ofstream out( "output.txt" );
   while( getline( in, line ) ) 
   {
      replace( line.begin(), line.end(), ' ', '_' );
      out << line << '\n';
   }
}

@lastchance
This was the output file

r_bqk_nr
pppppp_p
__n____p
________
____P___
P_______
P_PP_PPP
RNBQKB_R
You mean (preserving spaces of the right length):

r_bqk_nr
pppppp_p
__n____p
________
____P___
P_______
P_PP_PPP
RNBQKB_R
You aren't dealing correctly with the ends of lines.

Try this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const int SIDE = 8;


//==========================================================


void loadBoard( string filename, char board[][SIDE])
{
   ifstream infile ( filename );
   if ( !infile )
   {
      cerr << "File not found\n";
      return;
   }

   for ( int i = 0; i < SIDE; i++ )
   {
      for ( int j = 0; j < SIDE; j++) 
      {
         infile.get( board[i][j] );
      }
      string endOfLine;   getline( infile, endOfLine );
   }
}


//==========================================================


void printBoard( char board[][SIDE])
{
   for ( int i = 0; i < SIDE; i++ )
   {
      for ( int j = 0; j < SIDE; j++)
      {
         cout << board[i][j];
      }
      cout << '\n';
   }
}


//==========================================================


int main()
{
   char board[SIDE][SIDE];
   loadBoard( "input.txt", board );
   printBoard( board );
}


r bqk nr
pppppp p
  n    p
        
    P   
P       
P PP PPP
RNBQKB R
Yes that works, thank you!
 
string endOfLine;   getline( infile, endOfLine );


Why not just:

 
ignore(numeric_limits<streamsize>::max(), '\n'):


without requiring std::string?

That code assumes that each 'line' in the file is exactly SIDE chars right space padded (plus the \n).
That code assumes that each 'line' in the file is exactly SIDE chars right space padded


Which is what was eventually ascertained here:
http://www.cplusplus.com/forum/beginner/272958/#msg1177070



Why not just:
ignore(numeric_limits<streamsize>::max(), '\n'):
without requiring std::string?

Because I find
#include <string>
rather more useful on the whole than
#include <limits>
Topic archived. No new replies allowed.