Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert VB Code to C/C++ code 1

Status
Not open for further replies.

ojosVerdes

Technical User
Oct 10, 2002
50
US
Hi all,
I program using vb and I would like to convert the followig code to C or C++. I need to develop a small program that can run from the command line; I am having trouble converting the following code to C/C++

Can someone please help me.

Thanks

//bToCheck is a boolean.
//strReadIn is a string variable that holds up 200
//characters
//strBatch is a 25 string variable.

If Not bToCheck Then
If Mid(strReadIn, 155, 1) = "H" Then
strBatch = Mid(strReadIn, 155, 16)
intBL = 16
ElseIf Mid(strReadIn, 155, 3) = "SME" Then
strBatch = Mid(strReadIn, 155, 19)
intBL = 19
ElseIf Mid(strReadIn, 155, 3) = "DSI" Then
strBatch = Mid(strReadIn, 155, 19)
intBL = 19
ElseIf Mid(strReadIn, 155, 5) = "00000" Then
strBatch = Mid(strReadIn, 175, 25)
intBL = 25
End If
lngNofBatches = lngNofBatches + 1
bToCheck = True
End If
 
Federal102,
strBatch=strReadIn.substr(175, 25)

can I say
if(strBatch != strReadIn.substr(155,intBL))

Thanks
 
Federal102,
I am getting a error when I run the program.

The title of the errorr is :
Microsoft Visual c++ degug library

Program c:\cfiles\chkbth.exe
abnormal program termination

(press retry to debug the application)

...
however when I press retry I get an other error saying:
This program has performed an ilegal operation.

What could it be.


Thanks a million for all your help.
 
You need to do some debugging. The first question is, do you have a version of your code that was working before this one. i.e. do you know what code was changed to generated the error?



 
Federal102,
What Am I doing wrong here? why passing the file does not work.

If the strcpy is not the apropriate comand to use whichone is it?

Thanks again..


#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main(int argc, char* argv[])
{
bool bToCheck = false;
string strReadIn;
string strBatch;
string filename; /* Stores the file name */
string ed;
int intBL = 0;
int lngNofBatches = 0;
int intCounter=0;
// If the number of arguments passed is not = 2 then dispaly a message and
// stop program execution.

if (argc != 2)
{
cout << &quot;Usage: ChkBth asciifile\n&quot; << endl;
return -1 ;
}
else
{
strcpy(filename, argv[1]); // Get 2nd command line string and store it in the filename var
cout << argv[1] << endl;
}

//ifstream infile(&quot;i:\\client\\rjr\\16390311&quot;);
ifstream infile(filename);
istream fin(infile);
 
I would suggest you use CString. Then you can call methods you a more familiar with from the VB world, like Left, Right and Mid stuff. You can ignore strcpy/strcat etc and assign/concatenate strings the very same way you do in vb.

There are always ppl with different preferences, some like char* or TCHAR or whatnot. Others like std:string. My suggestion is to use stuff that works like things you are familiar with. Since I dont have my origin in VB, my personal preferences in this context is irrelevant (but I'd use CString anyway for a number of reasons ;-) ).


Another thing...

You cant print a std::string directly,
string foo;
printf(&quot;%s&quot;,foo); // This will cause a run time error

printf(&quot;%s&quot;,foo.c_str()); // get a const char* to the string. Works.
 
I tried to make the point earlier that mixing C and C++ was not a good idea. At this point I am not sure where the error lies, since you did not post the entire program - in any event, you do not use strcpy with std::string, just use the = operator.

As far as using CString, CString is the Microsoft attempt at std::string code. It is intended for use with MFC (i.e Windows programming). For a standard console app - you should use std::string.

Please post your complete code and we'll try and figure put the problem. Please bear in mind that my solution will involve the removal of ALL C constructs.
 
Federal102,
Here is the entire program
The problem seens to be that after the last record is read, the program actually look for an other line, since it does not find one, it trows and exception in chkbth.exe:

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main(int argc, char* argv[])
{
bool bToCheck = false;
string strReadIn;
string strBatch;
//string filename; /* Stores the file name */
string ed;
int intBL = 0;
int lngNofBatches = 0;
int intCounter=0;
// If the number of arguments passed is not = 2 then dispaly a message and
// stop program execution.
/*
if (argc != 2)
{
cout << &quot;Usage: ChkBth asciifile\n&quot; << endl;
return -1 ;
}
else
{
//strcpy(filename, argv[1]); // Get 2nd command line string and store it in the filename var
cout << argv[1] << endl;
}

strncpy(pszDest, pszSrc, 25);
should be:
strncpy(pszDest, pszSrc + 155, 25);
*/

ifstream infile(&quot;i:\\client\\rjr\\16390311&quot;);
//ifstream infile(filename);
istream fin(infile);
while(getline(fin, strReadIn))
{
if(!bToCheck)
{
if(strReadIn.size() < 200)
{
bToCheck = false;
continue;
}

if(strReadIn.substr(154, 1) == &quot;H&quot;)
{
strBatch = strReadIn.substr(154, 16);
intBL = 16;
}
else if(strReadIn.substr(154, 3) == &quot;SME&quot;)
{
strBatch = strReadIn.substr(154, 19);
intBL = 19;
}
else if(strReadIn.substr(154, 3) == &quot;DSI&quot;)
{
strBatch = strReadIn.substr(154, 19);
intBL = 19;
}
else if(strReadIn.substr(154, 5) == &quot;00000&quot;)
{
strBatch = strReadIn.substr(154, 25);
intBL = 25;
}

++lngNofBatches;
bToCheck = true;
continue;
}
else
{
if(strBatch.substr(0,intBL) != strReadIn.substr(154,intBL))
{
if(strReadIn.substr(154, 1) == &quot;H&quot;)
{
strBatch = strReadIn.substr(154, 16);
}
else if(strReadIn.substr(154, 3) == &quot;SME&quot;)
{
strBatch = strReadIn.substr(154, 19);
}
else if(strReadIn.substr(154, 3) == &quot;DSI&quot;)
{
strBatch = strReadIn.substr(154, 19);
}
else if(strReadIn.substr(154, 5) == &quot;00000&quot;)
{
strBatch = strReadIn.substr(154, 25);
}
++lngNofBatches;
}
}
}
cout << &quot;Number of Batches Found = &quot; << lngNofBatches << endl;
return 0;
}
 
The code as it stands looks fine to me (we can work on the commented out strcpy stuff later if you need to). Could you send me a sample data file to test? Please email to Sean_A_King@fleet.com
 
&quot;For a standard console app - you should use std::string.&quot;

Should you? Why? There is _nothing_ preventing you from using CString in a console app, it has no dependancies to the GUI or Windows related functionality whatsoever.

Yes, it is part of MFC, but MFC != GUI.

Sure, it has the ability to get a string from the resources, which isn't a contradiction to be useful in a console application.

Since unicode is native in WinNT etc you might as well say
&quot;For a standard console app - you should use std::wstring.&quot;
Where I would still say : There is _nothing_ preventing you from using CString in a unicode console app...

Btw, what is a &quot;standard console app&quot;?
Standard C++?
Platform independant?
Unicode/Ascii independant?


 
I guess I should have said, if you want to code a non-Windows app, you should use std::string

STL is platform independant while MFC is built for the Windows platform. STL is more or less part of the standard C++ language. MFC only ships with MS compilers. STL is
far more generic than MFC . STL is built on C++ templates which allow most of its contructs to operate on or with any native or user defined object. STL is geared more towards generic algorithms and iteration while MFC leans more
towards GUI type classes (dialogs, controls, etc).


 
Now that I have some real data to test, I have found one problem that was my fault - I coded your original VB program very literally without considering the consequences.

As I said previously, if your input is less than 200 chars, one of the substr functions will eventually try and access memory that it shouldn't. That is why I put in the if statement to test fro less than 200 chars. Unfortunately, the test is enclosed in another if statement that checks the value of bToCheck. bToCheck is set to true after the first pass and therefore the size check is never accessed again.

After I commented out the following

Code:
bToCheck = true;
continue;

I received the output &quot;Number of Batches Found = 3012&quot;.

I don't know if this is what you are expecting from your sample data. If so, we can work on the command line stuff. If not it's more debugging I'm afraid
 
Federal102,

Pos 155-179 looks like
Since I compare based on the start of the string for DSI, or, SME or, H for example:

DSI1057D\94K0001001\ 1) set bToCheck = true
DSI1057D\94K0001001\ strBatch = strReadIn.substr(154,19);
DSI1057D\94K0001001DSI1057D\42C0018901\ 2) the previous line is different 94K vrs 42c
DSI1057D\42C0018901\ compare strBatch vrs strReadIn.substr(154, 19);

DSI1057D\42C0018901H0003001\85Y\001\ 3) the previous line is different
H0003001\85Y\001H0003001\85Y\001
I should end with 3 distinct batches here, if I run it against the program.

by commenting out those two lines, you actually get a full count of all the lines in the file. That is not my objective

My objective is to count the unique batches only.
Ed
 
Now that I understand what you are doing, there is a much easier way to do this using the STL

The STL container set<typename T> allows the insertion of unique elements only. All you have to do is parse the input lines to get the desired substring and insert into the set. Since the set container does not allow duplicates, it will automatically give you the number of unique batches which is equal to its size.

Here is an example..

Code:
#include <set>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main(int argc, char* argv[])
{
	ifstream infile(&quot;input.txt&quot;);
	istream fin(infile);
	string strReadIn;
	string strBatch;
	int intBL;
	set<string> batch_set; //instantiate set container
	while(getline(fin, strReadIn))
	{
		if(strReadIn.size() < 200)
			continue;
		if(strReadIn.substr(154, 1) == &quot;H&quot;)
        {
            strBatch = strReadIn.substr(154, 16);
            intBL = 16;
			batch_set.insert(strReadIn); //attempt insert
        }
        else if(strReadIn.substr(154, 3) == &quot;SME&quot;)
        {
            strBatch = strReadIn.substr(154, 19);
            intBL = 19;
			batch_set.insert(strReadIn);
		}
        else if(strReadIn.substr(154, 3) == &quot;DSI&quot;)
        {
            strBatch = strReadIn.substr(154, 19);
            intBL = 19;
			batch_set.insert(strBatch);
        }
        else if(strReadIn.substr(154, 5) == &quot;00000&quot;)
        {
            strBatch = strReadIn.substr(154, 25);
            intBL = 25;
			batch_set.insert(strReadIn);
        }
	}
	cout << &quot;Number of batches found is &quot; << batch_set.size() << endl;
	return 0;
}

This returns batch count of 16 from your test file
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top