[C++] Stream

Hey, i have a small question. I like to read a executable file with a fstream and store it i a BYTE array.
Here my code:
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
#include <string>
#include <iostream>
#include <windows.h>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
	char* Path = "C:\\lo.exe";
	fstream f;
	f.open(Path, ios::in | ios::binary);

	if(!f.is_open())
	{
		cout << "Fehler beim öffnen der Datei!" << endl;
		return -1;
	}
	else
	{
		char* c;
		BYTE* b;
		/*while(f.getline(c, 1024))
		{

		}*/
	}

    f.close();

	return 0;
}


I have no idea how to do. Can somebody help me?
Thanks
Last edited on
The easiest thing to do would be to use a vector.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <fstream>
#include <vector>
using namespace std;

int main()
{
	ifstream EXE ("C:\\hi.exe");
	vector<__int8> Data;

	char Temp;
	while(EXE.get(Temp))
	{
		Data.push_back(Temp);
	}
	EXE.close();
}


EDIT: See below for an easier thing to do...
Last edited on
You can check the size of the file with stat.
1
2
    struct stat info;
    stat(filename.c_str(), &info);

Resize a vector of bytes to be that large,
1
2
    std::vector<BYTE> content;
    content.resize(info.st_size);

then read the info in.
1
2
    std::ifstream is(filename.c_str(), ios::binary);
    is.read((char*)&content[0], info.st.size());

I might do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

	// open with get pointer at end of file (std::ios::ate)
	std::ifstream ifs("test.exe", std::ios::binary|std::ios::ate);

	// read the get pointer with tellg() to give us the file length
	size_t len = ifs.tellg();

	// return the get pointer to the beginning of the file
	ifs.seekg(0);

	// allocate the right amount of memory
	char* data = new char[len];

	// read the file into our buffer
	ifs.read(data, len);

	// ... do stuff on the data here ...

	// discard the data
	delete[] data;
I have a problem with this code
1
2
    struct stat info;
    stat(filename.c_str(), &info);

He didn't accept the secend argument.

Here my hole code:

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
#include <string>
#include <iostream>
#include <windows.h>
#include <fstream>
#include <stdio.h>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
	char* Path = "C:\\lolexe";
	fstream f;
	f.open(Path, ios::in | ios::binary);

	if(!f.is_open())
	{
		cout << "Fehler beim öffnen der Datei!" << endl;
		return -1;
	}
	else
	{
		struct stat info;
		stat(Path, &info);

		vector < BYTE > content;
		content.resize(info.st_size);

		ifstream is(Path, ios::binary);
		is.read((char*)&content[ 0 ], info.st_size());
	}

    f.close();

	return 0;
}


Last edited on
#include <sys/stat.h>
and line 30 must be is.read((char*)&content[ 0 ], info.st_size); because stat::st_size is not a function.
Ok now my code works. This is the first time i working with vectors and so i don't how to print them on my console.
Can anyone help me?^^
1
2
for(int i=0;i<content.size();i++)
    cout <<content[i];

EDIT: or using iterators:
1
2
3
for(vector<BYTE>::iterator i=content.begin();i<content.end();i++)
    cout <<*i;
	
Last edited on
Ok thank you (:
Topic archived. No new replies allowed.