ifstream in VB6 
Author Message
 ifstream in VB6

Hello!

Could anybody please help translating this to VB6?
I am still very new to C++.

Thanks in advance!
Will

  template< class T > vector< T > ReadFilter(const string & name)
        {
          ifstream in(name.c_str());
          if(!in) {
                cerr << "Unable to open " << name  << endl;
                exit (-1);
          }

          T tmp;
          vector< T > filter(100);
          unsigned i(0);
          while(in >> tmp) {
                filter[i++] = tmp;
                if(i+1 == filter.size())
                  filter.resize(filter.size()+100);
          }
          filter.resize(i);
          return filter;
        }



Tue, 17 Jan 2012 23:33:03 GMT  
 ifstream in VB6
any VB6 open with read of an file to a string would do... (if not too
large)
there should be lots of nice examples out there....

but why call it filter?

or am I missing something....

//CY



Wed, 18 Jan 2012 00:09:04 GMT  
 ifstream in VB6

Hello!

It's not my code, so I have no idea why the filter is there.

Could you give me an explanation of what the code does and maybe post a
VB6 sample so that I can understand what this means in VB6?

Thanks a lot!
Will



Wed, 18 Jan 2012 02:26:53 GMT  
 ifstream in VB6

Quote:
> Hello!

> Could anybody please help translating this to VB6?
> I am still very new to C++.

> Thanks in advance!
> Will

Ok, look like is reading in a file into a vector (basically, a resizable array -
only generic, this is a standard stl class so you can look it up)...

Quote:
>   template< class T > vector< T > ReadFilter(const string & name)
>    {
>      ifstream in(name.c_str());
>      if(!in) {
>            cerr << "Unable to open " << name  << endl;
>            exit (-1);
>      }

Opened the file...

Quote:

>      T tmp;
>      vector< T > filter(100);
>      unsigned i(0);

Declared and initialized some values...

Quote:
>      while(in >> tmp) {
>            filter[i++] = tmp;

Add the current line to the vector

Quote:
>            if(i+1 == filter.size())
>              filter.resize(filter.size()+100);

Add an additional 100 slots to the vector

Quote:
>      }
>      filter.resize(i);

Trim off the excess

Quote:
>      return filter;
>    }

Return..

Fairly simple to accomplish in VB6 - but, it's been so long since I've worked
with VB6 (I'm a C# developer now) I can't remember all of my file IO functions
anymore, and my xp box with vb6 on it died :)  So, I can't give you an exact
translation...  Of course, you can't create an exact translation anyway since
that is a template function.

--
Tom Shelton



Wed, 18 Jan 2012 03:04:44 GMT  
 ifstream in VB6

Thank you!
What exactely is a template function?
I did look it up several times in my life already and never understood
it because I never worked with it.
Or maybe I did something similar in VB6 and never noticed it?
Clearing me up on this one would be very nice!

Will

Quote:

>> Hello!

>> Could anybody please help translating this to VB6?
>> I am still very new to C++.

>> Thanks in advance!
>> Will

> Ok, look like is reading in a file into a vector (basically, a resizable array -
> only generic, this is a standard stl class so you can look it up)...

>>   template< class T > vector< T > ReadFilter(const string & name)
>>        {
>>          ifstream in(name.c_str());
>>          if(!in) {
>>                cerr << "Unable to open " << name  << endl;
>>                exit (-1);
>>          }

> Opened the file...

>>          T tmp;
>>          vector< T > filter(100);
>>          unsigned i(0);

> Declared and initialized some values...

>>          while(in >> tmp) {
>>                filter[i++] = tmp;

> Add the current line to the vector

>>                if(i+1 == filter.size())
>>                  filter.resize(filter.size()+100);

> Add an additional 100 slots to the vector

>>          }
>>          filter.resize(i);

> Trim off the excess

>>          return filter;
>>        }

> Return..

> Fairly simple to accomplish in VB6 - but, it's been so long since I've worked
> with VB6 (I'm a C# developer now) I can't remember all of my file IO functions
> anymore, and my xp box with vb6 on it died :)  So, I can't give you an exact
> translation...  Of course, you can't create an exact translation anyway since
> that is a template function.



Wed, 18 Jan 2012 03:13:08 GMT  
 ifstream in VB6

Quote:
> Hello!

> Could anybody please help translating this to VB6?
> I am still very new to C++.

> Thanks in advance!
> Will

>  template< class T > vector< T > ReadFilter(const string & name)
> {
>   ifstream in(name.c_str());
>   if(!in) {
> cerr << "Unable to open " << name  << endl;
> exit (-1);
>   }

This is basically a read text file function that returns a dynamic
array(called victor in C++), with one byte or more in each element. The
number of bytes is sizeof(T). What T is; depends on where the call to that
function is used. You will find that it refers to something like byte or
char, both of which are one byte, but it could be something else, like int
or long, ect. This is what templates are for, one source code for all
alternatives, rather than writing functions for each type. See

http://en.wikipedia.org/wiki/Template_C%2B%2B

See also:

http://en.wikipedia.org/wiki/Ifstream

Quote:
>   T tmp;
>   vector< T > filter(100);
>   unsigned i(0);

Declare variables, although I am not sure of the last line.

Quote:
>   while(in >> tmp) {

Read X bytes and put them in "tmp" variable. X is sizeof(T).

Quote:
> filter[i++] = tmp;
> if(i+1 == filter.size())
>   filter.resize(filter.size()+100);
>   }
>   filter.resize(i);
>   return filter;
> }

Put the element in the dynamic array, and resize it as needed.

Here is a close VB6 equivalent, if this is to be read byte by byte:

Dim f As Integer
Dim FileContents() As Byte
Dim i As Long

f = FreeFile
Open FileName For Binary Access Read Shared As #f
' Allocate buffer for the entire file
ReDim FileContents(1 To LOF(f))
' Read the entire file into memory
Get f, , FileContents
Close f

For i = 1 To 5
    Debug.Print i, Hex(FileContents(i))
Next



Wed, 18 Jan 2012 03:56:49 GMT  
 ifstream in VB6

Quote:
> Thank you!
> What exactely is a template function?

A template function is a function that basically can work on any type.  In
.NET the concept is called generics, though it's not exactly the same since in
C++ a template is a compile time thing, and in .NET it's a runtime thing.

--
Tom Shelton



Wed, 18 Jan 2012 04:06:05 GMT  
 ifstream in VB6


Quote:


> > Hello!

> > Could anybody please help translating this to VB6?
> > I am still very new to C++.

> > Thanks in advance!
> > Will

> >  template< class T > vector< T > ReadFilter(const string & name)
> > {
> >   ifstream in(name.c_str());
> >   if(!in) {
> > cerr << "Unable to open " << name  << endl;
> > exit (-1);
> >   }

> This is basically a read text file function that returns a dynamic
> array(called victor in C++), with one byte or more in each element. The
> number of bytes is sizeof(T). What T is; depends on where the call to that
> function is used. You will find that it refers to something like byte or
> char, both of which are one byte, but it could be something else, like int
> or long, ect.

Minor pick:
    In this case the only type that would be acceptable would be a "char",
or something that can be the eqivalent of a char as the author is using the
default, ie, "bytes" are out. Although it may or may not work depending on
the compiler and environment - results could be, as they say, undefined.
     If the author wanted to insure bytes he would need to use ...
      ifstream in(name.c_str(), ios:binary);

Which you aptly demonstrate in your VB code ...
[Reading files as bytes, unless one specifically wants to deal with "text"
is generally a good idea. You are guaranteed to get what's there and avoid
any possible translations. However, if dealing with 'text' such translations
may be desireable.]

- Show quoted text -

Quote:
> Here is a close VB6 equivalent, if this is to be read byte by byte:

> Dim f As Integer
> Dim FileContents() As Byte
> Dim i As Long

> f = FreeFile
> Open FileName For Binary Access Read Shared As #f
> ' Allocate buffer for the entire file
> ReDim FileContents(1 To LOF(f))
> ' Read the entire file into memory
> Get f, , FileContents
> Close f

> For i = 1 To 5
>     Debug.Print i, Hex(FileContents(i))
> Next

It is interesting to note that you accomplished the objective more
efficiently in VB by grabbing the stream in one glup. The C++ author could
have done the same with a simple read(). [Details mercifully missing. :-)]

This indicates to me that 1) you are far more proficient with VB than the
author is with C++, and 2) this is a poor example from somewhere. The OP
needs to get another book. <grin>

For the OP:
While it is very, very, beneficial to have experience with one language
before learning a new one, twenty years of teaching/coaching programmers
"C++ as a Second Language" has convinced me that total immersion is the
best, if not only, way to learn C/C++. Forget trying to translate it into
your previous language. Take the examples and fight it out one ugly
semicolon at a time. I appreciate it is hard at times - we all prefer to
relate the new with something we've seen before, but the sooner you get into
a "C state-of-mind" the better you'll be in the short-term and the
long-term.

[The "unsigned i(0);" is just a </sarcasm on> clever way </sarcasm off> to
get an unsigned thingy that is initialized to zero. It is benign in this
case but this habit when dealing with files, streams, etc. contains a subtle
bug. Many files, streams, etc. provide for special end_of_file or other
marker with the value of "-1".]

But that is enough C++ in this group - someone is sure to get annoyed.
<grin>

Good luck with your new adventure.
-ralph



Wed, 18 Jan 2012 08:37:04 GMT  
 ifstream in VB6

| But that is enough C++ in this group - someone is sure to get annoyed.
| <grin>

Not me bro.  The only thing that annoys me is the usual suspects pushing
.NxT.  I actually enjoy my limited but useful time with C++



Wed, 18 Jan 2012 08:51:05 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. ifstream in VB6

2. ifstream >> in visual basic6

3. Ifstream

4. Can't open VB6 because VB6 caused a stack fault in module VB6.EXE

5. Can't open VB6 because VB6 caused a stack fault in module VB6.EXE

6. Converting VB6 to VB.NET without a full VB6 project

7. Windows Service Calling VB6 dll doesn't work but works with VB6

8. VB6 + SP4 error in vb6.dll

9. VB6 + SP4 error in vb6.dll

10. VB6 - SP5 exe generates drWatson during OLE communication with VB6 - SP3 exe

11. Does mscomctl for vb6 need the vb6 runtimes?

12. VB6 Pro and VB6 Enterprise differences??

 

 
Powered by phpBB® Forum Software