| .NET DotNet Forum Index » Visual C++ Forum » How can I pass a file to a function?... |
|
Page 1 of 1 |
|
| Author |
Message |
| Allen... |
Posted: Sun Jul 26, 2009 4:46 pm |
|
|
|
Guest
|
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");
}
--
Thanks
Allen |
|
|
| Back to top |
|
|
|
| David Lowndes... |
Posted: Mon Jul 27, 2009 2:21 am |
|
|
|
Guest
|
Quote: void Time::WriteToFile(String* path)
Change your function parameter to String ^ rather than String *
Dave |
|
|
| Back to top |
|
|
|
| Ben Voigt [C++ MVP]... |
Posted: Mon Jul 27, 2009 11:32 am |
|
|
|
Guest
|
"Allen" <allen_maki at (no spam) yahoo.com> wrote in message
news:62C04FDA-AC28-4E93-8AE2-1AE1A5023E01 at (no spam) microsoft.com...
Quote: 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;")
Quote:
"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");
}
--
Thanks
Allen |
|
|
| Back to top |
|
|
|
| Rob... |
Posted: Mon Aug 03, 2009 9:20 am |
|
|
|
Guest
|
shouldn't you delete sw at the end of the func ?
Rob.
"Allen" wrote:
Quote: 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");
}
--
Thanks
Allen
|
|
|
| Back to top |
|
|
|
| Ben Voigt [C++ MVP]... |
Posted: Mon Aug 03, 2009 2:36 pm |
|
|
|
Guest
|
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.
"Rob" <__rob_AT_robtso_DOT_nl__> wrote in message
news:2E4405E1-DB99-44A4-80EF-FCCA9C656F13 at (no spam) microsoft.com...
Quote: shouldn't you delete sw at the end of the func ?
Rob.
"Allen" wrote:
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");
}
--
Thanks
Allen
|
|
|
| Back to top |
|
|
|
|