Storing a file as DWORD?

Mar 24, 2009 at 11:36pm
Hello,

I'm currently trying to store a files data as a DWORD array. But I seem to be having problems, the output does not seem correct. As you can see in the code below, I am trying to create a header file of this data which I can later compile along with my app. It's more of a design time function.

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
			void CreateMemoryBlock(String ^FileIn, String ^FileOut)
			{
				pin_ptr<const wchar_t> wch =  PtrToStringChars(FileIn);
				int                    len = ((FileIn->Length+1) * 2);
				char                   *ch = new char[ len ];

				wcstombs(ch, wch, len);
				std::string s              = ch;

				//Read in.
				std::ifstream::pos_type size;
				char *memblock;
				std::ifstream file (s.c_str(), std::ios::in|std::ios::binary|std::ios::ate);
				if(file.is_open())
				{
					size = file.tellg();
					memblock = new char [size];
					file.seekg (0, std::ios::beg);
					file.read (memblock, size);
					file.close();
				}


				wch =  PtrToStringChars(FileOut);
				len = ((FileIn->Length+1) * 2);
				ch = new char[ len ];

				wcstombs(ch, wch, len);
				s             = ch;

				//Write out.
				int sizeo  = size / 4;
				DWORD* mem = (DWORD*)memblock;

				FILE* text = fopen(s.c_str(), "w");
				fprintf(text, "static DWORD test[] = {");

				for(int i = 0; i < sizeo; i++)
				{
					fprintf(text, "%u,", mem[i]);
					if(i % 32 == 31)
					{
						fprintf(text, "\n");
					}
				}
				fprintf(text, "};");

				fclose(text);

				delete[] memblock;
			}
	};


I'm pretty sure it is the sizeo variable that is incorrect. Does anybody have any advice for this?

Thanks
Topic archived. No new replies allowed.