PDA

View Full Version : Filename through command line



dumbie
07-08-2004, 11:04 PM
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!!

Relic
07-09-2004, 12:14 AM
Ouch.

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.

Rob The Bloke
07-10-2004, 02:14 AM
I always cheat :eek:


#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;
}

Rob The Bloke
07-10-2004, 02:17 AM
And for linux people (using gtk2)



#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;
}