How can I pass a file to a function?

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");
}
 
D

David Lowndes

void Time::WriteToFile(String* path)

Change your function parameter to String ^ rather than String *

Dave
 
B

Ben Voigt [C++ MVP]

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;")
 
B

Ben Voigt [C++ MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top