7 Replies - 2777 Views - Last Post: 18 June 2009 - 09:09 AM

#1 Servatis  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 394
  • Joined: 30-March 09

converting from C# to C++ without .net

Posted 17 June 2009 - 03:53 AM

Hi,

I have created a C# console application which obviously uses .net.
the problem is however that it is supposed to run via citrix which as it turns out does not like .net.

I was thinking, maybe I can convert it to a Win32 Console app with C++.
the problem is I have absolutely no C++ experience.

I used a converter application on my C# project and this is the C++ code i got.

#pragma once

using namespace System;
using namespace System::Collections;
using namespace System::IO;

namespace ConsoleApplication2
{
	private ref class Program
	{
		static void Main(array<System::String^> ^args)
		{
			//char enumerator that will get new valeus every time a new line is called from the list.
			CharEnumerator ^lineCharEnum;
			//ArrayList to hold the information of a single article until the article is complete.
			ArrayList ^singleArticle = gcnew ArrayList();
			//string to hold the id that should be added to the end of each line.
			System::String ^pdmId = nullptr;
			//string to hold the article number to be added to the start of each line.
			System::String ^articleNumber = nullptr;
			//string to hold the assebled new line to be added to the singleArticle ArrayList.
			System::String ^currentLine = nullptr;
			//string to hold the current line that has been read and is being used by the application.
			System::String ^lineFromTxt = nullptr;
			//strings to hold locations of source and destination files
			//System::String ^source = "\\\\hmbv3d\\keytechData\\ERP\\Tekstblokken.txt";
			//System::String ^destination = "\\\\hmbv3d\\keytechData\\ERP\\CAM-Import-Arttxt.txt";
			System::String ^source = "C:\\tekstblokCAM.txt";
			System::String ^destination = "C:\\test.txt";
			//int to check if the current char is the start of a new line.
			int newLineCheck = 0;
			//int to check if the first accolade in the line has already been used in the code.
			int accoladeCheck = 0;
			//int to count the ammount of lines in one article.
			int numberOfLinesIndex = 1;
			int amountOfArticles = 0;

			try
			{
				//Use StreamReader to read from a file.
//INSTANT C++ NOTE: The following 'using' block is replaced by its C++ equivalent:
//				using (StreamReader sr = new StreamReader(source))
				StreamReader ^sr = gcnew StreamReader(source);
				try
				{
					try
					{
						//Use StreamWriter to write to a seperate file.
//INSTANT C++ NOTE: The following 'using' block is replaced by its C++ equivalent:
//						using (StreamWriter sw = new StreamWriter(destination))
						StreamWriter ^sw = gcnew StreamWriter(destination);
						try
						{
							try
							{
								//use a while loop to run through the entire source file.
								while (sr->Peek() > 0)
								{
									lineFromTxt = sr->ReadLine();
									//Reset valeus of all variables to set up the process for the new line.
									lineCharEnum = lineFromTxt->GetEnumerator();
									currentLine = nullptr;
									pdmId = nullptr;
									newLineCheck = 0;
									accoladeCheck = 0;

									//Use MoveNext in the char enumeration to run through every 
									//char in the current line.
									while (lineCharEnum->MoveNext())
									{
										//Use the newLineCheck variable to see if the current char
										//is the first character in the enumeration.
										if (newLineCheck == 0)
										{
											//Check if the new line is also the start of a new article.
											//Also check if the line is longer than 14 characters.
											//There are lines that are shorter than 14 characters but they are 
											//never new articles. The line.Substring generates an error 
											//if there are not enough characters.
											if (lineCharEnum->Current == '{')
											{
												articleNumber = lineFromTxt->Substring(1, 14);
												newLineCheck = 1;
												numberOfLinesIndex = 1;
											}
											//If not a new article then add the article number to 
											//front of the line with the numberOfLinesIndex indicating 
											//which line it is.
											else
											{
												accoladeCheck = 1;
												currentLine += (articleNumber + ";" + numberOfLinesIndex + ";" + lineCharEnum->Current);
												newLineCheck = 1;
												numberOfLinesIndex++;
											}
										}
										//check if } can be found. 
										else if (lineCharEnum->Current == '}')
										{
											//see if it is the first or second accolade found in the current line.
											if (accoladeCheck == 1)
											{
												//if its the second. add all the characters behind the } to PdmID.
												while (lineCharEnum->MoveNext())
												{
													pdmId += lineCharEnum->Current;
												}
											}
											//if its the first accolade add the number of lines behind the article 
											//number which is already present in the currentLine string.
											else
											{
												currentLine += (";" + numberOfLinesIndex + ";");
												accoladeCheck = 1;
												numberOfLinesIndex++;
											}
										}
										//the { Access used to wrap the content of a field in should not be 
										//added to the file
										else if (lineCharEnum->Current == '{')
										{
										}
										//any other character should just be added to the currentLine string.
										else
										{
											currentLine += lineCharEnum->Current;
										}
									}

									//at the end of constructing the lines they should be added to the 
									//singleArticle ArrayList.
									singleArticle->Add(currentLine);

									//the end of the article is identified by the pdmId therefore if pdmId 
									//is not empty all the contents of singleArticle should be written to 
									//the new txt file and the pdm Id should be added to the end of each line.
									if (pdmId != nullptr)
									{
										for each (System::String ^firstSectionOfLine in singleArticle)
										{
											//voor pdm id, veranderen naar (firstSectionOfLine + ";" + pdmId)
											//code voor opslaan id loopt nogsteeds.
											sw->WriteLine(firstSectionOfLine);
										}
										amountOfArticles++;
										singleArticle->Clear();
									}
								}
							}
							catch(...)
							{
								Console::WriteLine("Bestand wel gevonden. Fout tijdens converteren, Bij het uitlezen van de characters.");
							}
						}
						finally
						{
							delete sw;
						}
						Console::WriteLine((Convert::ToString(amountOfArticles)) + " Artikelen geconverteerd.\nBewerking voltooid.\n\nText bestand opgeslagen op lokatie: " + destination);
					}
					catch(...)
					{
						Console::WriteLine("Bestand wel gevonden. Fout tijdens converteren, bij StreamWriter");
					}
				}
				finally
				{
					delete sr;
				}
				File::Delete(source);
			}
			catch(...)
			{
				Console::WriteLine("Kan bewerking niet uitvoeren controleer of het bron bestand op de goede plek staat. Fout bij het lezen van het bestand");
			}
		}
	};
}



thing is, I get a whole bunch of "undeclared identifier" and some other errors, which was to be expected.

my question is; is there any way I can get this code to work without .NET?

Is This A Good Question/Topic? 0
  • +

Replies To: converting from C# to C++ without .net

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: converting from C# to C++ without .net

Posted 17 June 2009 - 05:46 AM

rewriting the program as a C/C++ console program is certainly possible. Using the above code -- No.

I mean... in theory you could write a program that would translate the source from 1 language to the other... but that would be quite a bit of work for what you are doing.

So it would probably be easier to just start fresh with a new C++ program. The above program is well commented so it is easy to see what is going on, you just have to figure out how to do it in C++.
Was This Post Helpful? 1
  • +
  • -

#3 Servatis  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 394
  • Joined: 30-March 09

Re: converting from C# to C++ without .net

Posted 17 June 2009 - 06:10 AM

View PostNickDMax, on 17 Jun, 2009 - 04:46 AM, said:

rewriting the program as a C/C++ console program is certainly possible. Using the above code -- No.

I mean... in theory you could write a program that would translate the source from 1 language to the other... but that would be quite a bit of work for what you are doing.

So it would probably be easier to just start fresh with a new C++ program. The above program is well commented so it is easy to see what is going on, you just have to figure out how to do it in C++.



thank you for your reply.

not the answer i was hoping for, since I have no C/C++ experience it's going to be a a lot more work for such a small application.


any tips on where I should start would come in handy.

the application should be able to read a text file and write/edit it into another text file, like in the C# application.
but since I can't even find the string variable, I won't get this done any time soon.
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: converting from C# to C++ without .net

Posted 17 June 2009 - 07:14 AM

Start with the purpose of the program. Don't get hung up on the details of how it was done in C# since this may not be the best way to do it in C++.

It looks like you are filtering something (perhaps source code). What IS the purpose of this code?
Was This Post Helpful? 0
  • +
  • -

#5 Servatis  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 394
  • Joined: 30-March 09

Re: converting from C# to C++ without .net

Posted 17 June 2009 - 07:41 AM

I exported a text file from an access table.

using { and } as field separators and text separators (these are the only characters not in the table itself, so csv won't work.)

with this app I'm going through that text file character by character.
with these characters I "build" a string for each new line. I add/remove and move text according to which character the app finds.
for each "article" I put the strings that were built by the app in a string arraylist.
when the app finds the end of the article that entire arraylist gets written to a text file.
it keeps doing this for each article and adds them to the new text file.

so basically it converts a text file into another text file.

the thing is, I don't think i can do all that by only using char's, because sometimes I have to add things to the beginning of the line while the "trigger" for that is in the middle of the line it is reading.
so I need a string or something that is similar.

I think I can work without the string arraylist because I don't really need the pdmId variable (that was in the initial application but had to be taken out.)

I hope I explained it clearly, English is not my first language.

Edit: to make it clearer here are examples of the text files.

source txt file:

Quote

{150782202 {}{ZEEFPLAAT, GATEN 0 20mm, GEBOORD, AISI 316{}1086
{1508001000 {}{Menger 10-LFC-43 is menger van ordernr.
6025301000 Intact
T.b.v. van beurs menger (POS1) uitvoeren
EXCLUSIEF POSNRS 25 en 51!!!!!!!!!!!!!!!
Lafontaineschuif DN300 onder menger vervangen
door rotaryvalve DN300 (POS2, 1508002000).{}1090
{1508002000 {}{Levering Tebu (1stuks) na beurs retour{}1097


destination txt file:

Quote

150782202 ;1;ZEEFPLAAT, GATEN 0 20mm, GEBOORD, AISI 316
1508001000 ;1;Menger 10-LFC-43 is menger van ordernr.
1508001000 ;2;6025301000 Intact
1508001000 ;3;T.b.v. van beurs menger (POS1) uitvoeren
1508001000 ;4;EXCLUSIEF POSNRS 25 en 51!!!!!!!!!!!!!!!
1508001000 ;5;Lafontaineschuif DN300 onder menger vervangen
1508001000 ;6;door rotaryvalve DN300 (POS2, 1508002000).
1508002000 ;1;Levering Tebu (1stuks) na beurs retour

This post has been edited by Servatis: 17 June 2009 - 07:46 AM

Was This Post Helpful? 0
  • +
  • -

#6 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: converting from C# to C++ without .net

Posted 17 June 2009 - 08:14 AM

I agree with Nick. It would be easier to just start over in C++ instead of trying to use what you have and convert it.

Here is a snippet that shows how to read from and write to a text file in C++. I hope this helps you!

http://www.dreaminco...htm#lastComment

put
#include<iostream>
#include<fstream>
at the top of the program
Was This Post Helpful? 0
  • +
  • -

#7 Servatis  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 394
  • Joined: 30-March 09

Re: converting from C# to C++ without .net

Posted 18 June 2009 - 12:44 AM

View PostOliveOyl3471, on 17 Jun, 2009 - 07:14 AM, said:

I agree with Nick. It would be easier to just start over in C++ instead of trying to use what you have and convert it.

Here is a snippet that shows how to read from and write to a text file in C++. I hope this helps you!

http://www.dreaminco...htm#lastComment

put
#include<iostream>
#include<fstream>
at the top of the program


thanks,

I tried the code but it is giving me problems when I try it in a win32 console app.
I can't use a regular forms app because that uses .net
Was This Post Helpful? 0
  • +
  • -

#8 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: converting from C# to C++ without .net

Posted 18 June 2009 - 09:09 AM

Have you tried using Dev-C++? That's not .NET. You can download it free, just Google Dev-C++.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1