Divide/Split Code to Multiple (source/header/class) files in c++ and c# on Visual Studio 2017 11

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

Learn how to add #multiple #files to a single file #program and #divide/#split #code into multiple files. How to #compile and #run a #project with multiple #source,#class and #header files in c plus plus and c sharp projects.

c++ code:
//Source.cpp is a source file
#include <iostream> //for cout
#include <fstream > // for ofstream/ifstream
#include <string > //for << and getline
#include <vector > //for vectors
#include<list > //for lists
#include "Header.h"

//class file_element
//{
//public:
// std::string line;
//};

int main(int argc, char *argv[])
{
std::ifstream input_file;
input_file.open("test.txt");

std::ofstream output_file("test.txt.out");

std::string line;

std::list< file_element >elements;

for (int line_no = 1; std::getline(input_file, line); ++line_no)
{
file_element element
{
line = line
};

elements.push_back(element);
}

std::list< file_element >::iterator iter = elements.begin();
for (; iter != elements.end(); ++iter)
{
std::cout << (*iter).line << std::endl;
output_file << (*iter).line << std::endl;
}

if (input_file)
{
input_file.close();
output_file.close();
}
std::cin.get();
return 0;
}


//Header.h is a header file
#pragma once
#ifndef GUARD_TOKENS_H
#define GUARD_TOKENS_H

class file_element
{
public:
std::string line;
};

#endif


c# code:
//Program.cs is a class file
using System;//for Console
using System.Collections.Generic;//for lists
using System.IO;//to use StreamReader

namespace lec2csharp
{
//class File_element
//{
// public string line;
//};
//partial class Program
class Program
{
static void Main(string[] args)
{
StreamReader input_file = null;
input_file = new StreamReader("test.txt");

StreamWriter output_file = new StreamWriter("test.txt.out");

string line = "";

//List <File_element >elements = new List< File_element >();
List< Header.File_element >elements = new List< Header.File_element >();

//for (int line_no = 1; (line = input_file.ReadLine()) != null; ++line_no)
//{
// File_element element = new File_element
// {
// line = line
// };
// elements.Add(element);
//}

for (int line_no = 1; (line = input_file.ReadLine()) != null; ++line_no)
{
Header.File_element element = new Header.File_element
{
line = line
};
elements.Add(element);
}

//foreach (File_element iter in elements)
//{
// Console.WriteLine(iter.line);
// output_file.WriteLine(iter.line);
//}

foreach (Header.File_element iter in elements)
{
Console.WriteLine(iter.line);
output_file.WriteLine(iter.line);
}

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.Linq;
using System.Text;
using System.Threading.Tasks;

namespace lec2csharp
{
//class Class1
//{
//}
//partial class Program
//{
// class File_element
// {
// public string line;
// };
//}
class Header
{
public class File_element
{
public string line;
};
}
}

#cplusplus #csharp #tutorial

Partial classes in c#
C# Tutorial How to group partial class files in Solution