Solved

FOR RONSLOW

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is the real difference between C++ and Visual C++.NET? 7 144
sorting efficency of sorting algorithm 30 149
C++ mouse_event mouse look 7 131
gdb doesn't stop on breakpoint 2 139
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

732 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