Write to File .NET

May 4, 2011 at 3:50am
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 May 4, 2011 at 4:00am
May 4, 2011 at 4:35am
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
May 4, 2011 at 1:02pm
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 May 4, 2011 at 1:04pm
May 4, 2011 at 2:02pm
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 May 4, 2011 at 2:12pm
May 4, 2011 at 2:03pm
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 May 4, 2011 at 2:04pm
May 4, 2011 at 4:50pm
@ 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.
May 4, 2011 at 6:35pm
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 May 4, 2011 at 6:35pm
May 4, 2011 at 6:43pm
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".
May 5, 2011 at 4:36am
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?
May 5, 2011 at 7:00am
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.
May 5, 2011 at 9:48pm
I tried some of those, but they won't work.
May 8, 2011 at 11:33pm
When you use the normal C++, it doesn't let you see the designer. Which is the hardest part about making the program.
May 9, 2011 at 7:03am
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.
May 9, 2011 at 7:02pm
The designer is available only on commercial editions of Visual Studio.
May 10, 2011 at 10:05pm
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.