Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

file input

Posted on 2000-02-21
16
Medium Priority
?
219 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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 900 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…
Suggested Courses

916 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