Write to File .NET

I'm making a program for our church. I need it to save what the user types in and one of the names of the text box is
txtbName
. I need it to write to a text file of what the user types in, but how do I do that in .NET < still C++>. Here's a link for it.


https://rapidshare.com/files/459825879/GraceAG.exe
Last edited on
You want to look into using System.IO

http://msdn.microsoft.com/en-us/library/336wast5(v=vs.71).aspx

Copy and paste your code here with code-blocks with specific questions on what is not working
I'm using C++ cause that's the only language I know, and I'm still learning. I'm only 14.
This is an example that I found on Microsoft.





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#pragma endregion
	private: System::Void btnPraySubmit_Click(System::Object^  sender, System::EventArgs^  e) {




   String^ fileName = "example.txt";

   StreamWriter^ sw = gcnew StreamWriter(filename);
   sw->WriteLine("A text file is born!");
   sw->Write("You can use WriteLine");
   sw->WriteLine("...or just Write");
   sw->WriteLine("and do {0} output too.", "formatted");
   sw->WriteLine("You can also send non-text objects:");
   sw->WriteLine(DateTime::Now);
   sw->Close();
   Console::WriteLine("a new file ('{0}') has been written", fileName);

   return 0;

				 PrayRequest::Hide();
			 }



That's what I have in my program, just to test it out, but it doesn't produce a text file.

I do have using namespace System::IO;
Last edited on
That language IS NOT C++.

There is no problem if you want to use Microsoft technologies like .NET, but that is completely different programming language.




For your convenience, this is a C++ example for writing to a file:

1
2
3
4
5
6
7
8
9
#include <fstream>

int main(int argc, char* argv[])
{
	std::ofstream of("test.txt");
	of << "This is a test content.";
	of.close();
	return 0;
}


This example will create the file "test.txt" and put "This is a test content." inside it.
Last edited on
14 is old enough - I started programming around the 4th grade, as did many of my friends.

If the code compiles and runs, but produces no file, I would try changing the fileName to an absolute filepath, something like "C:\TEMP\example.txt" (you may need \\ or double slashes here, twice - I don't know how C's backslash convention interferes with Window's \ convention for directories).

See if that works.

If you are getting compile or link errors, post the details.
Last edited on
@ kfmfe04 & OP: Both C\C++ and Windows use UNC so a single backslash '\' is an escape character by default; any literal strings need to be double backslashed. Other then that I would use the 'fstream' header in place of 'System.IO' but that's more of a style preference.
Thanks, by the way. I'm at school now and I can't work on it, but I will as soon as I get home, and I'll tell you if it works. I know about escape characters. If I'm using Visual C++, it uses .NET, but how is it different? I've used C++ coding in there.
Last edited on
Visual Studio doesn't force you to use .NET. You can write (normal!) C++ programs with it too.

C++/CLI is a language designed to use .NET while still looking a bit like C++. However, it is a different language. It has new concepts such as "handles".
I know how to do ofstream and all, but you showed me a simpler looking way to do it so thanks, but it didn't work in my program (probably cause you said it's a different language). I've put too much work into it, to start over. Can you show me a code (if you know how) in C++/CLI for a text writer?
I have written such programs in C#, but not C++/CLI. However, I reiterate the link provided above:
http://msdn.microsoft.com/en-us/library/336wast5(v=vs.71).aspx

Also, two classes for reading from/writing to files:
http://msdn.microsoft.com/en-us/library/system.io.streamreader(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/system.io.streamwriter(v=vs.71).aspx

Hope this helps.
I tried some of those, but they won't work.
When you use the normal C++, it doesn't let you see the designer. Which is the hardest part about making the program.
Are you saying that you are using .NET in C++? Or that you are trying to make the program without .NET? I am not sure there is a designer for not .NET programming.
The designer is available only on commercial editions of Visual Studio.
I'd rather make it so it doesn't have .NET, but there's no designer for it (unless you pay a lot). But the Windows Forms Applications uses C++\CLI. Which by what I understand is a .NET language which is made to look like C++.
Topic archived. No new replies allowed.