Click to See Complete Forum and Search --> : Fastest way to do this


acidx
April 19th, 2003, 09:02 AM
I posted this in another part of this board and didn't get a response, maybe here I will.

Ok, so today I've been working on an executable dropper source code generator, and I ran into some problems. Well when I load the files I have to store the file data into a character array. I've gotten this part to work, so i can print to the output src file the following:

unsigned char File1[6] = { 0x4D, 0x5A, 0x90, 0x00, 0x03, 0x00 }

Now my question is, what is the fastest way to go about loading a file and then generating a string like the above? because going byte by byte in the file is actually rather slow if its a a 65kb file. Maybe someone can direct me in the right direction please?

Luis G
April 19th, 2003, 12:26 PM
You can always read a bunch of bytes (a struct is my choice) and then read one by one out of the memory.

Reading small packets of 256 or 512 bytes should improve the performance.

kuphryn
April 19th, 2003, 12:26 PM
What are you trying to do? I understand that you want to read in a file, but what do you want to print?

Kuphryn

Luis G
April 19th, 2003, 12:34 PM
As i understood, he's trying to parse a file and print the output to another file.

Thou, we need more details to help further.

Philip Nicoletti
April 19th, 2003, 03:26 PM
If the files are rather small such as 65K, you can create
a charr array and read it in as follows. You need to
delete [] afterwards of course. You can use
vector<char> instead. In tests that I have run on
various compilers, it tends to be only slight slower,
and you don't have to clean-up memory.



#include < iostream >
#include < fstream >
#include < ctime >

using namespace std;

int main()
{
std::ifstream in("pp1.txt",std::ios::binary);
if (!in)
{
std::cout << "problem with file open" << std::endl;
return 0;
}

clock_t c1,c2;

c1 = clock();

in.seekg(0,std::ios::end);
unsigned long length = in.tellg();
in.seekg(0,std::ios::beg);

char *szFileText = new char[length+1];
in.read(szFileText,length);
szFileText[length] = 0;

// when done

delete [] szFileText;


c2 = clock();

std::cout << static_cast< double >(c2-c1)/static_cast< double >(CLOCKS_PER_SEC) << std::endl;

return 0;
}

Nyromant
April 21st, 2003, 04:32 PM
Erm,do you want the bytes hardcoded or are these bytes read in runtime?

if you want to hardcode them you may write a small program which does the formatting of the array

you may do this like that:
first,write "{ " in the output-file,then do this:
read a byte from sourcefile and and store it,now use itoa(thebyte,charholder,RADIX) and write that string (charholder) to the file followed by " , ".
Do this for every byte in the file (Keep in mind that this would increase the filesize enormous) and complete the output-file with " }",finished
(result should like { 120 ,100 ....})
now just copy that array {} to your sourcecode

you may also write the hexstrings,but i dunno which function to use for that (i´ve also seen this question in visual c++ forum i think)

hth