file input

experts,
   The following code i wrote in visual basic.  It ran incredibly slow and was almost unusable for larger filse...could anyone please translate this into C++ code and if possible, make it much faster?  Thanks a lot!
'-----------------
Dim tst As Byte
Dim numoftimes As Long
 
    Open "c:\windows\desktop\test.exe" For Binary As #1
   
    'Input
    For len1 = 0 To 200000000 Step 3000  'input file
        For z = 1 To 3000  'for every 3000 bytes
            Get #1, len1 + z, tst  'input byte
            ival = (len1 - 1) / 3000
            string1(ival) = string1(ival) & Fill(CInt(tst), 3) 'tack onto string
            If EOF(1) Then Exit For
        Next z
        numoftimes = (len1 - 1)
        If EOF(1) Then Exit For
    Next len1
    Close #1
'------
Private Function Fill(ByVal data As String, ByVal length As Integer) As String
 Fill = data
 If Len(data) < length Then Fill = String(length - Len(data), "0") & Fill
End Function
'-----
TrEaSoNAsked:
Who is Participating?
 
AlexVirochovskyCommented:
#include <io.h>
#include <stdio.h>
.....
  FILE *fIn = fopen ("c:\\windows\\desktop\\test.exe","rb");  if (fIn)
    {
      long lLen = filelength(fileno(fIn)); //len of file
      char *buff = new char[3000];
      long iMax = lLen/3000;
      if (lLen%3000)
        iMax++;      
       
      For (long i = 0; i < iMax ;i++)
        {
          int iLen = 3000;
          if (iLen < lLen)
            iLen = lLen;
          fread(fIn,iLen,1,buff);
          if (iLen < 3000)
            memset(buff+iLen,'0',3000-ilen);
          //you text with buff  
          lLen -= iLen;
        }
       fclose(fIn);
    }
.....
I hope, it helps. Alex
If you have some Q about this text,
ask!
0
 
RONSLOWCommented:
Your Fill function seems odd .. it is declared as taking a String value, but you call it with an integer value.

That might explain some of the slowness.
0
 
TrEaSoNAuthor Commented:
Adjusted points to 300
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
TrEaSoNAuthor Commented:
wow..i didn't see that.  Thanks for pointing that out.  I fixed that much now, but it still runs really really slow on files above 200l.  i was told that the only way to get it faster is if i were to convert it to C++ code.  that is why i need this help.  Think you could help?  Thanks!
0
 
RONSLOWCommented:
Your Fill function seems odd .. it is declared as taking a String value, but you call it with an integer value.

That might explain some of the slowness.
0
 
RONSLOWCommented:
I'll do a quick convert...
0
 
TrEaSoNAuthor Commented:
thanks...do you have any idea how much quicker this should run once its in C++?
0
 
RONSLOWCommented:
Some other problems with your code that need fixing:

len1 goes from 0 upward, so ival will be negative .. seems weird.  Do you really want to use len1-1 ??  Or should it be (len1+z-1) ??

Think you need to fix your VB code first.
0
 
TrEaSoNAuthor Commented:
heh...geez...maybe i should have looked at this code more carefully.  Anything else you see that would make this thing as slow as it is?
0
 
RONSLOWCommented:
one thing is that you specify the position on every read.  It look like you are actaully reading sequentially through the file, so just use the simple Get statement that reads the next byte instaead of continually repositioning the file position.

I'm still trying to understand what the program is doing .. looks like it is converting a binary file into a text file, with each byte converted to a 3 digit string.  And then you are making a big array of such strings (3000*3 = 9000 characters long each).

But you don't declare the string nor say what you are doing with it.

A bit more info about what the program is trying to do will result in a much better conversion.

0
 
RONSLOWCommented:
one thing is that you specify the position on every read.  It look like you are actaully reading sequentially through the file, so just use the simple Get statement that reads the next byte instaead of continually repositioning the file position.

I'm still trying to understand what the program is doing .. looks like it is converting a binary file into a text file, with each byte converted to a 3 digit string.  And then you are making a big array of such strings (3000*3 = 9000 characters long each).

But you don't declare the string nor say what you are doing with it.

A bit more info about what the program is trying to do will result in a much better conversion.

0
 
TrEaSoNAuthor Commented:
I'm sorry for the lack of details about this program.  What i'm trying to do with this code is open any file as binary and input each byte of that file and convert it (the byte) to a 3 digit number.  After that is done i tack it (the byte) onto a string that contains 30k of the original file (hence the step 3000).  That string containing the 30k is inside of a string array that contains all the converted numbers of the program in 30k indexes.  That probably isn't much more than what you know...so if you need clarification on something just let me know...Thanks for your help thus far!
0
 
TrEaSoNAuthor Commented:
bye the way..i forgot to paste
Dim string1(10000) As String
in the posted code. :-)  oops.
0
 
RONSLOWCommented:
Shame that this code doesn't do what you want.

I'll put up a solution that may work a little better (ie. do what you asked for) if you want.

0
 
TrEaSoNAuthor Commented:
Guess i should have checked out the code before i actually graded this.  Could you check the C++ topic area for a question labled "FOR RONSLOW" that way i can give you some points for your great help?  Thanks!
0
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.