Thread: Filename Problems

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    25

    Filename Problems

    Well, the saving part of my program works fine, it saves data like it should, but I have one problem.

    I want my program to display a list of names depending on existing files in a directory, and then let the user choose which one to load.

    For example, if the program is with three other files, list1.dat, list2.dat, and list3.dat, i want it to display those files to the user, and let the user select which one to load.

    This seems like it would be a fairly common thing to do (returning a list of filenames in a specific directory), so there shouldn't be any trouble answering this one. I browsed the forums, and did a few searches, but found nothing.

    Thanks. =)

  2. #2
    Hello,

    If I understand correctly, you are looking for a File Directory Dialog for your program. There is a function that allows this. It is called GetOpenFileName. The GetOpenFileName function creates an Open dialog box that lets the user specify the drive, directory, and the name of a file or set of files to open.

    Simply, this function opens a dialog in which the user can select a filename, returning the path and filename in a string. The filter parameter lets you select which files to display in the list. The first and only parameter is a pair of values, a description and filter spec, delimited by a null character. For example, "Bitmap (*.bmp)\0*.bmp". Also, you can add multiple filters that are themselves delimited by null characters. If the filter is an empty string, then the filter *.* will be used by default.

    If the user specifies a file name and clicks the OK button, the return value is nonzero.

    The first parameter is a pointer to an OPENFILENAME structure that contains information used to initialize the dialog box. When GetOpenFileName returns, this structure contains information about the user"s file selection.

    Here is a quick example of how this can work:
    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    BOOL FileOpen(void) {
    	BOOL result;						// Result of open
    	OPENFILENAME ofn;					// OFN declaration
    	char szFileName[MAX_PATH];				// File name
    
    	ZeroMemory(&ofn, sizeof(ofn));				// Init ofn
    	szFileName[0] = 0;					// Init char array
    	ofn.lStructSize = sizeof(ofn);				// Set structure size
    	ofn.hwndOwner = 0;					// The handle
    	ofn.lpstrFilter = "Text Files ( *.txt )\0 *.txt\0";	// File filter
    	ofn.lpstrFile = szFileName;				// File name variable
    	ofn.nMaxFile = MAX_PATH;				// Max file
    	ofn.lpstrDefExt = "txt";				// Default Extension
    	ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;	// File must exist | Hide read only
    
    	if (result = GetOpenFileName(&ofn)) {			// If file selected
    		cout << "Filename: " << szFileName;		// Print it
    	}
    
    	return result;						// Return result
    }
    Please keep in mind that this code has not been tested. It is just the general concept of how this function works. You can always do Google or MSDN searches for more information.

    If you are wanting to do this procedure Console based, please let me know.


    - Stack Overflow
    Last edited by Stack Overflow; 12-01-2004 at 07:18 PM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Quite simply, in Standard C++, you can't. Although the functionality would be nice, it is far to system dependent to put into the standard library. So, if you want to get into Windows programming (or other OS of your choice), then go with what Stack said (or something similar). If you plan to just learn standard C++ right now, though, then directory operations will have to wait.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    25
    Hrmm well, I do think it is probably about time I try it. I have a good grasp on logic I think, I just have to work on crazy pointer solutions, and have google at hand.

    Mind directing me to a good place to start? I'd hate to have written all this code for nothing, although I know another way I can do it console based, which would to be just make one huge database file, and seperate the lists with one delimeter, the contacts with another, and go about it that way. It might perhaps even be easier because I could write something to search the file for the various list names, identify them, and then use those to let the user decide which list to load. That's just assuming all that will work.

  5. #5

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Unfortunately, I cannot direct you to any Windows specific sites except for MSDN: http://msdn.microsoft.com/
    (I rarely use Windows, much less program for it.)

    I would recommend doing this one in pure C++. The reaons I say this (aside from a slight personal bias against Win32 API):
    - From a design perspective, you'd want to be careful about where you put Windows code. That is, your program will be mostly portable because you are writing most of it in pure C++. Only a small bit of functionality comes from Win32 API (or another library which deep down would use Win32 API anyway). So, unless you have a very speed critical application that needs to use this functionality (usually games on Windows, and parallel scientific simulation on larger proprietary clusters and supercomputers), then it is a good practice to design an interface so that only a small part of the code knows anything about the system specific code, and is accessible through an interface (the implementation of which may change from platform to platform).
    - To get a sense for how you would go about designing the system (what functionality is where, etc), a more structured approach to learning something like Win32 API would probably be a wise idea. (The type of which you'd find in a good introductory book, which perhaps someone with a bit more Win32 knowledge than I could recommend).

    In short, before messing with system specific code, I'd recommend learning more about design patterns (you can search Google for this type of stuff, and I can also recommend a book : http://www.amazon.com/exec/obidos/tg...87360?v=glance) so that when you do start with Windows stuff, you are in much better shape as far as looking at things from the perspective of designing a system so that its components interact well, and, importantly, you keep factors such as portability in mind (maybe not even to an OS, but perhaps just using new/better API techniques than the old ones) when you move into that kind of coding, and you'd avoid the intermediate step of writing atrocious code that often appears in the form of ill designed attempts to use libraries/APIs such as Win32.

    The reason I say this is because starting with good programming habits early is greatly beneficial. You'd probably be able to easily implement it just fine either way, though, so that is just my two cents.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  2. Time as filename
    By rkooij in forum C Programming
    Replies: 8
    Last Post: 03-02-2006, 09:17 AM
  3. Generic Resource_Manager WIP with lots TODO
    By Shamino in forum C++ Programming
    Replies: 19
    Last Post: 02-01-2006, 01:55 AM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. Getting FULL filename from listbox
    By Garfield in forum Windows Programming
    Replies: 8
    Last Post: 01-27-2002, 08:28 AM