TrEaSoN
asked on
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.e xe" 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
'-----
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.e
'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
'-----
ASKER
Adjusted points to 300
ASKER
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!
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.
That might explain some of the slowness.
I'll do a quick convert...
ASKER
thanks...do you have any idea how much quicker this should run once its in C++?
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.
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.
ASKER
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?
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.
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.
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.
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.
ASKER
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!
ASKER
bye the way..i forgot to paste
Dim string1(10000) As String
in the posted code. :-) oops.
Dim string1(10000) As String
in the posted code. :-) oops.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
I'll put up a solution that may work a little better (ie. do what you asked for) if you want.
ASKER
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!
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_STRIN G*3+1];
};
int main(int argc, char* argv[]) {
// open the file
FILE *fInput = fopen("c:\\windows\\deskto p\\test.ex e","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",byt e); // d is decimal, use x for hex or o for octal
// append to the end of the string
strcat(strings[istring].st ring,digit s);
}
}
// 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 :-)
// 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_STRIN
};
int main(int argc, char* argv[]) {
// open the file
FILE *fInput = fopen("c:\\windows\\deskto
// 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)
// work out the number of strings we need
int nStrings = (lFileLength-1)%MAX_BYTES_
// 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]
// 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",byt
// append to the end of the string
strcat(strings[istring].st
}
}
// 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 :-)
That might explain some of the slowness.