Thread: OpenFileDialog Window in C

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    Umm...usually for specifying the library, you need -luser32. The -l tells you that's the library name to use, gcc will prefix it with "lib" and suffix it with ".a" or ".so" or ".lib" as needed. The -L only tells gcc what folders to search for the library files.
    Thanks Anduril... I haven't used DEV since the pascal days...

    He may need comdlg32 as well... since the GetOpenFileName() call is part of the common dialogs collection.

    In Pelles this is beyond simple simply #define WIN32_DEFAULT_LIBS and pragmas in the headers select the libs for me...

  2. #17
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    Ok, so that's fixed. But, another problem has occured, the openfile window that is suppose to open, doesn't open. In fact, nothing happens at all. Here is the code to open the window.
    Code:
         char FileName[MAX_PATH];
    
         // pick a program file
         int ChooseProgram(PCHAR FileName)
         { 
             OPENFILENAME  ofn;        
             memset(&ofn,0,sizeof(ofn));
             ofn.lStructSize     = sizeof(ofn);
             ofn.hwndOwner       = NULL;
             ofn.hInstance       = NULL;
             ofn.lpstrFilter     = "Text Files\0*.txt\0\0";    
             ofn.lpstrFile       = FileName;
             ofn.nMaxFile        = MAX_PATH;
             ofn.lpstrTitle      = "Please Select A File To Open";
             ofn.Flags           = OFN_NONETWORKBUTTON |
                              OFN_FILEMUSTEXIST |
                              OFN_HIDEREADONLY;
        if (!GetOpenFileName(&ofn))
          return(0);
       return 1; }
    I have windows.h included, as well as it is linked with comdlg32.lib. Any ideas?

  3. #18
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You're welcome. It's just a guess though, since I've never used DEV, or GCC in Windows, hence the "Umm...". I can't imagine they'd change the command line flags though.

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by binks View Post
    Ok, so that's fixed. But, another problem has occured, the openfile window that is suppose to open, doesn't open. In fact, nothing happens at all. Here is the code to open the window.
    Code:
         char FileName[MAX_PATH];
    
         // pick a program file
         int ChooseProgram(PCHAR FileName)
         { 
             OPENFILENAME  ofn;        
             memset(&ofn,0,sizeof(ofn));
             ofn.lStructSize     = sizeof(ofn);
             ofn.hwndOwner       = NULL;
             ofn.hInstance       = NULL;
             ofn.lpstrFilter     = "Text Files\0*.txt\0\0";    
             ofn.lpstrFile       = FileName;
             ofn.nMaxFile        = MAX_PATH;
             ofn.lpstrTitle      = "Please Select A File To Open";
             ofn.Flags           = OFN_NONETWORKBUTTON |
                              OFN_FILEMUSTEXIST |
                              OFN_HIDEREADONLY;
        if (!GetOpenFileName(&ofn))
          return(0);
       return 1; }
    I have windows.h included, as well as it is linked with comdlg32.lib. Any ideas?
    I've had this problem too... most often it's because there's random junk in the variable at ofn.lpstrFile (FileName in this case). Try setting FileName[0] = 0 before making the call.
    Code:
         char FileName[MAX_PATH];
    
         // pick a program file
         int ChooseProgram(PCHAR FileName)
         {  FileName[0] = 0;
             OPENFILENAME  ofn;        
             memset(&ofn,0,sizeof(ofn));
             ofn.lStructSize     = sizeof(ofn);
             ofn.hwndOwner       = NULL;
             ofn.hInstance       = NULL;
             ofn.lpstrFilter     = "Text Files\0*.txt\0\0";    
             ofn.lpstrFile       = FileName;
             ofn.nMaxFile        = MAX_PATH;
             ofn.lpstrTitle      = "Please Select A File To Open";
             ofn.Flags           = OFN_NONETWORKBUTTON |
                              OFN_FILEMUSTEXIST |
                              OFN_HIDEREADONLY;
        if (!GetOpenFileName(&ofn))
          return(0);
       return 1; }
    Alternatively you could initialise FileName to the name of the file you're suggesting they open. Just so it's not junk.

  5. #20
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    I added FileName[0] = 0;, and still no window. Any ideas?

  6. #21
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by binks View Post
    I added FileName[0] = 0;, and still no window. Any ideas?
    I got some bad news for you...
    Using exactly this code...
    Code:
    #include <windows.h>
    
    int ChooseProgram(PCHAR FileName)
      {  FileName[0] = 0;
         OPENFILENAME  ofn;        
         memset(&ofn,0,sizeof(ofn));
         ofn.lStructSize     = sizeof(ofn);
         ofn.hwndOwner       = NULL;
         ofn.hInstance       = NULL;
         ofn.lpstrFilter     = "Text Files\0*.txt\0\0";    
         ofn.lpstrFile       = FileName;
         ofn.nMaxFile        = MAX_PATH;
         ofn.lpstrTitle      = "Please Select A File To Open";
         ofn.Flags           = OFN_NONETWORKBUTTON |
                              OFN_FILEMUSTEXIST |
                              OFN_HIDEREADONLY;
        if (!GetOpenFileName(&ofn))
          return(0);
       return 1; }
    
    
    int _cdecl main(int argc, char *argv[])
      { 
        char FileName[MAX_PATH];
        ChooseProgram(FileName);
        return 0; }
    It works every time on Win7, winXP and even my old win2000 server machine....

    Post your code, lets see what's going on...
    Last edited by CommonTater; 03-23-2011 at 09:18 PM.

  7. #22
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    Alright, here we go:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <time.h>
    #include <stdlib.h>
    #include <windows.h>
    
    #define DELAY 0.1
    
    void options(void);
    void write(void);
    void read(void);
    
    void pause(void) // creates 1 second pause
    {
         time_t second;
         
         time(&second);
         while(difftime(time(NULL),second) < DELAY)
             ;
    }
    
    int main()
    {
        FILE *password;
        char enter_pass[11]; // used to make a password - open password
        char temp_input[11];
        int i; // used when string compare returns a value for password check
        char menu;
        int s; // encryption variable
        
        password = fopen("Resources\\Datafiles\\password.dat","r");
        
        if(password == NULL)
        {
                    password = fopen("Resources\\Datafiles\\password.dat","w");
                    
                    printf("Enter a password between 1 to 10 characters.\n");
                    scanf("%s",enter_pass);
                    
                    for (s = 0;s < sizeof(enter_pass); ++s) // encryption
                        enter_pass[s] = enter_pass[s] + 13;                
                    
                    fprintf(password,"%s",enter_pass);
                    printf("Password Saved!\n\n");
                    
                    fclose(password);
        }
        
        fread(enter_pass,11,1,password);
        
        for (s = 0;s < sizeof(enter_pass); ++s) // decryption
            enter_pass[s] = enter_pass[s] - 13;    
        
        printf("Enter Password: ");
        scanf("%s",temp_input);
        
        i = strcmp(enter_pass,temp_input);
        
        if(i != 0)
        {
             printf("\nIncorrect Password\n");
             pause();
             exit(0);
        }
        else
        {
            printf("\nCorrect Password, Access Granted!\n");
        }
        
        fclose(password);
        
        printf("\nWhat would you like to do?\n\n");
        printf("A - Write\n");
        printf("B - Read\n");
        printf("C - Extras\n");
        
        getchar(); // Absorbs the enter -> some1 @ wololo.net/talk/
        menu = getchar();
        fflush(stdin);
        menu = toupper(menu);
        
        switch(menu)
        {
                    case('A'):
                              write();
                              break;
                    case('B'):
                              read();
                              break;
                    case('C'):
                              options();
                              break;
                    default:
                            printf("Invalid entry\n");
                            pause();
                            exit(0);
        }
        
        getch();
        return(0);
    }
    
    void options(void)
    {
         FILE *read;
         char menu;
         char ch;
         
         printf("\nA - Changelog\n");
         printf("B - TODO List\n");
         printf("C - Credits\n");
         
         menu = getchar();
         fflush(stdin);
         menu = toupper(menu);
         
         switch(menu)
         {
                     case('A'):
                               read = fopen("Resources\\changelog.txt","r");
                               
                               printf("Openning file... \n\n\n");
                               
                               while(1)
                               {
                                       ch = fgetc(read);
                                       if(ch == EOF)
                                             break;
                                       printf("%c",ch);
                               }
                               
                               printf("\n\n\nFile successfully read!\n");
                               
                               fclose(read);
                               break;
                     case('B'):
                               read = fopen("Resources\\TODO.txt","r");
                               
                               printf("Openning file... \n\n\n");
                               
                               while(1)
                               {
                                       ch = fgetc(read);
                                       if(ch == EOF)
                                             break;
                                       printf("%c",ch);
                               }
                               
                               printf("\n\n\nFile successfully read!\n");
                               
                               fclose(read);
                               break;
                     case('C'):
                               read = fopen("Resources\\credits.txt","r");
                               
                               printf("Openning file... \n\n\n");
                               
                               while(1)
                               {
                                       ch = fgetc(read);
                                       if(ch == EOF)
                                             break;
                                       printf("%c",ch);
                               }                           
                               
                               printf("\n\n\nFile successfully read!\n");
                               
                               fclose(read);
                               break;
                     default:
                             printf("Incorrect entry\n");
                             pause();
                             exit(0);
         }
    }                
    
    void write(void)
    {
         FILE *write;
         FILE *nfile;
         char type;
         
         
         
         printf("\nBegin typing - Press ~ then enter to save and quit\n\n\n");
         
         write = fopen("Resources\\Documents\\write.dat","w");
         
         while((type = getc(stdin)) != '~')
               fputc(type,write);
    
         printf("\n\nSaving & Quiting...\n");
         fclose(write);
         pause();
         
         exit(0);
    }
                                                  
    void read(void)
    {
         FILE *read;
         char text;
         
         char FileName[MAX_PATH];
         
         // pick a program file
         int ChooseProgram(PCHAR FileName)
         {
             FileName[0] = 0; 
             OPENFILENAME  ofn;        
             memset(&ofn,0,sizeof(ofn));
             ofn.lStructSize     = sizeof(ofn);
             ofn.hwndOwner       = NULL;
             ofn.hInstance       = NULL;
             ofn.lpstrFilter     = "Text Files\0*.txt\0\0";    
             ofn.lpstrFile       = FileName;
             ofn.nMaxFile        = MAX_PATH;
             ofn.lpstrTitle      = "Please Select A File To Open";
             ofn.Flags           = OFN_NONETWORKBUTTON |
                              OFN_FILEMUSTEXIST |
                              OFN_HIDEREADONLY;
        if (!GetOpenFileName(&ofn))
          return(0);
       return 1; }
         int _cdecl main(int argc, char *argv[])
      { 
        char FileName[MAX_PATH];
        ChooseProgram(FileName);
        return 0; }
         /*printf("\nReading file... \n\n\n");
         
         read = fopen("Resources\\Documents\\write.dat","r");
         
         while((text = getc(read)) != EOF)
               putchar(text);
         
         printf("\n\n\nFile Successfully read!\n");
         fclose(read);*/
    }
    The commented out code was what I had before, but it was only hardcoded to 1 file. I think I have good programming habits, so it shouldn't be too difficult to read.

  8. #23
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... this is what happens when you try to do "scoop and poop" coding... you end up with free standing functions right in the middle of your other functions... and C does not allow that.

    Take a real close look at what you've got here...
    Code:
                                                 
    void read(void)
    {
         FILE *read;
         char text;
         
         char FileName[MAX_PATH];
         
         // pick a program file                                     <<--- this is a free standing function!
         int ChooseProgram(PCHAR FileName)
         {
             FileName[0] = 0; 
             OPENFILENAME  ofn;        
             memset(&ofn,0,sizeof(ofn));
             ofn.lStructSize     = sizeof(ofn);
             ofn.hwndOwner       = NULL;
             ofn.hInstance       = NULL;
             ofn.lpstrFilter     = "Text Files\0*.txt\0\0";    
             ofn.lpstrFile       = FileName;
             ofn.nMaxFile        = MAX_PATH;
             ofn.lpstrTitle      = "Please Select A File To Open";
             ofn.Flags           = OFN_NONETWORKBUTTON |
                              OFN_FILEMUSTEXIST |
                              OFN_HIDEREADONLY;
        if (!GetOpenFileName(&ofn))
          return(0);
       return 1; }    
    
     int _cdecl main(int argc, char *argv[])   <<--- this is the main functtion from my example.
      { 
        char FileName[MAX_PATH];
        ChooseProgram(FileName);
        return 0; } 
    
    
         /*printf("\nReading file... \n\n\n");  
       
         read = fopen("Resources\\Documents\\write.dat","r");
         
         while((text = getc(read)) != EOF)
               putchar(text);
         
         printf("\n\n\nFile Successfully read!\n");
         fclose(read);*/
    }
    That is NEVER going to work....

    Stop trying to cut and paste (scoop and poop) and learn how to write your own code!
    Last edited by CommonTater; 03-24-2011 at 02:53 PM.

  9. #24
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    So I prototyped the functions (I thought they were in windows.h) but still not working. They are prototyped as
    Code:
    int ChooseProgram(PCHAR FileName);
    int _cdecl main(void);
    I tried the second one like this
    Code:
    int _cdecl main(int argc, char *argv[]);
    but got a whole lot of errors. Nothing has worked! Still no window.

  10. #25
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by binks View Post
    So I prototyped the functions (I thought they were in windows.h) but still not working. They are prototyped as
    Code:
    int ChooseProgram(PCHAR FileName);
    int _cdecl main(void);
    I tried the second one like this
    Code:
    int _cdecl main(int argc, char *argv[]);
    but got a whole lot of errors. Nothing has worked! Still no window.
    That's right... you're getting a ton of errors...

    BECAUSE you took a complete program -- notice the main function????--- and pasted it into the middle of an ill formed subroutine in your own code. Very simply... that is not going to work.

    It betrays a complete lack of understanding about C programming and even the code examples I gave you...
    That's the problem with scoop and poop programming... you don't learn a darned thing from any of it.

    Now, you get to figure this one out on your own.

  11. #26
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    I'll try...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  3. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM

Tags for this Thread