Split Code to Mutiple (source/header/class) files in c++ and c# with Command prompt 39

Опубликовано: 16 Февраль 2026
на канале: Arif Mahmood
76
1

Learn how to #compile and #run a c plus plus and c sharp #project with #multiple #files in #command #prompt. How to enable optimization and catch programming mistakes. How to #divide/#split #code into #separate #source, #header and #class files.

C++ Code:


C# Code:
//Program.cs is a class file
using System;
using System.Collections.Generic;//for string
using System.IO;//to use StreamReader

namespace lec2csharp
{
class Program
{
static void Main(string[] args)
{
StreamReader input_file = null;
StreamWriter output_file = null;

string file = "test.txt";

List<File_element> elements = new List<File_element>();
elements.Clear();

Header.Pullout_contents(file, elements);

Header.Show_contents(output_file, elements);

Header.Pushin_contents(file + ".out", elements);

if (input_file != null)
{
input_file.Close();
output_file.Close();
}
Console.ReadLine();
}
}
}


//Class1.cs is another class file
using System;
using System.Collections.Generic;
using System.IO;//to use StreamReader

namespace lec2csharp
{
class File_element
{
internal string line;
};

class Header
{
internal static void Pullout_contents(string in_file, List<File_element> elements)
{
StreamReader input_file = new StreamReader(in_file.ToString());
elements.Clear();
string line = "";
for (int line_no = 1; (line = input_file.ReadLine()) != null; ++line_no)
{
File_element element = new File_element();
element.line = line;
elements.Add(element);
}
}

internal static void Show_contents(StreamWriter Out, List<File_element> elements)
{
foreach (File_element iter in elements)
{
Console.WriteLine(iter.line);
}
}

internal static void Pushin_contents(string out_file, List<File_element> elements)
{
StreamWriter output_file = new StreamWriter(out_file.ToString());
output_file.AutoFlush = true;
Console.SetOut(output_file);
Show_contents(output_file, elements);// using polymorphism
}
}
}


#cplusplus #csharp #tutorial


run command prompt commands from within a C# application
C# Tutorial Console Application in .Net