Click here to Skip to main content
15,885,715 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Please anyone who is familiar with visual C++, please I need your help.
I have this problem in my code that i've been trying to fix without any success.

I have this program that i'm building using Windows Forms. It is about a hospital management system, but I have a problem writing to files. Take an example the following code:
using namespace System;
using namespace System::IO;

int main()
{
//creating the file object data.txt
FileStream^ fs = gcnew FileStream("data.txt", FileMode::Create);
StreamWriter^ sw = gcnew StreamWriter(fileName);

try{

//writing lines from a string and a textbox input
sw->WriteLine("First Name: ", this->FnameTextBox->Text);
}
//Exception handler
catch (Exception^)
{
Console::WriteLine("data could not be written");
fs->Close();
return -1;
}

fs->Close();
return 0;
}


The problem that i'm having is to write both the string and the input from the user (FnameTextBox) to the same line in the text file. This will appear on the text file like this:
First Name: Marlaw19 //if the input was Marlaw19

The code only writes "First Name:" and leaves out the input from the user through the control "FnameTextBox".
I've already declared the text box control and the text box works correctly on it self on its own line eg. sw->WriteLine(this->FnameTextBox->Text);

I've tried every tricks but it is still not working!

Any one who is familiar with Visual C++, Please I need your help.
Posted
Updated 29-Apr-11 1:10am
v3

use
sw->WriteLine("First Name: {0}", this->FnameTextBox->Text);
 
Share this answer
 
Comments
Olivier Levrey 29-Apr-11 6:02am    
Good, my 5. Another possibility is concatening the strings.
Ryan Zahra 29-Apr-11 6:09am    
Perfect answer! My 5
macdonaldgeek 29-Apr-11 6:55am    
I'll try that and see what happens. I've never thought of putting {0} or + in the code before. Thanx guyz.
Member 7796364 29-Apr-11 10:48am    
You can use this to write many things at once like the following code
sw->WriteLine("Dt {0} UpDown {1} LeftRight {2} ChA {3} ChB {4} ChC {5} ChD {6}",SD1,SD2,SD3,SD4,SD5,SD6,SD7);

Took me awhile to figure this out, this answer helped a bunch

thanks
I'm not into Visual C++, but all I could tell you is to have a single string which has the value of "First Name". Then append to the string, the value found in the text box. Then write the string in the stream writer.
 
Share this answer
 
Comments
Olivier Levrey 29-Apr-11 6:03am    
Sure. My 5.
Ryan Zahra 29-Apr-11 6:09am    
Thanks :)
You can use Prerak Patel's answer or this one (it is actualy Ryan's answer):
sw->WriteLine("First Name: " + this->FnameTextBox->Text);
 
Share this answer
 
v2
Comments
Prerak Patel 29-Apr-11 6:05am    
Have 5. I put it as {0} because OP's code seems like he was trying that way.
Olivier Levrey 29-Apr-11 6:12am    
Thank you Prerak. Yes your solution is closer to Op's original code.
Ryan Zahra 29-Apr-11 6:08am    
My 5! Good as well!
Olivier Levrey 29-Apr-11 6:12am    
Thank you Ryan.
macdonaldgeek 2-May-11 7:51am    
It worked! thanx everyone for your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900