Learn how to #run a c sharp and c plus plus #program in #Linux/#Unix. How to #read contents of an input file, process and #write those into an output file.
c++ code:
#include <iostream> //for cout
#include <fstream> // for ofstream/ifstream
#include <string> //for << and getline
int main(int argc, char *argv[])
{
std::cout << "There are " << argc << " arguments." << std::endl;
for (int i = 0; i != argc; ++i)
{
std::cout << argv[i] << std::endl;
}
std::string in_file = argv[1];
std::ifstream input_file(in_file.c_str());
std::string out_file = in_file + ".out";
std::ofstream output_file(out_file.c_str());
std::string line;
for (int line_no = 1; std::getline(input_file, line); ++line_no)
{
std::cout << "LINE " << line_no << ":" << line << std::endl;
output_file <<"LINE " << line_no << ":" << line << std::endl;
}
if (input_file)
{
input_file.close();
output_file.close();
}
std::cin.get();
return 0;
}
c# code:
using System;
using System.IO;//to use StreamReader
namespace lec1csharp
{
class Program
{
static void Main(string[] args)
{
int argc = args.Length;
Console.WriteLine("There are {0} arguments.", argc);
for (int i = 0; i != argc; ++i)
{
Console.WriteLine(args[i]);
}
StreamReader input_file = null;
StreamWriter output_file = null;
string in_file = args[0];
input_file = new StreamReader(in_file);
string out_file = in_file + ".out";
output_file = new StreamWriter(out_file);
string line = "";
for (int line_no = 1; (line = input_file.ReadLine()) != null; ++line_no)
{
Console.WriteLine("LINE {0} :{1}", line_no, line);
output_file.WriteLine("LINE {0} :{1}", line_no, line);
}
if (input_file != null)
{
input_file.Close();
output_file.Close();
}
Console.ReadLine();
}
}
}
#cplusplus #csharp #tutorial
How to run/compile C# application in Ubuntu/Linux
C# Files and Streams