Shows how to do file handling in c plus plus and c sharp using #GUI on visual studio 2017 in order to #read from a file and #write into a file
C++ code:
//Myform.cpp is another file
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
int main()
//int main (array<String^>^args )
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
guicpp::MyForm form;
Application::Run(%form);
return 0;
}
//Myform.h is one file
#pragma once
namespace guicpp {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
……………………………
……………………………..
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
//StreamReader^ input_file = gcnew StreamReader("test.txt");
//StreamWriter^ output_file = gcnew StreamWriter("test.txt.out");
StreamReader^ input_file = File::OpenText("test.txt");
StreamWriter^ output_file = File::CreateText("test.txt.out");
//StreamWriter^ output_file = File::AppendText("test.txt.out");
String^ line;
//while ((line = input_file->ReadLine()) != nullptr)
//{
// MessageBox::Show(line);
// output_file->WriteLine(line);
//}
line= input_file->ReadToEnd();
MessageBox::Show(line);
output_file->WriteLine(line);
if (input_file)
{
input_file->Close();
output_file->Close();
}
}
};
}
.C# code:
//Form1.cs is one file
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace GUICS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//StreamReader input_file = new StreamReader("test.txt");
//StreamWriter output_file = new StreamWriter("test.txt.out");
StreamReader input_file = File.OpenText("test.txt");
StreamWriter output_file = File.CreateText("test.txt.out");
//StreamWriter output_file = File.AppendText("test.txt.out");
String line;
//while ((line = input_file.ReadLine()) != null)
//{
// MessageBox.Show(line);
// output_file.WriteLine(line);
//}
line = input_file.ReadToEnd();
MessageBox.Show(line);
output_file.WriteLine(line);
if (input_file != null)
{
input_file.Close();
output_file.Close();
}
}
}
}
#cplusplus #csharp #tutorial
Tutorial Create a text file and write in it using C#
File Input output in GUI application C#
Creating the base gui app in visual studio