Solved

file input

Posted on 2000-02-21
16
210 Views
Last Modified: 2010-04-02
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
'-----
0
Comment
Question by:TrEaSoN
  • 8
  • 7
16 Comments
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2544527
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
 

Author Comment

by:TrEaSoN
ID: 2544550
Adjusted points to 300
0
 

Author Comment

by:TrEaSoN
ID: 2544551
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2544559
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2544565
I'll do a quick convert...
0
 

Author Comment

by:TrEaSoN
ID: 2544574
thanks...do you have any idea how much quicker this should run once its in C++?
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2544597
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
 

Author Comment

by:TrEaSoN
ID: 2544609
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

by:RONSLOW
ID: 2544627
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2544641
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
 

Author Comment

by:TrEaSoN
ID: 2544652
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
 

Author Comment

by:TrEaSoN
ID: 2544655
bye the way..i forgot to paste
Dim string1(10000) As String
in the posted code. :-)  oops.
0
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 300 total points
ID: 2544892
#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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2548834
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
 

Author Comment

by:TrEaSoN
ID: 2548847
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2548890
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now