Read Write File (handling) in c++ and c# Character by Character with Char 46

Опубликовано: 14 Октябрь 2024
на канале: Arif Mahmood
360
1

Shows how to read a file with #characters. How to change #char to #string. How to #read a file letter by letter 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::fstream input_file("test.txt");
//std::ifstream input_file("test.txt");
std::ofstream output_file("test.txt.out");

char line;

//while (input_file >> std::skipws >> line)
////for (;input_file >> std::noskipws >> line;)
//{
// std::cout << line << std::endl;
// output_file << line << std::endl;
// //std::cout << std::string(1, line)<<std::endl;
// //output_file << std::string(1, line)<<std::endl;
//}

for (;input_file.get(line);)
{
std::cout << line << std::endl;
output_file << line << std::endl;
}

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

//for (; !input_file.EndOfStream;)
//{
// char line = (char)input_file.Read();
// //Console.WriteLine(line);
// //output_file.WriteLine(line);
// Console.WriteLine(Char.ToString(line));
// output_file.WriteLine(Char.ToString(line));
//}


string input_file = File.ReadAllText("test.txt");
foreach (Char line in input_file)
{
Console.WriteLine(line);
output_file.WriteLine(line);
}

Console.ReadLine();
}
}
}

#csharp #cplusplus #tutorial

How to Read and Write to Text File