How can I pass a file to a function?

  • Thread starter Thread starter Allen
  • Start date Start date
A

Allen

How can I pass a file to a function? If tried the code below (using
string* or String*) I got:

"error C2143: syntax error : missing ')' before '*' "

//The function in "Time.ccp"

void Time::WriteToFile(String* path)
{
FileStream* fs = new FileStream(path, FileMode::Create);
StreamWriter* sw = new StreamWriter(fs);
String* TimeString = (System::DateTime::Now.ToString("mm"));
sw->WriteLine(TimeString);
sw->Flush();
sw->Close();
}

// "Time.h"
__gc class Time
{

public:
Time (string* path);
void WriteToFile(string* path);
void Time::ReadFromFile();
private:
string* path;

};

//Call the function from the handler
private: System::Void InBtn_Click(System::Object * sender,
System::EventArgs * e)
{
Time * Employee1;
Employee1 = new Time;
Employee1->WriteToFile(S"TimeX.txt");
}
 
void Time::WriteToFile(String* path)

Change your function parameter to String ^ rather than String *

Dave
 
Allen said:
How can I pass a file to a function? If tried the code below (using
string* or String*) I got:

What version of Visual Studio are you using? Please use at least VS2005
(Express Edition of VC2008 is available as a free download) and the new
C++/CLI syntax that David mentioned. In addition to using ^ instead of *
for handles to managed types, "__gc class" becomes "ref class".

C++ is also case-sensitive, so you can't use string in the header and then
String in the implementation. The first refers to the native std::string
class (and requires "using namespace std;" or full qualification) while the
second is the .NET System::String reference type (and requires "using
namespace System;")
 
Yes, but since it did get closed, the call to Dispose shouldn't have much
left to do.

fs on the other hand, is still open and keeps the file locked for an
indefinite period, which will probably cause contention.
 
Back
Top