|
-
April 19th, 2003, 09:02 AM
#1
Fastest way to do this
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?
be happy that your Windows crashed; it gives your computer a rest
-
April 19th, 2003, 12:26 PM
#2
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.
int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}
-
April 19th, 2003, 12:26 PM
#3
What are you trying to do? I understand that you want to read in a file, but what do you want to print?
Kuphryn
-
April 19th, 2003, 12:34 PM
#4
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.
int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}
-
April 19th, 2003, 03:26 PM
#5
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.
Code:
#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;
}
-
April 21st, 2003, 04:32 PM
#6
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
"Don´t Panic"
Software and cathedrals are much the same - first we build them, then we pray.
Fluchen ist die einzige Sprache, die alle Programmierer perfekt beherrschen. (Murphy)
If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.
(found at http://mindprod.com/unmainmisc.html)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|