HighTechTalks DotNet Forums  

How to use file I/o codes with forms and controls codes

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss How to use file I/o codes with forms and controls codes in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Allen
 
Posts: n/a

Default How to use file I/o codes with forms and controls codes - 12-02-2007 , 06:07 PM






Hi Everybody,

I would appreciate it if you can give me a hand here.

I want to use the two codes, below, together to do the following
when the "IN" button is clicked (InBtn_Click):-

1- Create (or open) output.txt, and
2- Type the current time (hour only) in it.

I tried to include the first code inside the handle function in the second
code, but it does not work. Can anybody tell me who I am going to work the
two codes together?

Each code works fine by itself. The message box inside the
handler works fine when "IN" button is clicked. The the output.txt file
open and the current time will be stamped inside it when I use it in a
different function. But if I put the first code inside the handler function
of the second code, then, when I click the "IN" button, I would not get what
I am expecting, Which is to open output.txt and stamp the current time.

First code:

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

Second Code:

private: System::Void InBtn_Click(System::Object * sender,
System::EventArgs * e)
{

// If I put the first code here, output.txt would not be created and current
time
//would not be stamped.

if ( mark == '-')
{
MessageBox::Show(" You can't sign in twice! ",S" Error ");
}
}




--
Thanks
Allen


Reply With Quote
  #2  
Old   
Barry Kelly
 
Posts: n/a

Default Re: How to use file I/o codes with forms and controls codes - 12-03-2007 , 11:59 AM






Allen wrote:

Quote:
I would appreciate it if you can give me a hand here.
The error messages you're getting would help us out.

Quote:
I tried to include the first code inside the handle function in the second
code, but it does not work. Can anybody tell me who I am going to work the
two codes together?

Each code works fine by itself. The message box inside the
handler works fine when "IN" button is clicked. The the output.txt file
open and the current time will be stamped inside it when I use it in a
different function. But if I put the first code inside the handler function
of the second code, then, when I click the "IN" button, I would not get what
I am expecting, Which is to open output.txt and stamp the current time.

First code:

FileStream* fs = new FileStream(S"output.txt", FileMode::Create);
StreamWriter* sw = new StreamWriter(fs);
String* TimeString = System:ateTime::Now.ToString("HH");
sw->WriteLine(TimeString);
sw->Flush();
sw->Close();
This is using the obsolete VS 2003 C++ syntax (/clrldSyntax).
AFAIK, for a modern project, you'd need to manually configure the
compiler to support the old syntax. Current syntax looks more like:

---8<---
using namespace System;
using namespace System::IO;

// ...

// use new C++/CLI syntax for managed references (for illustration only;
// disposable objects like FileStream should use stack-style as
// illustrated in the next line, where possible.
FileStream^ fs = gcnew FileStream("output.txt", FileMode::Create);

// Use new C++/CLI stack-style objects for automatic disposal.
StreamWriter sw(fs);
sw.WriteLine(DateTime::Now.ToString("HH")); // note '.' in stack-style.

// Because StreamWriter implements IDisposable and is allocated in stack
// style, an IDisposable.Dispose call will automatically occur when the
// stack-style object go out of scope. The flush and close are implicit
// in the disposal of a stream.
--->8---

Quote:
Second Code:

private: System::Void InBtn_Click(System::Object * sender,
System::EventArgs * e)
{

// If I put the first code here, output.txt would not be created and current
time
//would not be stamped.

if ( mark == '-')
{
MessageBox::Show(" You can't sign in twice! ",S" Error ");
}
}
I expect that in the source code file properties in the project for this
form, the syntax is configured for modern C++/CLI, rather than the
obsolete syntax, and that's the reason it's not working.

Without showing us your compiler error(s), though, it's hard to be
certain.

-- Barry

--
http://barrkel.blogspot.com/


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.