• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 325
  • Last Modified:

FOR RONSLOW

RONSLOW,
   Here's your question...Any help would be appreciated.  Thanks!
0
TrEaSoN
Asked:
TrEaSoN
  • 5
  • 3
1 Solution
 
RONSLOWCommented:
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
 
TrEaSoNAuthor Commented:
Adjusted points to 400
0
 
TrEaSoNAuthor Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RONSLOWCommented:
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
 
TrEaSoNAuthor Commented:
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
 
KangaRooCommented:
Is speed the main aspect of a program?

Ronslow, this should get you 'safe' for a few weeks ;)
0
 
TrEaSoNAuthor Commented:
speed isn't the main aspect, however it plays a pivital role in the program.
0
 
RONSLOWCommented:
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
 
TrEaSoNAuthor Commented:
Hey, thanks for all your help on this...I really appreciated it.  Hope to talk to you some other time...bye!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now