Thread: Win32 file handling

  1. #1
    george7378
    Guest

    Win32 file handling

    I was wondering whether there is a way to do file input/output with Win32/C++? For example, in a console C++ program, I can fo this:

    ofstream write;
    write.open ("file.txt");
    myfile << "Hello";
    myfile.close();

    However, when I try to use the above functions in a Win32 program, it will not compile. How do I open and write to/read from a text file in Win32?

    Thanks.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Googling "win32 api file io" gave everything you need to know right in the first link: File Management Functions (Windows)
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    All of those functions are perfectly valid in a Win32 application. What compile errors are you getting?
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    george7378
    Guest
    I have a program which is instructed to do the above when I press a button in my Win32 program:

    Code:
    case IDC_WRITE:
    				
    
    ofstream write;
    write.open ("file.txt");
    write << "Hello";
    write.close();
    					
    
    break;
    Here are the errors I get:

    Code:
    c:\documents and settings\xp\my documents\visual studio 2008\projects\setup2\setup2\main.cpp(42) : error C2065: 'ofstream' : undeclared identifier
    c:\documents and settings\xp\my documents\visual studio 2008\projects\setup2\setup2\main.cpp(42) : error C2146: syntax error : missing ';' before identifier 'write'
    c:\documents and settings\xp\my documents\visual studio 2008\projects\setup2\setup2\main.cpp(42) : error C2065: 'write' : undeclared identifier
    c:\documents and settings\xp\my documents\visual studio 2008\projects\setup2\setup2\main.cpp(43) : error C2065: 'write' : undeclared identifier
    c:\documents and settings\xp\my documents\visual studio 2008\projects\setup2\setup2\main.cpp(43) : error C2228: left of '.open' must have class/struct/union
            type is ''unknown-type''
    c:\documents and settings\xp\my documents\visual studio 2008\projects\setup2\setup2\main.cpp(45) : error C2228: left of '.close' must have class/struct/union
            type is ''unknown-type''

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Console style i/o won't work in a windows program, because there's no console window.

    You should take itsme's suggestion and read up on the API calls.

    But you can also use C style fopen(), fprintf(), fread(), fwrite(), fclose() etc. (and their C++ equivalents) as these don't pass text through the console.

  6. #6
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    #include <fstream>

    and your done

  7. #7
    george7378
    Guest
    OK, I just changed it for this:

    Code:
    FILE * pFile;
    pFile = fopen("file.txt","w");
    fwrite (buf , 1 , sizeof(buf) , pFile ); // 'buf' is a char* which takes its value from a text box that the user types into.
    fclose (pFile);
    It works fine (it creates a text file and outputs the value of 'buf' to it) but I get a warning:

    Code:
    warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
            c:\program files\microsoft visual studio 9.0\vc\include\stdio.h(237) : see declaration of 'fopen'
    Is it still OK to use the fopen() function, or is it likely to cause problems?

  8. #8
    george7378
    Guest
    Never mind - I changed it to this:

    Code:
    FILE * pFile;
    fopen_s(&pFile,"file.txt","w");
    fwrite (buf , 1 , sizeof(buf) , pFile );
    fclose (pFile);
    ...and it seems to do the same thing - is this right?

    Thanks for your help, everyone.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by CommonTater View Post
    Console style i/o won't work in a windows program, because there's no console window.

    You should take itsme's suggestion and read up on the API calls.

    But you can also use C style fopen(), fprintf(), fread(), fwrite(), fclose() etc. (and their C++ equivalents) as these don't pass text through the console.
    Since when is ofstream considered "console style i/o"?
    bit∙hub [bit-huhb] n. A source and destination for information.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by george7378 View Post
    Never mind - I changed it to this:

    Code:
    FILE * pFile;
    fopen_s(&pFile,"file.txt","w");
    fwrite (buf , 1 , sizeof(buf) , pFile );
    fclose (pFile);
    ...and it seems to do the same thing - is this right?

    Thanks for your help, everyone.
    There you go....
    fopen is depricated because of the possibility of a buffer overrun in the filename.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by CommonTater View Post
    There you go....
    fopen is depricated because of the possibility of a buffer overrun in the filename.
    fopen_s has nothing to do with a buffer overrun on the filename. The only parameter change is that the FILE pointer is passed as a double pointer so that the function can return an error code. Also, saying fopen() has been deprecated is misleading since it is still the standards complaint way to open a file. It is only deprecated when you use a Microsoft compiler.
    bit∙hub [bit-huhb] n. A source and destination for information.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by bithub View Post
    It is only deprecated when you use a Microsoft compiler.
    And guess which compiler our friend is using...
    For you "it's only"... for him it's 100%

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Handling -Read write and edit a file
    By aprop in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 02:01 PM
  2. File handling help
    By stevy123 in forum C Programming
    Replies: 7
    Last Post: 04-20-2007, 09:31 AM
  3. file handling ....!
    By Prasad kulkarni in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-12-2002, 01:52 PM
  4. C++ file handling
    By unregistered in forum C++ Programming
    Replies: 5
    Last Post: 05-29-2002, 01:33 PM
  5. C file handling
    By valar_king in forum C Programming
    Replies: 5
    Last Post: 01-22-2002, 03:32 PM