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
 
Check out the MFC CString class first (no doubt that this will be more pleasent for you than using character pointers).

All evaluations should be put between parentheses:

If(!bToCheck) { //Notice also how not is replaced by ! and that if blocks should be put between those {} (don't know what they're called in English).
}

Loose the then and end if (they've been replaced by {}).

>>lngNofBatches = lngNofBatches + 1
In C++ you can just write lngNofBatches++ (which is shorter as well as more efficient).
Greetings,
Rick
 
Rick,
Tell me something, if I have an array of character, the size being 200, and I want to grab from positions 155 to 179. How do I do it is c++?


I belevieve once I stablish this, I'll be able to take it from there.

Thanks

Ed.
 
If it's copying the characters that you want:

//Create destination buffer:
char* pszDest = new char[25];
ZeroMemory((void*)pszDest, 25);

//Copy selected part:
strncpy(pszDest, pszSrc, 25); //Assuming your original array of characters is pointed to by pszSrc, of course.


Greetings,
Rick
 
Rick,
Correct me if I am wrong, but that will only take the first 25 position [from 1-25] no?

This part of my code, please bear in mind that I not a C++ programmer. And I don't know if my array is a pointer array.


if((pfile1 = fopen(filename, "rb")) == NULL)
{
printf("Sorry, can't open %s", filename);
return -1;
}

while(!feof(pfile1))// && (!feof(pfile2))) /* Continue until end of file */
{
char strIn[200];
char strOut[200];
char strFinal[200];

//Read the first line of the file
fgets(strIn, sizeof(strIn), pfile1);

//Create destination buffer:
char* pszDest = new char[25];
ZeroMemory((void*)pszDest, 25);

//Copy selected part:
strncpy(pszDest, strIn, 25); //Assuming your original ar ay of characters is pointed to by pszSrc, of course.

//printf("%s\n", BatchN);

//strncat(strOut, strIn, sizeof(strOut));
//strncpy(strFinal, strOut, strlen(strOut - 2));

intBtn++;
}
fclose(pfile1); /* Close the files */

printf("Number of Batched Found = %ld\n", intBtn);
return 0;
 
A C++ solution is as follows..

Code:
#include <string>

using std::string;


int main(int argc, char* argv[])
{
	bool bToCheck = false;
	string strReadIn;
	string strBatch;
	
	//Code to initialize strReadIn////////////////

	int intBL = 0;
	int lngNofBatches = 0;
	if(!bToCheck)
	{
		if(strReadIn.substr(155, 1) == &quot;H&quot;)
		{
			strBatch = strReadIn.substr(155, 16);
			intBL = 16;
		}
		else if(strReadIn.substr(155, 3) == &quot;SME&quot;)
		{
			strBatch = strReadIn.substr(155, 19);
			intBL = 19;
		}
		else if(strReadIn.substr(155, 3) == &quot;DSI&quot;)
		{
			strBatch = strReadIn.substr(155, 19);
			intBL = 19;
		}
		else if(strReadIn.substr(155, 5) == &quot;00000&quot;)
		{
			strBatch = strReadIn.substr(175, 25);
			intBL = 25;
		}
		++lngNofBatches;
		bToCheck = true;
	}
	return 0;
}
 
FEDERAL102,

How do I asign the record from the file to the variable strReadIn?
Can that be done with a:
fgets(strReadIn, sizeof(strReadIn), pfile1); ???

Thanks

if((pfile1 = fopen(filename, &quot;rb&quot;)) == NULL)
{
printf(&quot;Sorry, can't open %s&quot;, filename);
return -1;
}

while(!feof(pfile1))// && (!feof(pfile2))) /* Continue until end of file */
{
// CODE YOU GAVE ME HERE

}
fclose(pfile1); /* Close the files */

printf(&quot;Number of Batched Found = %ld\n&quot;, intBtn);
return 0;
}


 
Code:
#include<iostream>
#include<fstream>
#include<string>

using namespace std;


int main(int argc, char* argv[])
{
	bool bToCheck = false;
	string strReadIn;
	string strBatch;
	int intBL = 0;
	int lngNofBatches = 0;
	//Code to initialize strReadIn///////////
	ifstream infile(&quot;input.txt&quot;);            
	istream fin(infile);			
	while(fin >> strReadIn )		
	////////////////////////////////////////
	{
	
		if(!bToCheck)
		{
			//Check if input is < 200 characters - if so ignore////
			if(strReadIn.size() < 200)			     
			{						     	
				bToCheck = false;			     
				continue;				     
			}						     
			///////////////////////////////////////////////////////
			if(strReadIn.substr(155, 1) == &quot;H&quot;)
			{
				strBatch = strReadIn.substr(155, 16);
				intBL = 16;
			}
			else if(strReadIn.substr(155, 3) == &quot;SME&quot;)
			{
				strBatch = strReadIn.substr(155, 19);
				intBL = 19;
			}
			else if(strReadIn.substr(155, 3) == &quot;DSI&quot;)
			{
				strBatch = strReadIn.substr(155, 19);
				intBL = 19;
			}
			else if(strReadIn.substr(155, 5) == &quot;00000&quot;)
			{
				strBatch = strReadIn.substr(175, 25);
				intBL = 25;
			}
			++lngNofBatches;
			bToCheck = true;
		}
	}
	return 0;
}
 
FEDERAL102,

When I compile the program I get the following:


How can I solve it?

Thanks in advance.

Ed.
--------------------Configuration: ChkBth - Win32 Debug--------------------
Compiling...
ChkBth.c
c:\program files\microsoft visual studio\vc98\include\eh.h(32) : fatal error C1189: #error : &quot;eh.h is only for C++!&quot;
Error executing cl.exe.

ChkBth.obj - 1 error(s), 0 warning(s)
 
Thanks for all your help. One more thing, I want to be able to pass the ascii file as an argument, however this is not working properly, Can you please point me in the right direction.

Thanks

Ed.



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

using namespace std;

int main(int argc, char *argv[])
{
bool bToCheck = false;
string strReadIn;
string strBatch;
char filename[80]; /* Stores the file name */
int intBL = 0;
long lngNofBatches = 0;

//Code to initialize strReadIn///////////

// If the number of arguments passed is not = 2 then dispaly a message and
// stop program execution.
if (argc != 2)
{
printf(&quot;Usage: ChkBth asciifile\n&quot;);
printf( &quot;Arg 0\n &quot; , argv[0]);
printf( &quot;Arg 1\n &quot; , argv[1]);
return -1 ;
}
else
{
strcpy(filename, argv[1]); // Get 2nd command line string and store it in the filename var

}


ifstream infile(filename);
printf( &quot;File name 2\n &quot; , filename);
istream fin(infile);
while(fin >> strReadIn )
////////////////////////////////////////
{

if(!bToCheck)
{
//Check if input is < 200 characters - if so ignore////
if(strReadIn.size() < 200)
{
bToCheck = false;
continue;
}
///////////////////////////////////////////////////////
printf( &quot; 1 &quot; , strReadIn.substr(155,20));
if(strReadIn.substr(155, 1) == &quot;H&quot;)
{
strBatch = strReadIn.substr(155, 16);

intBL = 16;
}
else if(strReadIn.substr(155, 3) == &quot;SME&quot;)
{
strBatch = strReadIn.substr(155, 19);
intBL = 19;
}
else if(strReadIn.substr(155, 3) == &quot;DSI&quot;)
{
strBatch = strReadIn.substr(155, 19);
intBL = 19;
}
else if(strReadIn.substr(155, 5) == &quot;00000&quot;)
{
strBatch = strReadIn.substr(175, 25);
intBL = 25;
}
++lngNofBatches;
bToCheck = true;
continue;
}

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

++lngNofBatches;
}
//}
}
printf(&quot;Number of Batched Found = %ld\n&quot;, lngNofBatches);
return 0;
}
 
What problem are you having? If the file is not opening, I suspect that you are not providing the full path name in your command line argument.

One small stylistic comment. The code that I wrote was pure C++, it now is liberally sprinkled with C constructs such as char[] and functions like printf and strcpy. You should really pick one or the other.
 
I want to pass the file that contains the data as an argument.

This program will be executes from the command line as:
ChkBth asciifile


I am not having any luck.
print and strcpy appear on the vc++ help. I though that they are C++ functions.

 
Yes ojosVerdes, you're absolutely right!
Big mistake on my part, I forgot to include one thing:

strncpy(pszDest, pszSrc, 25);

should be:

strncpy(pszDest, pszSrc + 155, 25);


But I see you're already there...

Anyway:
sprintf, strcpy etc. are C++ functions. Just because they're also used in pain C doesn't mean they're not C++. C++ is just an extension on the C language. Personally, I always stay away from string CString and such (whenever I can) and always use plain char* (or rather: _TCHAR*), sprintf etc.
Greetings,
Rick
 
this is not working, Please help

it looks like it does not even open the file. Please take a look,

TIA.

Ed


#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;
long lngNofBatches = 0;

//Code to initialize strReadIn///////////

// If the number of arguments passed is not = 2 then dispaly a message and
// stop program execution.
/*
if (argc != 2)
{
printf(&quot;Usage: ChkBth asciifile\n&quot;);
printf( &quot;Arg 0\n &quot; , argv[0]);
printf( &quot;Arg 1\n &quot; , argv[1]);
return -1 ;
}
else
{
strcpy(filename, argv[1]); // Get 2nd command line string and store it in the filename var

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

//ifstream infile(&quot;input.txt&quot;);
//istream fin(infile);
//while(fin >> strReadIn )
ifstream infile(&quot;i:\\client\\rjr\\85y001.txt&quot;);
istream fin(infile);
while(fin >> strReadIn)
{
if(!bToCheck)
{
if(strReadIn.size() < 200)
{
strBatch = strReadIn.substr(1, 25);
intBL = 16;
printf( &quot; ed\t &quot; , strBatch);
bToCheck = false;
continue;
}

if(strReadIn.substr(155, 1) == &quot;H&quot;)
{
strBatch = strReadIn.substr(155, 16);
intBL = 16;
//printf( &quot; ed\t &quot; , strBatch);
}
else if(strReadIn.substr(155, 3) == &quot;SME&quot;)
{
strBatch = strReadIn.substr(155, 19);
intBL = 19;
}
else if(strReadIn.substr(155, 3) == &quot;DSI&quot;)
{
strBatch = strReadIn.substr(155, 19);
intBL = 19;
}
else if(strReadIn.substr(155, 5) == &quot;00000&quot;)
{
strBatch = strReadIn.substr(175, 25);
intBL = 25;
}

lngNofBatches++;
bToCheck = true;
continue;
}

printf( &quot;got her &quot;);
//if(strReadIn.substr(1,intBL) != strReadIn.substr(155,intBL))
if(strBatch != strReadIn.substr(155,intBL))
{

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

lngNofBatches++;
}
//}
}
printf(&quot;Number of Batched Found = %ld\n&quot;, lngNofBatches);
return 0;
}
 
Although printf and strcpy etc. etc. can be used in C++, there are much simpler C++ constructs that can, and should be used in their place if you want to write robust C++ code.

For example strncpy(pszDest, pszSrc + 155, 25);

could be rewritten as pszDest = pszSrc with no concern over buffer overflow.

I am not trying to downplay the strengths of C in anyway, I just think if you want to code in C, code in C. If you want to code in C++, code in C++.
 
This code should work. Are you absolutely sure that you have the path correct?

Put this line..

Code:
cout << strReadIn << endl;

after the line

Code:
while(fin >> strReadIn)

and make sure that your file contents are printed to the screen. If not, your path is not correct.
 
Federal102,

the records in my text file are delimeted by carriagereturn and line feed, they are fix length.

However the strReadIn is treating the record as a space delimeted.

How do I fix that?

Thanks
ifstream infile(&quot;input.txt&quot;);
istream fin(infile);
while(fin >> strReadIn )
////////////////////////////////////////
{

 
Federal102,
I got 28 diferent segments of data.

As soon as it find a space in the record, is treats those characters as a single record.

For example if the records looks like
aaaa bbbb cccc 1111 sdsds 1212 dadfas 121212 .....

it prints
aaaa
bbbb
cccc
1111
sdsds
....

I want it to treat the entire line as one single record.

Thanks


 
OK to read in a line as single record, replace

Code:
while (fin >> strReadIn)

with

Code:
while(getline(fin, strReadIn))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top