Read short array from File

I am making a device that is like a simple computer, but first I am making an emulator for the device. I made a short array to hold the 2 bytes while I tested the interpretation and it works:

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
//Original program
const unsigned short oldProg[] = { 'B', 'O', 'S', 'V', 0, 1, 383, 'C', 
	'S', 0, 1, 'D', 29, 6, 0,'D', 30, 6, 0, 'D', 31, 6, 0, 'D', 28, 6, 0,
	'D', 32, 6, 0, 'D', 27, 5, 0, 'D', 33, 5, 0, 'D', 29, 2, 0,
	'D', 31, 2, 0, 'D', 27, 4, 0, 'D', 33, 4, 0,//54 - Smiley
	'D', 13, 27, 0, 'D', 13, 26, 0, 'D', 13, 25, 0, 'D', 13, 24, 0,
	'D', 13, 23, 0, 'D', 13, 22, 0, 'D', 13, 21, 0, 'D', 13, 20, 0,
	'D', 13, 19, 0, 'D', 14, 19, 0, 'D', 15, 19, 0, 'D', 16, 19, 0,
	'D', 17, 19, 0, 'D', 18, 20, 0, 'D', 18, 21, 0, 'D', 18, 22, 0,
	'D', 14, 23, 0, 'D', 15, 23, 0, 'D', 16, 23, 0, 'D', 17, 23, 0,//134 - P
	'D', 20, 27, 0, 'D', 20, 26, 0, 'D', 20, 25, 0, 'D', 20, 24, 0,
	'D', 20, 23, 0, 'D', 21, 23, 0, 'D', 22, 23, 0,//162 - r
	'D', 25, 27, 0, 'D', 25, 26, 0, 'D', 25, 25, 0, 'D', 25, 24, 0,
	'D', 25, 23, 0, 'D', 26, 23, 0, 'D', 27, 23, 0, 'D', 28, 23, 0,
	'D', 28, 24, 0, 'D', 26, 25, 0, 'D', 27, 25, 0, 'D', 28, 25, 0,
	'D', 26, 27, 0, 'D', 27, 27, 0, 'D', 28, 27, 0,//222 - e
	'D', 31, 27, 0, 'D', 31, 25, 0, 'D', 31, 24, 0, 'D', 31, 23, 0,
	'D', 32, 27, 0, 'D', 32, 25, 0, 'D', 32, 23, 0, 'D', 33, 27, 0,
	'D', 33, 26, 0, 'D', 33, 25, 0, 'D', 33, 23, 0,//266 - s
	'D', 35, 27, 0, 'D', 35, 25, 0, 'D', 35, 24, 0, 'D', 35, 23, 0,
	'D', 36, 27, 0, 'D', 36, 25, 0, 'D', 36, 23, 0, 'D', 37, 27, 0,
	'D', 37, 26, 0, 'D', 37, 25, 0, 'D', 37, 23, 0,//310 - s
	'D', 45, 20, 0, 'D', 46, 20, 0, 'D', 47, 20, 0, 'D', 48, 20, 0,
	'D', 49, 20, 0, 'D', 50, 20, 0, 'D', 50, 21, 0, 'D', 49, 22, 0,
	'D', 48, 23, 0, 'D', 47, 24, 0, 'D', 46, 25, 0, 'D', 45, 26, 0,
	'D', 45, 27, 0, 'D', 46, 27, 0, 'D', 47, 27, 0, 'D', 48, 27, 0,
	'D', 49, 27, 0, 'D', 50, 27, 0,//382 - Z
	'I', 'z', 387, 'K', 'E', 'Q' };//389 + 1 = 389 shorts = 778 bytes

/* Writing the original file*/
char* filename = "example.sm";

ofstream out;
out.open(filename, ios::binary | ios::out);
out.write((char*)&oldProg, sizeof(oldProg));
out.close();


Now I want to load the bytes from the file instead of having them preprogrammed. I wish to do this with ifstream as I wrote the bytes to the file to ofstream.

How would I read each short, as a short, into an array say "prog", and then copy it to my main program storage, an array called "memory"?
Last edited on
You could read the from the file in a similar way to that in which it was written.

1
2
    unsigned short buf[389] = {0}; 
    readFile("example.sm", buf, 389);


Function to read the file, with some basic error checking:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void readFile(const char * fname, unsigned short * array, unsigned length)
{
    std::ifstream fin(fname, std::ios::binary);
    fin.seekg(0, std::ios_base::end);
    unsigned file_len = fin.tellg();
    if (file_len != length * sizeof(unsigned short))
    {
        std::cout << "Error: file length: " << file_len 
                  << "  expected: " << length * sizeof(unsigned short) << std::endl;
        return; 
    }
    
    fin.seekg(0, std::ios_base::beg);
    fin.read( (char *) array, file_len);
    fin.close();
}
Thanks a lot!! That worked perfectly. I had to make a few adjustments here and there, but I was able to get that to work. I couldn't figure it out at first as I can never quite remember how to use file IO (and don't use it very often), so this was a big help. Thanks!
Topic archived. No new replies allowed.