having junk when reading characters from text file

Trying to read some characters from a text file into a 2d array but when I print my array, there are some junk characters...
Here is a screenshot of my text file http://prntscr.com/jg8v1f
and the output http://prntscr.com/jg8vl8
Thank you!
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void main()
{
	char A[8][8];
	char b;
	ifstream source;
	if (source.fail())
	{
		cout << "error!!" << endl;
		exit(1);
	}
	source.open("Easy.txt");
	for (int r = 0; r < 8; r++)
		for (int c = 0; c < 8; c++)
		{
			source.get(b);
			if (b == '#')
				A[r][c] = b;
			else
				if (b == '.')
					A[r][c] = b;
				else
					if (b == 'a')
						A[r][c] = b;
		}
	
	for (int r = 0; r < 8; r++)
	{
		for (int c = 0; c < 8; c++)
			cout << A[r][c];
		cout << endl;
	}
	source.close();
	system("pause");
}
Last edited on
It's more portable for main to return an int instead of being void.

You are checking for source.fail() before you even try to open the file!

When reading the file character-by-character, you are not taking into account the newline characters. I would just skip whitespace generally. The easiest way to do that is to use source >> b; instead of source.get(b).

BTW, it's generally better to use spaces instead of tabs since people's tab settings may be different from yours. The standard "tab" size these days is 4 spaces (definitely not 8!).
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main() {
    char A[8][8];
    char b;

    ifstream source("Easy.txt");
    if (!source) {
        cerr << "error!!\n";
        exit(1);
    }

    for (int r = 0; r < 8; r++)
        for (int c = 0; c < 8; c++) {
            source >> b;
            switch (b) {
            case '#':
            case '.':
            case 'a':
                A[r][c] = b;
                break;
            default:
                cerr << "bad character in file: " << b << '\n';
                exit(1);
            }
        }

    for (int r = 0; r < 8; r++) {
        for (int c = 0; c < 8; c++)
            cout << A[r][c];
        cout << '\n';
    }
    
    // ifstreams automatically close when they go out of scope
    // (that's the magic of a destructor!)
}

Last edited on
Hello chrisamgad,

Sometimes it is not the program that is the problem, but the input file containing characters that you do not see.

I found a program called HxD - Hexeditor found at https://mh-nexus.de/en/programs.php

It is helpful to see the hex numbers of what is exactly in a file. I have found some strange things in a text file that should not have been there.

If it is not the program check the "txt" file.

Hope that helps,

Andy
It's more portable for main to return an int instead of being void.

You are checking for source.fail() before you even try to open the file!

When reading the file character-by-character, you are not taking into account the newline characters. I would just skip whitespace generally. The easiest way to do that is to use source >> b; instead of source.get(b).

BTW, it's generally better to use spaces instead of tabs since people's tab settings may be different from yours. The standard "tab" size these days is 4 spaces (definitely not 8!).

Great this worked for this specific code but what if I exchanged the 'a' character with a ' ' character (space) how could I make it work?
what if I exchanged the 'a' character with a ' ' character (space) how could I make it work

Then don't skip spaces. But you still need to deal with the newline char:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    for (int r = 0; r < 8; r++)
        for (int c = 0; c < 8; c++) {
            source.get(b);
            switch (b) {
            case '#':
            case '.':
            case ' ':
                A[r][c] = b;
                break;
            case '\n':
                break;   // ignore newlines
            default:
                cerr << "bad character in file: " << b << '\n';
                exit(1);
            }
        }


Reading line-by-line, it could be:
1
2
3
4
5
6
    string line;
    for (int r = 0; r < 8; r++) {
        getline(source, line);
        for (int c = 0; c < 8; c++)
            A[r][c] = line[c];   // assuming chars are good
    }

1
2
3
4
5
6
string line;
    for (int r = 0; r < 8; r++) {
        getline(source, line);
        for (int c = 0; c < 8; c++)
            A[r][c] = line[c];   // assuming chars are good
    }

Thank you it worked!
Last edited on
Topic archived. No new replies allowed.