Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Filename through command line

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2004
    Posts
    1

    Filename through command line

    I'm in desperate help! i have a opengl app in Visual C and i need to know how to have my main function accept an input filename from the command line using the argc and argv parameters.

    my specific problem is that the main function accepts char** argv. while the function i need to call accepts FILE *filename type. PLS HELP!!

  2. #2
    Senior Member OpenGL Guru Relic's Avatar
    Join Date
    Apr 2000
    Posts
    2,527

    Re: Filename through command line

    Ouch.
    Code :
    if (argc > 1) // >1 because argv[0] is the exe name.
    {
      char *filename = argv[1]; // Assumes first parameter is your filename.
      FILE *file = fopen(filename, "rb"); // or "r" for ASCII.
      if (file)
      {
        YourFunction(file);
        fclose(file);
      }
    }
    For a Windows Win32 API program parse the
    WinMain function's parameter LPSTR lpCmdLine.

  3. #3
    Advanced Member Frequent Contributor
    Join Date
    May 2000
    Location
    Oxford, England
    Posts
    547

    Re: Filename through command line

    I always cheat
    Code :
    #include <windows.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    int OpenFileDialog(char FileName[]){
    	char Dir[512];
    	char fn[512] = {0};
    	OPENFILENAME ofn;
    	memset(&amp;ofn,0,sizeof(OPENFILENAME));
    	ofn.lStructSize = sizeof(OPENFILENAME);
    	ofn.lpstrFile = fn;
    	ofn.nMaxFile = 512;
    	ofn.lpstrTitle = "Open File";	
    	GetCurrentDirectory(512,Dir);
    	if( GetOpenFileName(&amp;ofn) ) {
    		int i;
    		for (i=0;i<512;++i){
    			if( fn[i] == '\\' )
    				fn[i] = '/';
    		}
    		strcpy(FileName,fn);
    		SetCurrentDirectory(Dir);
    		return 1;
    	}
    	FileName[0] = '\0';
    	SetCurrentDirectory(Dir);
    	return 0;
    }
    int SaveFileDialog(char FileName[]){
    	char Dir[512];
    	char fn[512] = {0};
    	OPENFILENAME ofn;
    	memset(&amp;ofn,0,sizeof(OPENFILENAME));
    	ofn.lStructSize = sizeof(OPENFILENAME);
    	ofn.lpstrFile = fn;
    	ofn.nMaxFile = 512;
    	ofn.lpstrTitle = "Save File";
    	GetCurrentDirectory(512,Dir);
    	if( GetSaveFileName(&amp;ofn) ) {
    		int i;
    		for (i=0;i<512;++i) {
    			if( fn[i] == '\\' )
    				fn[i] = '/';
    		}
    		strcpy(FileName,fn);
    		SetCurrentDirectory(Dir);
    		return 1;
    	}
    	FileName[0] = '\0';
    	SetCurrentDirectory(Dir);
    	return 0;
    }
    int main() {
    	// to use...
    	char filename[512];
    	if(OpenFileDialog(filename)) {
    		FILE* fp = fopen(filename,"rb");
     
    		// read file
     
    		fclose(fp);
    	}
    	else {
    		// cancel pressed...
    	}
    	return 0;
    }

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    May 2000
    Location
    Oxford, England
    Posts
    547

    Re: Filename through command line

    And for linux people (using gtk2)

    Code :
    #include <gtk/gtk.h>
    #include <string.h>
    #include <assert.h>
    int   g_argc=0;
    char** g_argv=0;
    char g_FileName[512]={0};
    static void file_ok_sel( GtkWidget        *w,
                             GtkFileSelection *fs ) {
    	strcpy(g_FileName,gtk_file_selection_get_filename (GTK_FILE_SELECTION (fs)));
    }
    int OpenFileDialog(char FileName[]) {
        GtkWidget *filew;
    	g_FileName[0] = '\0';
        gtk_init (&amp;g_argc, &amp;g_argv);
        filew = gtk_file_selection_new ("File selection");
        g_signal_connect (G_OBJECT (filew), "destroy",
    	              G_CALLBACK (gtk_main_quit), NULL);
        g_signal_connect (G_OBJECT (GTK_FILE_SELECTION (filew)->ok_button),
    		      "clicked", G_CALLBACK (file_ok_sel), (gpointer) filew);
        g_signal_connect_swapped (G_OBJECT (GTK_FILE_SELECTION (filew)->cancel_button),
    							 "clicked",
    							 G_CALLBACK (gtk_widget_destroy),
    			      			 G_OBJECT (filew));
        g_signal_connect_swapped (G_OBJECT (GTK_FILE_SELECTION (filew)->ok_button),
    							 "clicked",
    							 G_CALLBACK (gtk_widget_destroy),
    			      			 G_OBJECT (filew));
        gtk_widget_show (filew);
        gtk_main ();
    	if(g_FileName[0] != '\0') {
    		strcpy(FileName,g_FileName);
    		return 1;
    	}
        return 0;
    }
    int SaveFileDialog(char FileName[]) {
    	return OpenFileDialog(FileName);
    }
    int main(int argc,char **argv) {
     
    	// annoying gtk thing...
    	g_argc = argc;
    	g_argv = argv;
     
    	// to use...
    	char filename[512];
    	if(OpenFileDialog(filename)) {
    		FILE* fp = fopen(filename,"rb");
     
    		// read file
     
    		fclose(fp);
    	}
    	else {
    		// cancel pressed...
    	}
    	return 0;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •