Thread: i want to read a txt file

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    157

    i want to read a txt file

    i have implemented my program now where it gets a file (txt) from the user via an open dialog box and now i have the file path and title. but, now with this i want to open up and have the capabilities to read the file and get the data from it. i know how to do it with standard c (fopen, fclose, fgets, etc.) but now i'm working through the winapi and i never had to work with files and the winapi before so i am not familiar with the necessary functions i will need.

    anybody know these functions and params and the such? thanks!

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    If your happy with fopen and the rest then use them.....there's nothing from stopping you using those funcs for simple file reading/writting

  3. #3

  4. #4
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218
    Just so you know,
    what blank showed you is part of the win API common dialog boxes such as choosefont & choosecolor.

    this one is openfilename common dlg.

    what you do initialize it first(filling out the structure of type ofn)

    then use function GetOpenFileName(,,,,) to open a file for you.

    its simple and very handy and safe.

    below is an example;


    Code:
    #include <windows.h>
    #include <commdlg.h>
    
    static OPENFILENAME ofn ;
    
    void FileInitialize (HWND hwnd)
    {
         static char szFilter[] = TEXT ("Text Files (*.TXT)\0*.txt\0")  \
                                   TEXT ("ASCII Files (*.ASC)\0*.asc\0") \
                                   TEXT ("All Files (*.*)\0*.*\0\0") ;
         
         ofn.lStructSize       = sizeof (OPENFILENAME) ;
         ofn.hwndOwner         = hwnd ;
         ofn.hInstance         = NULL ;
         ofn.lpstrFilter       = szFilter ;
         ofn.lpstrCustomFilter = NULL ;
         ofn.nMaxCustFilter    = 0 ;
         ofn.nFilterIndex      = 0 ;
         ofn.lpstrFile         = NULL ;          //  Open and Close functions
         ofn.nMaxFile          = MAX_PATH ;
         ofn.lpstrFileTitle    = NULL ;          // Open and Close functions
         ofn.nMaxFileTitle     = MAX_PATH ;
         ofn.lpstrInitialDir   = NULL ;
         ofn.lpstrTitle        = NULL ;
         ofn.Flags             = 0 ;             //  Open and Close functions
         ofn.nFileOffset       = 0 ;
         ofn.nFileExtension    = 0 ;
         ofn.lpstrDefExt       = TEXT ("txt") ;
         ofn.lCustData         = 0L ;
         ofn.lpfnHook          = NULL ;
         ofn.lpTemplateName    = NULL ;
    }
    
    BOOL FileOpenDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
    {
         ofn.hwndOwner         = hwnd ;
         ofn.lpstrFile         = pstrFileName ;
         ofn.lpstrFileTitle    = pstrTitleName ;
         ofn.Flags             = OFN_HIDEREADONLY | OFN_CREATEPROMPT ;
         
         return GetOpenFileName (&ofn) ;
    }
    
    BOOL filesavedlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
    {
         ofn.hwndOwner         = hwnd ;
         ofn.lpstrFile         = pstrFileName ;
         ofn.lpstrFileTitle    = pstrTitleName ;
         ofn.Flags             = OFN_OVERWRITEPROMPT ;
         
         return GetSaveFileName (&ofn) ;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. read values from a txt file
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 08-30-2002, 01:37 PM