Simple File Question

This is a discussion on Simple File Question within the Windows Programming forums, part of the Platform Specific Boards category; OPENFILENAME ofn; ofn.lpstrFilter = "Text Files (*.txt)

Thread: Simple File Question

*.txt

Thread: Simple File Question

All Files (*.*)

Thread: Simple File Question

*.*

Thread: Simple File Question

"; I really don't know what the code above means... I read ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    85

    Simple File Question

    OPENFILENAME ofn;
    ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";

    I really don't know what the code above means...
    I read through the tutorial but I still don't understand.
    Please help...

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    >>OPENFILENAME ofn;

    This is a structure defined in the headers of the Windows SDK. You create an instance of this structure and then initialise some of its members before using it with GetOpenFileName() or GetSaveFileName()..


    >>ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";

    You are assigning a pointer to a literal string to the lpstrFilter member of the structure.....

    This string tells the dialog box what files to list....for instance....use office or inernet explorer and try open a file....on the bottom you will have a drop down box for say HTML files......text files...word files...whatever.

    This string defines how that box works.....
    First you have text to describe the first type of files you want the dialog to try open (or save)........then this is ended with a NULL (remember '\0' in a string is a NULL char!!)....then you put a search criteria like "*.txt" - select all text files....then another NULL....repeat this process for all selections and end with 2 NULLs (on the example above there is only 1....but dont forget that the literal string itself is NULL terminated.....so that makes up the extra)

    So the above allows you to choose Text files (files with a .txt extention)...or all files (any extention)

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Fordy, wouldn't it be superfluous to put that second NULL char at the end? Wouldn't it compile to be another? Or maybe since he explicitly put one there the compiler assumes that that's the NULL one, but yet he needs another?
    1978 Silver Anniversary Corvette

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    85
    Originally posted by Fordy
    >>OPENFILENAME ofn;

    This is a structure defined in the headers of the Windows SDK. You create an instance of this structure and then initialise some of its members before using it with GetOpenFileName() or GetSaveFileName()..


    >>ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";

    You are assigning a pointer to a literal string to the lpstrFilter member of the structure.....

    This string tells the dialog box what files to list....for instance....use office or inernet explorer and try open a file....on the bottom you will have a drop down box for say HTML files......text files...word files...whatever.

    This string defines how that box works.....
    First you have text to describe the first type of files you want the dialog to try open (or save)........then this is ended with a NULL (remember '\0' in a string is a NULL char!!)....then you put a search criteria like "*.txt" - select all text files....then another NULL....repeat this process for all selections and end with 2 NULLs (on the example above there is only 1....but dont forget that the literal string itself is NULL terminated.....so that makes up the extra)

    So the above allows you to choose Text files (files with a .txt extention)...or all files (any extention)
    Very detailed explanation!!!
    Really thanks your help!!

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by Garfield
    Fordy, wouldn't it be superfluous to put that second NULL char at the end? Wouldn't it compile to be another?
    The line only has 1 NULL char as text at the end (1 * '\0')

    Originally posted by Garfield
    Or maybe since he explicitly put one there the compiler assumes that that's the NULL one, but yet he needs another?
    The compiler doesnt second guess you........I havent checked, but I assume that when you put lieral text into your code, the compiler adds a line like the Define Byte instruction in MASM ...it then defines space for the amount of chars as well as a terminating NULL byte.....then it fills the memory with the chars and puts the NULL on the end......then if you add the line as above with the '\0' on the end it just so happens that the region in memory ends with 2 NULLs....which is perfect for GetOpenFileName()

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    So is this:

    char string[] = "This is a string";

    Is this NULL terminated?
    1978 Silver Anniversary Corvette

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by Garfield
    So is this:

    char string[] = "This is a string";

    Is this NULL terminated?
    Yeah...the compiler does it for you....put this in a program and the feed it into your debugger...........When you hit the line above you will notice that string[16] = 0x00...........the NULL char

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. Simple file input question?
    By LeighRogers in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2007, 02:55 PM
  4. Very very simple file input question.
    By Kurisu33 in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2006, 12:37 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM