Solved

FOR RONSLOW

Posted on 2000-02-22
9
313 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
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!

 
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

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!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

687 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