Click to See Complete Forum and Search --> : FileStream
kenobe
November 8th, 2005, 04:14 AM
Hi
I have created a VC++.net MFC dialogbased application.
I need to save some data to a binary file.
my code is as follow
using namespace System;
using namespace System.IO;
using namespace System.Text;
using namespace System.IO.Stream;
:
:
:
void CfileReadWriteDlg::OnBnClickedSave()
{ CString infoFile ;
infoFile = "studentInfo.dat";
// TODO: Add your control notification handler code here
FileStream *fs = new FileStream(infoFile,FileMode::Create);
StreamWriter *swfs = new StreamWriter(fs);
swfs->WriteLine("This is a test");
swfs->Flush();
swfs->close();
Close();
}
when I run build solution , I got the following errors
error C2065: 'FileStream' : undeclared identifier
error C2065: 'fs' : undeclared identifier
error C2061: syntax error : identifier 'FileStream'
error C2653: 'FileMode' : is not a class or namespace name
error C2065: 'StreamWriter' : undeclared identifier
error C2065: 'swfs' : undeclared identifier
::
::
Why am I getting this errors, did I miss something? or
FileStream is only for C# and cannot be used in VC++.net?
Thanks
Ejaz
November 8th, 2005, 05:15 AM
[ Redirected Thread ]
cilu
November 8th, 2005, 05:18 AM
Well, I guess you mean:
using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::IO::Stream;
kenobe
November 8th, 2005, 05:30 AM
I did tried
using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::IO::Stream;
but still same errors!
darwen
November 8th, 2005, 05:41 AM
I think the key here is in the line of the question :
VC++.net MFC dialogbased
You're mixing managed and unmanaged code ! MFC-based applications are native, not managed so you don't have access to the .NET functionality unless you tell is so.
I'd create a WinForms application if I were you (managed windows application or whatever).
Or create an MFC application and stick to native C++ code (i.e. use the MFC file classes like CFile, CArchive etc).
Don't try to mix the two unless your absolutely certain about what you're doing.
Darwen.
cilu
November 8th, 2005, 06:22 AM
I think the key here is in the line of the question :
VC++.net MFC dialogbased
Ah. Good point. Somehow I missed that... ;)
kenobe
November 8th, 2005, 06:23 AM
Hi darwen
I think you are right, but you said
so you don't have access to the .NET functionality unless you tell is so.
So, how could I do it? how do I tell the IDE that I need to acess to the .NET functionality?
Thanks
cilu
November 8th, 2005, 06:27 AM
You must use the /clr command option.
To set this compiler option:
Open the project's Property Pages dialog box.
Click the Configuration Properties folder.
Click the General property page.
Modify the Use Managed Extensions property.
NoHero
November 8th, 2005, 06:42 AM
You must use the /clr command option.
To set this compiler option:
Open the project's Property Pages dialog box.
Click the Configuration Properties folder.
Click the General property page.
Modify the Use Managed Extensions property.
Or use the #pragma directive:
// Unmanaged code
#pragma managed
// Managed code
#pragma unmanaged
darwen
November 8th, 2005, 12:10 PM
To say what I said before I don't think you should be mixing managed and native code in this way : you should stick to one or the other.
There's no need to write a dialog based MFC app using .NET underneath when you can just write a winforms application instead.
You only mix the two when necessary : if it's unnecessary you'll soon start asking questions like "how do I convert a LPCSTR to a String ^" etc etc when there's no need.
Please just stick to one or the other. Or be prepared for headaches aplenty.
Darwen.
kenobe
November 8th, 2005, 09:28 PM
Hi Darwen
I understand your point and agree to it fully.
But this is the assignment given to me to write a student attendance system using .NET.
I choose dialogbased application so that I can capture the user input easily.
Come back to the problem, I apply the change to YES as suggested by
you
To set this compiler option:
Open the project's Property Pages dialog box.
Click the Configuration Properties folder.
Click the General property page.
Modify the Use Managed Extensions property.
Yet I still have the same error
I hope I am not too naive to understand your advise
darwen
November 8th, 2005, 11:58 PM
You're still not getting it are you ? Write a WinForms app : this'll do all the UI in managed code, not native. There will be a wizard to do this in VS2003. There's not in VS2002, but there is in VS2003.
Personally if I'd stated
But this is the assignment given to me to write a student attendance system using .NET.
in a question I wrote and someone handed in an MFC application I'd mark them down because they wouldn't have produced what I'd asked for. If this happened in my company I would get you to write it again : period.
Darwen.
kenobe
November 9th, 2005, 12:44 AM
Hi Darwen
I appreciate and understand your advise.
But if I switch to WinForms app, it means that I have to rewrite the project all over again starting from zero.
But if this is the only choice, then I need to do it and I have to pay the price of not selecting the correct .net application type initially.
Thanks
darwen
November 9th, 2005, 10:11 AM
Hopefully you've designed your application nicely and all the functionality will be in portable classes which you can transfer over.
If not, I'd strongly recommend you do it again. Mixing MFC and .NET like this isn't advisable : in a commercial environment it would make the MFC application dependent on the .NET framework being installed on the target machine. One of the reasons for writing native code is to remove this dependency.
Chalk it up to experience and get on with it : you'll find that you'll end up rewriting a lot of code when learning a new language - it's just part and parcel of learning how not to do things.
Darwen.
kenobe
November 9th, 2005, 10:07 PM
Hi
Darwen
I will take your advise and note it in my diary .
Thank you very much, appreciated.
Thanks
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.