Okay, so I have a program that read integers from a file and stores them in an array. The relevant code:

readLevel.open("level.txt");
     if((readLevel.is_open()))
     {
           for(int rows = 0;rows<=BLOCKROWS-1;rows++)
           {
                   for(int cols = 0;cols<=BLOCKCOLUMS-1;cols++)
                   {
                           blocks[rows][cols] = readLevel.get();
                   }
           }
     }

However, here's the trouble. Instead of reading the value I want into the array, instead it read the ascii code. In other words, instead of reading 0 into the array, it reads 48. I've been trying for a good few hours to find some way for it to read the actual value from the file, but I'm at my wits end, so any help would be much appreciated.

Recommended Answers

All 6 Replies

What's in the file -- a text '0' or a binary 0? .get() only reads exactly what's in the file. It doesn't translate an ascii digit into its binary equivalent. You need to do the conversion.

What's in the file -- a text '0' or a binary 0? .get() only reads exactly what's in the file. It doesn't translate an ascii digit into its binary equivalent. You need to do the conversion.

Yes, I realize why it's happening, just not how I can fix it. The text file stores a bunch of numbers, as text, sequentially, and this seemed like a good way to read it. Except for the fact that I cannot find a way to turn the ascii characters into decimal values. Not short of hardcoding a conversion for every relevant number.

edit:
Well, given the small number of possible values, I realized it might be easier to just hardcode it, so I fixed it like this:

for(int rows = 0;rows<=BLOCKROWS-1;rows++)
           {
                   for(int cols = 0;cols<=BLOCKCOLUMS-1;cols++)
                   {
                           blocks[rows][cols] = readLevel.get();
                           switch(blocks[rows][cols])
                           {
                                                     case 48:
                                                          blocks[rows][cols] = 0;
                                                          break;
                                                     case 49:
                                                          blocks[rows][cols] = 1;
                                                          break;
                                                     case 50:
                                                          blocks[rows][cols] = 2;
                                                          break;
                                                     case 51:
                                                          blocks[rows][cols] = 3;
                                                          break;
                                                     case 52:
                                                          blocks[rows][cols] = 4;
                                                          break;
                                                     case 53:
                                                          blocks[rows][cols] = 5;
                                                          break;
                                                     case 54:
                                                          blocks[rows][cols] = 6;
                                                          break;
                           }
                   }
           }

I still wish there would be a better way of doing it, though. It would take a long time with more values.

Your first post contains your answer:

Instead of reading the value I want into the array, instead it read the ascii code. In other words, instead of reading 0 into the array, it reads 48.

If you have 48 and wanted 0, how can you get there?

What happens if '1' is the first thing read? Or 8? Is there a pattern?

If blocks is an int array, can't you just change line 8 to: readLevel >>blocks[rows][cols]; As long as your files are laid out in a logical manner e.g.,

0 1 2 3 
4 5 6 7

it should work

Ah, just realized than instead of hardcoding every value, I could just go:

blocks[rows][cols] = (readLevel.get())- 48;

Ah, just realized than instead of hardcoding every value, I could just go:

blocks[rows][cols] = (readLevel.get())- 48;

BINGO!!! :icon_mrgreen:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.