Well, time to learn C#

improving on my skillset, I guess

Page 1 of 1

11 Replies - 973 Views - Last Post: 04 May 2010 - 12:06 AM Rate Topic: -----

#1 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Well, time to learn C#

Posted 02 May 2010 - 01:37 AM

Hi, I'm a frequent visitor to the C++ section, I think I'm going to learn C#. Anyone else done this second language thing yet? ;)

I have some code I want to convert to C#, just so I have a good understanding of C#, it's a C++ console application to split a larger file into smaller pieces and it works just fine. It doesn't use any classes I have made, it would be somewhat unnecessary.

But, how can I learn a good way to design the C# application? Any thoughts, or rough skeletons may help some, if this tactic doesn't work out, (i think it will) I could try another..

suggestions are always welcome.

//Solve++
//taylorc8
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstring> // memset()
using namespace std;


typedef unsigned int uint;


// Read specified size from File returns # of bytes read
int ReadFromFile(ifstream&, uint, char[]);

// Write buffer to output file
void WriteToFile(ofstream&, uint, char[]);

// Opens the file, returns zero on success
int OpenOutputFile(ofstream&, string);

//Open input file, zero on success
int OpenInputFile(ifstream&, string);

//CleanupRoutine, deletes memory, closes files.
inline void CleanupRoutine(ifstream&, ofstream&, char[]);

//Print message showing how-to use prog.
inline void Usage();

/*******************************************************************************
*********************************************************************************
*********************************************************************************
								Entry-Point
*********************************************************************************
*********************************************************************************
********************************************************************************/
int main(int argc, char* argv[])
{
	//First steps, make sure command line args are valid
	if(argc<3)
	{
		Usage();
		return 1;
	}

	uint BUFFER_SIZE=1024 * 16; // 16 K buffer Size
	uint PIECE_SIZE = (atoi(argv[2]) * 1024);

	//Make sure valid value entered for piece size!
	if(PIECE_SIZE == 0)
	{//If the user enters a negative number, the PIECE_SIZE will be massively large
		cout << "Default piece size being used. (16KB)" << endl;
		PIECE_SIZE = 1024*16;
	}
	else if(PIECE_SIZE < BUFFER_SIZE)
		BUFFER_SIZE=PIECE_SIZE;


	string inFileName=argv[1];//string for File Name
	string outFileName;

	ifstream inFile; //INPUT file stream
	ofstream outFile; //OUTPUT file stream
	stringstream fileNameSS; //stringstream for file name
	uint count=0; //to append to fileName string
	uint bytesRead=0; // count of bytes read during Read Operation

	//Allocate memory for array (buffer)
	char * buffer=NULL;
	try
	{
		buffer= new char[BUFFER_SIZE];
	}
	catch(std::bad_alloc)
	{
		cout << "Low-memory, attempting half-sized buffer~~" << endl;
		BUFFER_SIZE/=2;
		buffer = new char[BUFFER_SIZE];
	}
		
		
	memset(buffer,0,BUFFER_SIZE); // zero the buffer (possibly unneccessary)

	//Open input file
	if(OpenInputFile(inFile,inFileName)!=0)
	{
		cerr << "Error opening input file.\nTerminating...\n";
		CleanupRoutine(inFile,outFile,buffer);
		return 1;
	}

	do
	{
		uint totalRead=0;//accumulate total bytes read from file

		fileNameSS << inFileName << '.' << count << ".SC";
		outFileName=fileNameSS.str();
		fileNameSS.str(""); // clear stringstream
		count++; //incrementing filename extension

		//Open file
		if(OpenOutputFile(outFile,outFileName)==0)
		{
			while( (totalRead < PIECE_SIZE) && (outFile.good() && inFile.good()) )
			{
				//THIS IS THE DEFAULT CASE, Next section is for the final read operation
				if( (totalRead + BUFFER_SIZE) < PIECE_SIZE )
				{
					//read up to buffer_size
					bytesRead=ReadFromFile(inFile,BUFFER_SIZE, buffer);
					totalRead+=bytesRead;
					//Write
					WriteToFile(outFile, bytesRead, buffer);
				}
				else
				{//read certain portion (less than buffer size)
					bytesRead=ReadFromFile(inFile, (PIECE_SIZE - totalRead), buffer);
					totalRead+= bytesRead; // THIS WILL BE = TO PIECE_SIZE WHEN FINISHED
					WriteToFile(outFile, bytesRead, buffer);
					outFile.close();
					//**Finished with Piece (if this works properly, break; need not be used here.**
				}
			}//end while(totalRead < PIECE_SIZE)
		}
		else
		{
			cerr << "Error opening output file.\nTerminating...\n";
			CleanupRoutine(inFile,outFile,buffer);
			return 2;
		}

	}while( bytesRead && inFile.good() );


	CleanupRoutine(inFile,outFile,buffer);
	return 0;
}




int ReadFromFile(ifstream& inFile, uint size,char buffer[])
{// Read specified size from File
//returns # of bytes read
	inFile.read(buffer,size);
	return (int)inFile.gcount();
}

void WriteToFile(ofstream& outFile, uint size,char buffer[]) // Write buffer to file
{
	outFile.write(buffer,size);
}


int OpenOutputFile(ofstream& file,string name) // Opens output file,
//returns zero on success
{
	file.open(name.c_str(),ios::out | ios::binary);

	if(file.is_open())
		return 0;
	else
		return 1;
}

int OpenInputFile(ifstream& file,string name) //Open input file
{
	file.open(name.c_str(),ios::in | ios::binary);

	if(file.is_open())
		return 0;
	else
		return 1;
}
void CleanupRoutine(ifstream& inf, ofstream& outf,char buffer[])
{
	if(inf.is_open())
		inf.close();
	if(outf.is_open())
		outf.close();
	if(buffer!=NULL)
		delete [] buffer;
}




void Usage()
{
	cout <<  " Solve++.exe\nThis program is designed to split a larger file into smaller pieces.\n";
	cout <<  "The pieces will appear in the current directory with a file extension of .SC\n";
	cout << endl << "Usage:" << endl;
	cout << "Solve++ \"file_to_split\" <piece size in KB>" << endl;
	cout << "Example: Solve++ \"inputfile.exe\" 64";
	cout << endl << "Please see the usage for Cog++ for re-assembly.\n";
}


Is This A Good Question/Topic? 0
  • +

Replies To: Well, time to learn C#

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Well, time to learn C#

Posted 02 May 2010 - 04:08 AM

You'll be happy to know that the framework itself will actually do all of the above for you?

Take a look into the StreamReader and StreamWriter classes, or depending on the kind of files you're reading, TextReader and TextWriter :D
Was This Post Helpful? 2
  • +
  • -

#3 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Well, time to learn C#

Posted 02 May 2010 - 02:28 PM

I'm actually asking about Object Oriented Design for C# for making that application, like what classes and how they will work together so I understand a little of C# OOD.
Was This Post Helpful? 0
  • +
  • -

#4 FreezingHazard  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 148
  • Joined: 22-February 10

Re: Well, time to learn C#

Posted 02 May 2010 - 03:00 PM

To be honest don't go for C# if you already know C++.

C++ and C# are practically the same thing. I know C# really well and can say compared to C++ there is no difference except XNA and C# is extremely easy compared to C++.

Also C++ has over the top amount of job options compared to C#, I don't know why this is, I personally think it's because most people that are hiring are just picking C++ because of the hype of how it's "the best", not really knowing what is "actually" better or worse, and the truth is they can perform equally, it's all about what one matches your style.

In C# you can also run unsafe code, and I believe C++ is used for PS3 programming, that or Java. I'm unsure.

But stick with what you got, because the change will not do anything I promise.
Was This Post Helpful? -1
  • +
  • -

#5 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Well, time to learn C#

Posted 02 May 2010 - 04:41 PM

View PostFreezingHazard, on 02 May 2010 - 09:00 PM, said:

To be honest don't go for C# if you already know C++.

C++ and C# are practically the same thing. I know C# really well and can say compared to C++ there is no difference except XNA and C# is extremely easy compared to C++.

Also C++ has over the top amount of job options compared to C#, I don't know why this is, I personally think it's because most people that are hiring are just picking C++ because of the hype of how it's "the best", not really knowing what is "actually" better or worse, and the truth is they can perform equally, it's all about what one matches your style.

In C# you can also run unsafe code, and I believe C++ is used for PS3 programming, that or Java. I'm unsure.

But stick with what you got, because the change will not do anything I promise.

The change will do a HELL of a lot.

There's a tonne of difference between C++ and C#... A really really really really lot of huge differences. Java is a language that is compiled and ran in a virtual machine and certainly is not used on the PS3.

C# and C++ can't perform equally on everything, C# outperforms C++ in many areas when used correctly, and vice versa. Not sure where the point came from about employment, I've never seen a single C/C++ job on the market for the same as a .NET Developer, at least in the UK ;)
Was This Post Helpful? 0
  • +
  • -

#6 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Well, time to learn C#

Posted 02 May 2010 - 10:12 PM

I'm just looking to improve my skillset, I am not even college educated yet, I learned a *great* deal of C++ at home, through middle school, and now into attending a community college, I really do still want to learn C#, not that I would let it take time away from my C++. My instructor is terrible, I know how to use C++ and I've read the entire book used for the course and can use anything in it. I'm ready for the university, in short. But I want to learn another language than C++, and it's not because I'm tired of C++, I love it, in fact. I think another language, even recommended by Bjarne Stroustrup, learning it would be a good thing, and would give me more perspective on C++ and other languages. C# seems to be not a bad option, I think it would be used in a lot of applications, but if there is another option that would be good (besides C, I can use it) that input would be great (another language, coming from C# users?).

I want to learn C# for fun, I will become a CS programmer. I don't care how hard I have to work in college, or even on the job. I like to program, the challenge, and also the rewards.

If someone has a good way to describe the design, (to my own mind I am a master at this) some pointers would be great.

If you looked through my comments, which I've omitted from the prog. I posted, you may understand how I describe what it is I try to do. I could probably write a book on learning C++ basics. Not to brag, I did learn C++ myself. The college, actually my professor, recommended a great book to cover many things I didn't know about C++, and it emphasizes object oriented design which I have, in later programs adopted to a great extent. The book is not complete, none of them are. I read the entire book myself.

"Programming and problem solving with C++" Oh, and I'm an idiot, I just put waaay too much time into programming, because I love it. Now, I'm hoping I can get a degree and a job doing it :)

The book that started me programming with C++ was "Ivor Horton's Beginning Visual C++ 6.0" and Ivor Horton is a freaking *wonderful* teacher!

Oh, and while in middle school C++ did my math homework, nothing to be too awfully proud of, but still cool :)


Now, after learning of object oriented design concepts, I'm wondering how to apply that to C#? C++ from what I've read has somewhat of an odd approach to OOP. If someone has an excellent lay-man way to explain designing the C# application to do the same thing, I would seriously read and enjoy it!

after all the typing, programming is a fun hobby, and now the middle school kid who lives in the middle of a corn field that liked to program is trying to get a degree and a job. Not to mention learn C# or another fun language!
Was This Post Helpful? 0
  • +
  • -

#7 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Well, time to learn C#

Posted 02 May 2010 - 10:52 PM

so... about the design of an application to split a file into smaller pieces, each with an integer before the final .SC extension so I can identify them to re-assemble them.. I want to make a C# application to do the same thing the C++ application I have posted does, now, how should the design be like? Give me a tip or two about object oriented design with C#, please!

How would *you* make this program?


wait... freezing hazard just said C++ and C# are nearly the same thing...?

I don't want to vote him down for it, but correct him immediately! If you're going to bring jobs into this, then don't lie to me, and tell me if .NET is going to be a good choice over C++ used for games or any occupation worth my time. How is the job market today, language-wise?

This post has been edited by taylorc8: 02 May 2010 - 10:57 PM

Was This Post Helpful? 0
  • +
  • -

#8 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Well, time to learn C#

Posted 03 May 2010 - 04:46 AM

View Posttaylorc8, on 03 May 2010 - 04:52 AM, said:

so... about the design of an application to split a file into smaller pieces, each with an integer before the final .SC extension so I can identify them to re-assemble them.. I want to make a C# application to do the same thing the C++ application I have posted does, now, how should the design be like? Give me a tip or two about object oriented design with C#, please!

How would *you* make this program?

Basically once you have the skills to design an application, I'm sure you could design it just fine.

All you have to remember is that in C#, everything is an object. C/C++ is kindof hybrid in this matter, you can have methods outside of objects etc etc.

I'm not sure what pointers I could give you about design, but if you want to post what you think it should be like.. I'd be happy to take a look and comment on it.

With your exact application, all I can say is that I'd obviously have a Program class, containing the main application logic. All of the actual working could be split out into 2 other classes, FileJoiner and FileSplitter, containing basic methods to split and reconstruct files.

View Posttaylorc8, on 03 May 2010 - 04:52 AM, said:

wait... freezing hazard just said C++ and C# are nearly the same thing...?

I don't want to vote him down for it, but correct him immediately! If you're going to bring jobs into this, then don't lie to me, and tell me if .NET is going to be a good choice over C++ used for games or any occupation worth my time. How is the job market today, language-wise?

That's what my last reply addressed ;) .NET is definitely a very wanted language, you could look on any IT Job board (granted I'm talking from the UK, but I'm going to assume it's the same in the US) and you will find a tonne of .NET jobs.. It's very rare that you'd see a C/C++ development position, apart from Embedded or Linux Development.
Was This Post Helpful? 1
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Well, time to learn C#

Posted 03 May 2010 - 06:32 AM

View PostFreezingHazard, on 02 May 2010 - 05:00 PM, said:

To be honest don't go for C# if you already know C++.

C++ and C# are practically the same thing. I know C# really well and can say compared to C++ there is no difference except XNA and C# is extremely easy compared to C++.

Also C++ has over the top amount of job options compared to C#, I don't know why this is, I personally think it's because most people that are hiring are just picking C++ because of the hype of how it's "the best", not really knowing what is "actually" better or worse, and the truth is they can perform equally, it's all about what one matches your style.

In C# you can also run unsafe code, and I believe C++ is used for PS3 programming, that or Java. I'm unsure.

But stick with what you got, because the change will not do anything I promise.


Are you referring to C++ vs. C#(the language itself) or C++ vs. .Net? If you are referring to the latter, I would have to disagree with almost every point you made.
Was This Post Helpful? -1
  • +
  • -

#10 Dannyboy997  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 64
  • View blog
  • Posts: 258
  • Joined: 17-April 09

Re: Well, time to learn C#

Posted 03 May 2010 - 08:33 AM

As mentioned above: C# as XNA, so if your going to go into game programming then this would be worth it. I know that c++ as Dark GDK, but I think XNA is much easier and more powerful on what you can do. I know that you can virtually do everything that XNA does in C++ with Dark GDK.


Another Plus, is that XNA can export to the Xbox 360. Which can be releast on the LIVE market place.


For a Visual App. C# is much easier to use. I tried to create a visual application in C++, but it was so complicated. I just did the same in C# and it was so easy.


So C# is very powerful and easy to use in some cases. I started learning it a couple of weeks ago, and I have almost all of the basics and some advanced concepts.
Was This Post Helpful? 1
  • +
  • -

#11 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Well, time to learn C#

Posted 03 May 2010 - 09:02 PM

Quick question, how do I pass a FileStream by reference?

I think I'm going to have:

public class FileSplitter
{

public class FileOperations // Re-usable?
{
//OpenFile() function
//ReadFromFile() function
//WriteToFile() function
//CloseFile() function
}

public static int Split(String inputFileName, uint pieceSize)
//to be called and do the actual work, needs a couple pieces of info
//in the param. list, input file, piece size


}


that will probably be the whole design, the actual work will be in FileSplitter.Split() and I plan to use functions in the class FileOperations in Split().

FileStream inFileStream = new FileStream(inFileName, FileMode.Open);//open existing
                BinaryReader inFileReader = new BinaryReader(inFileStream);


I have this code written for doing some binary file stuff, I plan to pass the BinaryReader to functions that need to read, and a BinaryWriter to the functions that write. If there is a better way, let me know.

Also, suggestions about the design anyone?
Was This Post Helpful? 0
  • +
  • -

#12 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Well, time to learn C#

Posted 04 May 2010 - 12:06 AM

View Posttaylorc8, on 04 May 2010 - 03:02 AM, said:

Quick question, how do I pass a FileStream by reference?

Well, you shouldn't. You should just create an instance of the FileOperations class and have a member variable that is the FileStream you're going to work with.

Your main method shouldn't really be static, unless the whole class is going to be static.. Not because of best practice or anything, just because of what you're doing.

View Posttaylorc8, on 04 May 2010 - 03:02 AM, said:

Also, suggestions about the design anyone?

Are you missing my replies? ;)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1