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";
}

New Topic/Question
Reply




MultiQuote






|