Solved

FOR RONSLOW

Posted on 2000-02-22
9
281 Views
Last Modified: 2010-04-02
RONSLOW,
   Here's your question...Any help would be appreciated.  Thanks!
0
Comment
Question by:TrEaSoN
  • 5
  • 3
9 Comments
 
LVL 10

Accepted Solution

by:
RONSLOW earned 400 total points
ID: 2548892
OK .. I'll post it here too for you...

// TrEaSoN.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <io.h>
#include <stdio.h>
#include <string.h>

// here we define a string to hold of up to
// 3000*3 characters (300 bytes * 3 digits per bytr)
// plus the required nul terminator
#define MAX_BYTES_PER_STRING 3000
struct String3K {
      char string[MAX_BYTES_PER_STRING*3+1];
};

int main(int argc, char* argv[]) {
      // open the file
      FILE *fInput = fopen("c:\\windows\\desktop\\test.exe","rb");
      // if could not open, just finish
      // really want error handling here
      if (! fInput) return 1;
      // find out how long the file is
      long lFileLength = _filelength(fileno(fInput)); //len of file
      // work out the number of strings we need
      int nStrings = (lFileLength-1)%MAX_BYTES_PER_STRING+1;
      // allocate storage for all the strings
      String3K* strings = new String3K[nStrings];
      // loop thru the file
      for (int istring = 0; istring < nStrings; istring++) {
            // clear the string to start with
            strings[istring].string[0] = '\0'; // clear the buffer string
            // read thru the 3000 byte chunk
            for (int ichar = 0; ichar < MAX_BYTES_PER_STRING; ichar++) {
                  // get the character
                  int byte = fgetc(fInput);
                  // if we get to end of file, just stop here
                  if (byte == EOF) break;
                  // convert the byte to a three digit number
                  // with leading zeros
                  char digits[4];
                  sprintf(digits,"%3.3d",byte);      // d is decimal, use x for hex or o for octal
                  // append to the end of the string
                  strcat(strings[istring].string,digits);
            }
      }
      // finished withthe file now
      fclose(fInput);
      //
      // do what you want with the strings
      //
      // finished with the array of strings
      delete[] strings;
      // all done
      return 0;
}

NOTE: I haven't run it .. but it does compile (MSVC) and at least it looks like it might do what you want :-)
0
 

Author Comment

by:TrEaSoN
ID: 2549027
Adjusted points to 400
0
 

Author Comment

by:TrEaSoN
ID: 2549028
I must say, this is awsome.  I increased the points for you...and i was wondering.  Is there a way to even make this faster?  I know i seem to be getting speed hungry here, but speed is a main aspect in a program.  Any ideas?
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2549042
One thing to do is to read in the whole (upto) 3000 byte chunk in one hit into a 3000byte buffer (need to work out the _actual_ length for the last chunk of the file).  Then loop through the buffer.  This is instead of doing it one byte at a time (which is slow).

I'll see if I can show you how with another version shortly .. got to actually do work right now (that's what I'm paid for ;-)
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:TrEaSoN
ID: 2549056
heh...i understand completely.  I really should consider doing that too.  Thanks for all this help so far.  I look forward to talking with you about this further!
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2549428
Is speed the main aspect of a program?

Ronslow, this should get you 'safe' for a few weeks ;)
0
 

Author Comment

by:TrEaSoN
ID: 2552118
speed isn't the main aspect, however it plays a pivital role in the program.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2552394
another way to get speed increase is to speed up to conversion from int to string.  Best way, with limited number of values, is to just have an array of 256 3-digit strings.  Then the conversion is just an array lookup. ie change the lines

// convert the byte to a three digit number
// with leading zeros
char digits[4];
sprintf(digits,"%3.3d",byte); // d is decimal, use x for hex or o for octal

to

// convert the byte to a three digit number
// with leading zeros
char* digits = digitslookup[byte];

and add to the start of the program

char* digitslookup[256] = {"000","001",
....
"253","254","255"};

(I'm sure you can fill in the rest).

Another things that can be done is to replace the strcat with directly copying the digits to the correct part of the string (because you know that the first 3-digit string is a pos 0, the next at 3 etc etc.

so you can replace

// append to the end of the string
strcat(strings[istring].string,digits);

with

// append to the end of the string
strcpy(strings[istring].string+ichar*3,digits);

that should speed things up quite a bit too, becuase strcat scans along the string everytime until it find the end of string (nul characters), and then copies to there.



0
 

Author Comment

by:TrEaSoN
ID: 2552407
Hey, thanks for all your help on this...I really appreciated it.  Hope to talk to you some other time...bye!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now