Solved

FOR RONSLOW

Posted on 2000-02-22
9
308 Views
Last Modified: 2010-04-02
RONSLOW,
   Here's your question...Any help would be appreciated.  Thanks!
0
Comment
Question by:TrEaSoN
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

 
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
 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

763 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