13 Replies - 1483 Views - Last Post: 29 June 2017 - 08:51 AM

#1 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 239
  • Joined: 29-October 16

What's the Easiest Way to Write a String to File?

Posted 17 January 2017 - 03:25 PM

Hello all,
So I'm writing a program in CLI/C++ which will take a string from a textbox and write that string to a file. I have no problem getting the fstream commands to work when I compile a command line exe but when I write a program in CLI/C++[CLR/UI project] I get a compiler error that says:


___
no operator "<<" matches these operands
____



And it's referencing this command "myfile << x;"
Here is my code:

			

#include <Windows.h>
#include <string>
#include <iostream>
#include <fstream>


using namespace System;
using namespace std;


String^ x = textBox1->Text;
			textBox2->Text = x;
			
			ofstream myfile;
			ofstream myfile("example1.txt");
			myfile << x;
			myfile.close();


I'm having trouble finding a good answer on google or any of the forums I've checked, and i've been searching all day. Is there something that I need to include at the top of the file?

Really maybe I'm doing this all wrong but I'm just trying to take a string and write it to a file... shouldn't be very difficult but every solution I try is giving compiler errors or is missing some header file.

Can anyone give me a quick answer about the commands I need to use to write a string to a file in CLI/C++ or .NET? Thanks and have a great day!

Is This A Good Question/Topic? 0
  • +

Replies To: What's the Easiest Way to Write a String to File?

#2 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 239
  • Joined: 29-October 16

Re: What's the Easiest Way to Write a String to File?

Posted 19 January 2017 - 10:27 AM

My gut feeling is that this should not be as difficult as it is... so noone knows what the difference in syntax and header files is for outputting in CRC vs Command line???
Was This Post Helpful? 0
  • +
  • -

#3 Thomas1965  Icon User is offline

  • D.I.C Head

Reputation: 51
  • View blog
  • Posts: 198
  • Joined: 09-September 16

Re: What's the Easiest Way to Write a String to File?

Posted 20 January 2017 - 03:15 PM

When using C++/CLI it's best to forget normal C++.
try
{
  System::IO::File::WriteAllText("example.txt", textBox1->Text);
}
catch (Exception^ ex)
{
 MessageBox::Show(ex->Message);
}

Was This Post Helpful? 1
  • +
  • -

#4 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 239
  • Joined: 29-October 16

Re: What's the Easiest Way to Write a String to File?

Posted 20 January 2017 - 04:34 PM

//#pragma once
#include <string>
#include <iostream>
#include <stdlib.h>
#include <Windows.h>
#include <fstream>
#include <msclr\marshal_cppstd.h>

namespace SendToFile {

	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 std;
	using namespace msclr::interop;
	static string mystr;
	static string mystr2;
	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
		//static string mystr;
	public:
		Form1(void)
		{
			//static string fff;
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}

	private: System::Windows::Forms::TextBox^  textBox1;
	protected:

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->SuspendLayout();
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(13, 42);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(100, 20);
			this->textBox1->TabIndex = 1;
			this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(284, 261);
			this->Controls->Add(this->textBox1);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
		//static string mystr;
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
	{
		//string mystr;
		//mystr = "writethistofile";
		ofstream myfile;
		myfile.open("example.txt");
		myfile << mystr;
		myfile.close();




	}

	private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e)
	{
		std::string mystrTEST = "test string to marshal";
	
		String^ result;
		result = textBox1->Text;
		result = marshal_as<String^>(mystrTEST);
		result = textBox1->Text;
		std::string stdString = marshal_as<std::string>(result);
		ofstream myfile;
		myfile.open("example.txt");
		myfile << stdString;
		myfile.close();

		
	}
	};
}
		
	}

Was This Post Helpful? 0
  • +
  • -

#5 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 239
  • Joined: 29-October 16

Re: What's the Easiest Way to Write a String to File?

Posted 20 January 2017 - 04:49 PM

So I figured out what the problem was...

fstream commands can only use std::string (s) while only a managed String^ can be directly derived from a textbox.

Solution, for me, was to first take the textBox1 input as a managed string. Then, copy the managed string over to a standard string and write to the disk from STD... probably more work than I need but it's the best solution I've found and works perfectly. When I change the text in textBox1 it automatically writes to disk!

PS: I know there's a bunch of code in there that is doing nothing (that was from me experimenting to get this to work) .. the most important parts are what comes at the very end and the inclusion of the marshal string header file.

**see above code**

~~~~~~~~
~~~~~~~~~

**response**

View PostThomas1965, on 20 January 2017 - 03:15 PM, said:

When using C++/CLI it's best to forget normal C++.
try
{
  System::IO::File::WriteAllText("example.txt", textBox1->Text);
}
catch (Exception^ ex)
{
 MessageBox::Show(ex->Message);
}


Interesting.. I'll try that out too! I just got this working so going to toy with it for a bit but your solution looks far simpler if I can get it to work. Thanks man.

This post has been edited by hexagod: 20 January 2017 - 04:58 PM

Was This Post Helpful? 0
  • +
  • -

#6 Sam Hobbs  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 50
  • Joined: 01-April 17

Re: What's the Easiest Way to Write a String to File?

Posted 06 April 2017 - 12:27 PM

In a C++/CLI program we have a choice of either C functions for I/O or C++ Standard classes or .Net classes. fstream is a C function, std::string is a C++ Standard class and String^ is a .Net class.
Was This Post Helpful? 1
  • +
  • -

#7 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 239
  • Joined: 29-October 16

Re: What's the Easiest Way to Write a String to File?

Posted 18 May 2017 - 10:10 AM

View PostThomas1965, on 20 January 2017 - 03:15 PM, said:

When using C++/CLI it's best to forget normal C++.
try
{
  System::IO::File::WriteAllText("example.txt", textBox1->Text);
}
catch (Exception^ ex)
{
 MessageBox::Show(ex->Message);
}


Thomas thanks for the help! I'll toy around with this command when I'm back into writing files. Been messing around more with powershell lately so haven't needed this too much but now that I know more about programming than when the OP was made I'll revisit this! Appreciate your advice :)
Was This Post Helpful? 0
  • +
  • -

#8 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 239
  • Joined: 29-October 16

Re: What's the Easiest Way to Write a String to File?

Posted 26 June 2017 - 01:56 PM

When I try to use this command I'm getting an error saying that the text file doesn't exist. This doesn't happen when I use the marshal string and fstream commands.

An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll

Additional information: Could not find a part of the path 'C:\Helpdisk\Old Versions\New folder\Utility (v2 Backup) - Copy (2)\Utility\Data\textbox1.txt'.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5779
  • View blog
  • Posts: 19,657
  • Joined: 05-May 12

Re: What's the Easiest Way to Write a String to File?

Posted 26 June 2017 - 07:22 PM

Show us your code.
Was This Post Helpful? 1
  • +
  • -

#10 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 239
  • Joined: 29-October 16

Re: What's the Easiest Way to Write a String to File?

Posted 28 June 2017 - 08:37 AM

private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e)
{

System::IO::File::WriteAllText("example.txt", textBox1->Text);
}

ok nvm, it's working now. Looks like I only get the error for the missing file when I use a directory in the filepath "example.txt" IE "XXXXX\\example.txt"
Was This Post Helpful? 0
  • +
  • -

#11 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 239
  • Joined: 29-October 16

Re: What's the Easiest Way to Write a String to File?

Posted 28 June 2017 - 09:06 AM

View PostThomas1965, on 20 January 2017 - 03:15 PM, said:

When using C++/CLI it's best to forget normal C++.
try
{
  System::IO::File::WriteAllText("example.txt", textBox1->Text);
}
catch (Exception^ ex)
{
 MessageBox::Show(ex->Message);
}


Thanks Thomas, that indeed is the easiest way lmao. Forget marshall strings :gunsmilie:
Was This Post Helpful? 0
  • +
  • -

#12 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 239
  • Joined: 29-October 16

Re: What's the Easiest Way to Write a String to File?

Posted 28 June 2017 - 09:13 AM

Yo another thing real quick, does anyone know how to write a multiline textbox into a .txt or other file in plaintext? It seems like when I'm using multiline textboxes the second lines get cut off :detective:/>
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5779
  • View blog
  • Posts: 19,657
  • Joined: 05-May 12

Re: What's the Easiest Way to Write a String to File?

Posted 28 June 2017 - 04:38 PM

View Posthexagod, on 28 June 2017 - 11:37 AM, said:

private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e)
{

System::IO::File::WriteAllText("example.txt", textBox1->Text);
}

ok nvm, it's working now. Looks like I only get the error for the missing file when I use a directory in the filepath "example.txt" IE "XXXXX\\example.txt"


If the path XXXXX is not rooted, then you are at the mercy of XXXXX existing as a path relative to your current directory. Even if it were rooted, you have to ensure that all the directories exist from the root to the directory containing "example.txt". That method you are calling will not create intermediate directories like most of the PowerShell commands tend to do.

View Posthexagod, on 28 June 2017 - 12:13 PM, said:

Yo another thing real quick, does anyone know how to write a multiline textbox into a .txt or other file in plaintext? It seems like when I'm using multiline textboxes the second lines get cut off :detective:/>/>

What method are you currently using? WriteAllText() should do right thing.
Was This Post Helpful? 1
  • +
  • -

#14 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 239
  • Joined: 29-October 16

Re: What's the Easiest Way to Write a String to File?

Posted 29 June 2017 - 08:51 AM

Ok cool, thanks SkyDiver. I was using marshal strings to convert managed Strings^ to std::strings. I haven't tested it out yet with WriteAllText()

Thomas1965, that's a cool little trick with the try, catch and writealltext. Thanks for the tip. there's still so much to learn, but I used your code to prevent exceptions when starting the program for the first time
private: System::Void MyForm_Load(System::Object^  sender, System::EventArgs^  e) 
	{
		//Splash Screen
		Info ^ form = gcnew Info;
		form->Show();
		try
		{
			textBox1->Text = System::IO::File::ReadAllText("t1.txt");
			textBox2->Text = System::IO::File::ReadAllText("t2.txt");
			textBox3->Text = System::IO::File::ReadAllText("t3.txt");
			textBox4->Text = System::IO::File::ReadAllText("t4.txt");
			textBox5->Text = System::IO::File::ReadAllText("t5.txt");
			textBox6->Text = System::IO::File::ReadAllText("t6.txt");
			textBox7->Text = System::IO::File::ReadAllText("t7.txt");
			textBox8->Text = System::IO::File::ReadAllText("t8.txt");
			textBox9->Text = System::IO::File::ReadAllText("t9.txt");
			textBox10->Text = System::IO::File::ReadAllText("t10.txt");
			textBox11->Text = System::IO::File::ReadAllText("t11.txt");
			textBox12->Text = System::IO::File::ReadAllText("t12.txt");
			textBox13->Text = System::IO::File::ReadAllText("t13.txt");
			textBox14->Text = System::IO::File::ReadAllText("t14.txt");
			textBox15->Text = System::IO::File::ReadAllText("t15.txt");
			textBox16->Text = System::IO::File::ReadAllText("t16.txt");
			textBox17->Text = System::IO::File::ReadAllText("t17.txt");
			textBox18->Text = System::IO::File::ReadAllText("t18.txt");
			textBox19->Text = System::IO::File::ReadAllText("t19.txt");
			textBox20->Text = System::IO::File::ReadAllText("t20.txt");
			textBox21->Text = System::IO::File::ReadAllText("t21.txt");
		}
		catch (Exception^ ex) 
		{
			//MessageBox::Show(ex->Message);
			System::IO::File::WriteAllText("t1.txt", textBox1->Text);
			System::IO::File::WriteAllText("t2.txt", textBox2->Text);
			System::IO::File::WriteAllText("t3.txt", textBox3->Text);
			System::IO::File::WriteAllText("t4.txt", textBox4->Text);
			System::IO::File::WriteAllText("t5.txt", textBox5->Text);
			System::IO::File::WriteAllText("t6.txt", textBox6->Text);
			System::IO::File::WriteAllText("t7.txt", textBox7->Text);
			System::IO::File::WriteAllText("t8.txt", textBox8->Text);
			System::IO::File::WriteAllText("t9.txt", textBox9->Text);
			System::IO::File::WriteAllText("t10.txt", textBox10->Text);
			System::IO::File::WriteAllText("t11.txt", textBox11->Text);
			System::IO::File::WriteAllText("t12.txt", textBox12->Text);
			System::IO::File::WriteAllText("t13.txt", textBox13->Text);
			System::IO::File::WriteAllText("t14.txt", textBox14->Text);
			System::IO::File::WriteAllText("t15.txt", textBox15->Text);
			System::IO::File::WriteAllText("t16.txt", textBox16->Text);
			System::IO::File::WriteAllText("t17.txt", textBox17->Text);
			System::IO::File::WriteAllText("t18.txt", textBox18->Text);
			System::IO::File::WriteAllText("t19.txt", textBox19->Text);
			System::IO::File::WriteAllText("t20.txt", textBox20->Text);
			System::IO::File::WriteAllText("t21.txt", textBox21->Text);
		}



appreciate the help fellas :beta1:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1