Convert String to Char in c++ and c# 45

Опубликовано: 02 Июнь 2026
на канале: Arif Mahmood
8,252
37

Shows how to read from a file with string and #convert #string into #char in c plus plus and c sharp.

c++ code:
#include <iostream> //for cout
#include <fstream> // for ofstream/ifstream
#include <string> //for << and getline
int main()
{
std::ifstream input_file("test.txt");
std::ofstream output_file("test.txt.out");

std::string line;
for (int line_no = 1; std::getline(input_file, line); ++line_no)
{
for (size_t i = 0; i < line.size(); i++)
{
//std::cout << line[i] << std::endl;
//output_file << line[i] << std::endl;
std::cout << std::string(1, line[i]) <<std::endl;
output_file << std::string(1, line[i]) << std::endl;
}
//std::cout << line << std::endl;
//output_file << line << std::endl;
}

if (input_file)
{
input_file.close();
output_file.close();
}

std::cin.get();
return 0;
}



C# Code:
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 = new StreamReader("test.txt");
StreamWriter output_file = new StreamWriter("test.txt.out");

string line = "";
for (int line_no = 0; (line = input_file.ReadLine()) != null; line_no++)
{
for (int i = 0; i < line.Length; ++i)
{
//Console.WriteLine(line[i]);
//output_file.WriteLine(line[i]);
Console.WriteLine(Char.ToString(line[i]));
output_file.WriteLine(Char.ToString(line[i]));
}
//Console.WriteLine(line);
//output_file.WriteLine(line);
}

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

Console.ReadLine();
}
}
}

#cplusplus #csharp #tutorial

How to Split String in C# String Split based on any Char