locked
How can I pass a file name to a function? RRS feed

  • Question

  • I am new to .NET.  I am using MS Visual C++ .NET 2003. 

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

     

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

     

    //The function in "My 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();

    }

     

    //    "My Time.h"

     

    __gc

    class Time

    {

    public

    :

    Time (String* path);

    void WriteToFile(string* path);

    void Time::ReadFromFile();

    private

    :

    String* path;

     

    };

     

     

    //    calling 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");

                                                   

                                       

                                        }

     


    • Edited by autostrad Sunday, July 26, 2009 9:05 PM
    Sunday, July 26, 2009 8:59 PM

Answers

  • Thanks everybody.  If anybody want to know how I did it just let me know.
    • Marked as answer by autostrad Saturday, August 1, 2009 1:19 AM
    Saturday, August 1, 2009 1:18 AM
  • May be I should get credit for it!
    • Marked as answer by autostrad Saturday, August 1, 2009 1:20 AM
    Saturday, August 1, 2009 1:18 AM

All replies

  • That's the old MC++ syntax.  You have to insert __gc in random places.  Start at the place the compiler is complaining.  Or buy the update, nobody deserves to be exposed to it.
    Hans Passant.
    Monday, July 27, 2009 12:47 AM
  • Or at least get VC++ 2008 Express which is free.
    http://blog.voidnish.com
    Monday, July 27, 2009 1:27 AM
  • I have vc++ 2005 and vc s 2008.  But my program (needs update) which I wrote 5 years  ago was written by vc++ 2003.  I cannot use anything else.  If you say use convert.  It will not work.  If you help made it work, I will reward you.

      
    Monday, July 27, 2009 2:25 AM
  • I have vc++ 2005 and vc s 2008.  But my program (needs update) which I wrote 5 years  ago was written by vc++ 2003.  I cannot use anything else.  If you say use convert.  It will not work.  If you help made it work, I will reward you.

      

    What line gives you the error?

    Also, this line :

    void WriteToFile(string* path);

    needs to be :

    void WriteToFile(String* path);
    http://blog.voidnish.com
    Monday, July 27, 2009 2:28 AM
  • I saw a book passes the information to the function something like this:


    void Time::WriteToFile(StreamWriter* sw )  

    Instead of this:

      

    void Time::WriteToFile(String* path)

     

    Tuesday, July 28, 2009 1:17 AM
  • Well what's confusing about that? When you design a class, you can create methods with whatever signatures you want to. In your case since your method takes a String* and not a StreamWriter*, that's how you'd call it. Else you'd need to rewrite the method with the other signature.
    http://blog.voidnish.com
    Tuesday, July 28, 2009 1:27 PM
  • I want to rephrase  the question.

    I am using  MS Visual C++ .NET 2003.

    The problem is:   I can use this code to write the current time (minutes only) or anything else to a file:

    This is a working code:

    fs = new FileStream(S"FileA.txt", FileMode::Create);  
    sw = new StreamWriter(fs);  
    String* TimeString = (System::DateTime::Now.ToString("mm")); 
     sw->WriteLine(TimeString); 
     sw->Flush();          
    sw->Close();

    I have to open many files named from FileA to FileB, FileC and so on.  I need a function to use instead of writing this block of code every time.

    I am wrong when I want to do the function something like this:

    This code does not work:

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

     I want help to make the function so that I could use it in my program.

    Wednesday, July 29, 2009 7:46 PM
  • This code does not work:

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

     I want help to make the function so that I could use it in my program.

    Define "doesn't work".

    David Wilkinson | Visual C++ MVP
    Wednesday, July 29, 2009 8:22 PM
  • Thanks everybody.  If anybody want to know how I did it just let me know.
    • Marked as answer by autostrad Saturday, August 1, 2009 1:19 AM
    Saturday, August 1, 2009 1:18 AM
  • May be I should get credit for it!
    • Marked as answer by autostrad Saturday, August 1, 2009 1:20 AM
    Saturday, August 1, 2009 1:18 AM