CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2009
    Posts
    9

    File open in VC++ CLR Console application

    I got a problem with my project with FILE open in my project so I have created a sample project in Visual studio 2005 which is a Visual C++ CLR Console Application and write this pretty simple code and test the file handilng but it's not working and give me couple of compilation error.

    #include "stdafx.h"

    using namespace System;

    int main(array<System::String ^> ^args)
    {
    Console::WriteLine(L"Hello World");
    FILE* test = fopen("C:\\test.txt","rt");
    return 0;
    }

    Errors

    Error 1 error C2065: 'FILE' : undeclared identifier
    Error 2 error C2065: 'test' : undeclared identifier
    Error 3 error C3861: 'fopen': identifier not found

    Can anybody help me to figure this out and it will probably help me to figure out my project issue too.

    Lahiru

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: File open in VC++ CLR Console application

    The project you have created is a .NET project, but you're using native C++ (fopen etc).

    You should create a Win32 console application and have

    Code:
    #include <windows.h>
    at the top of the cpp file, below the #include "stdafx.h".

    As an aside using fopen is strongly discouraged these days because of the alternatives which cannot leak.

    You should look into using the std classes for file access i.e. ifstream instead.

    If you really do want to use .NET I'd suggest switching to C# - then you can't mix up native and managed code.

    Have a look in the System::IO::File class. It has static methods to read and write files, e.g. System::IO::File::ReadAllText.

    Darwen
    Last edited by darwen; January 16th, 2009 at 08:36 AM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  3. #3
    Join Date
    Jan 2009
    Posts
    9

    Re: File open in VC++ CLR Console application

    No I switched to use a clr due to an issue I got with memory allocation with the outside code I'm using with my project and I'm restrict to use clr right now. In my project I'm got number of errors in one of the class cpp and .h files. When I include stdio.h at the top all the errors comes with FILE get removed but with the header file of that class still gives me the error with FILE*.

    Error 2 error C2061: syntax error : identifier 'FILE'

    This is completely stupid..Do you have any idea about this..please?

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: File open in VC++ CLR Console application

    In other words, instead of actually fixing a problem, lets try a different approach that is also not properly understood...

    Given sufficient time and enough random attempts.......(Hey where did my mokies go?? )
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Jan 2009
    Posts
    9

    Re: File open in VC++ CLR Console application

    Yeah.. I figured it out. I was a mistake since I just added number of files from my unmanaged source code using add existing items. I have added a new class and looked at the clr classes and I figured out a text was missing and I add that and add the stdio.h file and compiled. Then bunch of errors and that was about my library issue and right now no error...

    Thanks all for the help !

    Lahiru

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured