0

here is my code i can't seem to write data on my file.

StreamWriter sw = gcnew StreamWriter(Application::StartupPath + "\\Employee\\" +""+NameE->Text + "" +NumberE->Text + "" +EmailE->Text + ".txt" , true);//Path to write .txt file

    sw.WriteLine(label1->Text + " " + NameE->Text);
    sw.WriteLine(label2->Text + " " + NumberE->Text);
    sw.WriteLine(label3->Text + " " + EmailE->Text);
    sw.Close();

I keep Getting this error

Severity Code Description Project File Line Suppression State Error C2664
'System::IO::StreamWriter::StreamWriter(const System::IO::StreamWriter %)': cannot convert argument 1 from 'System::IO::StreamWriter ^' to 'System::IO::Stream ^'t

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
  • By reading the compiler errors, you can see that the compiler is not able to parse the 1st argument supplied to StreamWriter. You can try to split the concatenation and try to print the strings one after another to debug the issue. – Vignesh Vedavyas Dec 20 '19 at 12:24
  • You're also missing the `^` on `sw`, since you're using `gcnew` – ChrisMM Dec 20 '19 at 12:29
  • 1
    The compiler is struggling to find a constructor overload for this invalid code. Google "c++/cli stack semantics" to find the correct way to do this, very important to learn how to get ahead in C++/CLI. https://stackoverflow.com/a/11087648/17034 – Hans Passant Dec 20 '19 at 12:33

1 Answers1

1
StreamWriter sw = gcnew StreamWriter(...);

gcnew returns a managed reference. This is analogous to C++, where new returns a pointer. In C++, you specify a pointer by using a *: MyType* foo = new MyType(...). In C++/CLI, you specify a managed reference by using a ^.

StreamWriter^ sw = gcnew StreamWriter(...);
//          ^-- Right here.
David Yaw
  • 27,383
  • 4
  • 60
  • 93