I can't name files cause of a (.)

Pages: 12
First, I don't entirly understand the code. below. It was given to me to help with a problem I was having.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
             {
                 System::String^   str = textBox1->Text;
                 System::IntPtr p2 = Marshal::StringToHGlobalAnsi(str);
                char* str2 = (char*)p2.ToPointer();
                
                //Do what you want with the char*
                ofstream myfile("example.txt" ); //open this file for output
                 myfile << str2 << endl;
                 myfile.close();

                //Now tidy up
                Marshal::FreeHGlobal(p2);

             }



The thing it helped accomplish was: creating a batch-file making program.

The issue I'm having is that if I change the code around, I can get the code to name the file. However, after a (.) it won't continue reading the textBox:

EXAMPLE:
textBox2->Text = "newguy.bat"

it only reads "newguy"

How do I fix this?
Last edited on
That's what I call C++ .NET. Instead of using c++'s STL use the .NET managed classes. For file input/output there should be a StreamReader and StreamWriter that you can construct passing a reference to a FileStream object. You may have better luck going that route.

They are located in System::IO

Those classes have Write() and WriteLine() functions that take just about any data type as a parameter (int, double, String^, etc).
Last edited on
Any chance you have an example, or a really good link? Google almost always takes to me pages that make no sense to me at all.

Sorry for the inconvenience.
Oh and Thank you :)
thenewguy wrote:
The issue I'm having is that if I change the code around, I can get the code to name the file. However, after a (.) it won't continue reading the textBox:


What code do you have now??
Because if I change the code to something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
             {
                 //This shows how to convert a System::String to a C++ char*
                 System::String^   str = textBox1->Text;
                 System::IntPtr p2 = Marshal::StringToHGlobalAnsi(str);
                char* str2 = (char*)p2.ToPointer();
                
                //Do what you want with the char*
                ofstream myfile(str2 ); //use name from the text box as the name of the output file
                 myfile << "put this text in the file" << endl;
                 myfile.close();

                //Now tidy up
                Marshal::FreeHGlobal(p2);

             }


If I put thenewguy.bat in the edit box, then I get a file called the thenewguy.bat created (if it does not exist) and
the data is put in the file.
There is no stopping at the dot as you say.


So my questions are:
1. Do you get a file created??
2. If you look in explorer at the directory the file is in - switch to details view - does it show
the file as a 'batch file' and also shows the correct icon for the file.
because if it does - but the filename does not show the extension, then Explorer is
in 'HIDE EXTENSIONS FOR KNOWN FILE TYPES' mode.


In the meantime post your code - as you said you had changed the code around.
1.) yes a file is created, but just called newguy

2.) I leave my file extensions showing. It's just a preference. Any other way?
Show us your code - let's check it out.
pic of what I'm seeing

http://i869.photobucket.com/albums/ab255/michaeloohra/untitled-8.jpg

/////////////////

CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

#pragma once
#include <Windows.h>
#include <iostream>
#include <fstream>
using namespace std;
using namespace System::Runtime::InteropServices;


namespace batchwrong {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			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::Button^  Close;
	protected: 
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::TextBox^  textBox2;
	private: System::Windows::Forms::Button^  button1;

	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)
		{
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
			this->Close = (gcnew System::Windows::Forms::Button());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// Close
			// 
			this->Close->Location = System::Drawing::Point(321, 243);
			this->Close->Name = L"Close";
			this->Close->Size = System::Drawing::Size(75, 23);
			this->Close->TabIndex = 0;
			this->Close->Text = L"Close";
			this->Close->UseVisualStyleBackColor = true;
			this->Close->Click += gcnew System::EventHandler(this, &Form1::Close_Click);
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(13, 13);
			this->textBox1->Multiline = true;
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(383, 224);
			this->textBox1->TabIndex = 1;
			// 
			// textBox2
			// 
			this->textBox2->Location = System::Drawing::Point(13, 244);
			this->textBox2->Name = L"textBox2";
			this->textBox2->Size = System::Drawing::Size(100, 20);
			this->textBox2->TabIndex = 2;
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(119, 244);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(131, 23);
			this->button1->TabIndex = 3;
			this->button1->Text = L"Create File";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(408, 276);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->textBox2);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->Close);
			this->Icon = (cli::safe_cast<System::Drawing::Icon^  >(resources->GetObject(L"$this.Icon")));
			this->Name = L"Form1";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = L"New Guy Batch File Maker";
			this->TopMost = true;
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void Close_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
					//This is to close form
			 }
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
			
				 
				 System::String^   str = textBox1->Text;
                 System::IntPtr p2 = Marshal::StringToHGlobalAnsi(str);
                char* str2 = (char*)p2.ToPointer();
                
               
				
				 System::String^   str4 = textBox2->Text;
                 System::IntPtr p4 = Marshal::StringToHGlobalAnsi(str);
                char* str3 = (char*)p4.ToPointer();


                ofstream myfile(str3); 
                 myfile << str2 << endl;
                 myfile.close();

           
                Marshal::FreeHGlobal(p2);

               
              


			 }
};
}


I don't know C++/CLI, but taking a guess from looking at the picture and how you name your variables, is it possible that you're accidentally reading from that canvas (is it a canvas?) where the word "pause" is written?
Not 100% sure what you are asking Filipe (like the name btw),

But if by "canvas" you mean "textBox1"... No, it's a textBox with multi lines enabled.

But the second box is where the file is names. and it's a textBox as well

textBox2->Text to be exact..

Sorry if that isn't your question
Last edited on
Ok, walk with me. On lines 132 and 133 you say this:

1
2
System::String^ str = textBox1->Text;
System::IntPtr p2 = Marshal::StringToHGlobalAnsi(str);

On lines 139 and 140:

1
2
System::IntPtr p4 = Marshal::StringToHGlobalAnsi(str);
char* str3 = (char*)p4.ToPointer();

And finally, on line 143:

ofstream myfile(str3);

And as we saw, str3 is created by passing str into a function, and str is created by getting the text in textBox1, which is the big white one where it says "pause".
Last edited on
yes, I meant to go in numerical order, but messed up when I first created that solution.

ofstream myfile(str3);
tells the file to be named what is in the smaller textBox (textBox2->Text)

The bigger textBox, is TextBox1

Pic below:
http://i869.photobucket.com/albums/ab255/michaeloohra/untitled-9.jpg
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
System::String^   str = textBox1->Text; //set textBox1 to the instructions of the batch file
                 System::IntPtr p2 = Marshal::StringToHGlobalAnsi(str);
                char* str2 = (char*)p2.ToPointer();
                
               
				
				 System::String^   str4 = textBox2->Text; //set file name to textBox2->Text
                 System::IntPtr p4 = Marshal::StringToHGlobalAnsi(str);
                char* str3 = (char*)p4.ToPointer();


                ofstream myfile(str3);  // <====textBox2->Text inserted here
                 myfile << str2 << endl; // <==== textBox1->Text inserted here
                 myfile.close();

           
                Marshal::FreeHGlobal(p2);
Last edited on
Read my post carefully. I marked every point of interest. str3's data comes from str, whose data comes from TextBox1, which is the big one where it says "pause". To fix it, change line 139 from this:

System::IntPtr p4 = Marshal::StringToHGlobalAnsi(str);

to this:

System::IntPtr p4 = Marshal::StringToHGlobalAnsi(str4);

Now all of this happened because you didn't choose significant names for your variables. If the file name was named filename instead of str4 it would be pretty hard to make that mistake.
lol I got the code from someone, cause I didn't know how to turn a textBox string into a string that could be used with the ofstream files




The last question I have though is this:
How do code "When I/user press 'ENTER' it does the button click event?


Anyway the change you pointed out saved me. but to clarify I can name str4 whatever I want?

lastly I am most grateful. Thank you! all of you!
Last edited on
Glad to help. I don't know anything about C++/CLI, though. I assume you have to intercept some sort of key press event and process it. There's probably something on MSDN.
lol I got the code from someone, cause I didn't know how to turn a textBox string into a string that could be used with the ofstream files



It was me that filled in the code in the function in the original post...
Last edited on
filipe thanks again, I wish I had the ability to look at a C-L i don't know and figure out where the issues are lol. I'm impressed.


Guestgulkan, Thank you for help on both threads.

Do you know about keyboard button press events?
It's a property of the button that you set. I can't remember what it is right off the top of my head. I check and get back to you.
I'm wrong. It's a property of the form called 'AcceptButton'. You set the property to the name of the button you want to be the button that gets 'clicked' when you press enter.
Pages: 12