GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Jul-2008, 01:39
zsturgeon zsturgeon is offline
New Member
 
Join Date: Jul 2008
Posts: 1
zsturgeon is on a distinguished road

Default file input.


Does anyone know how I would go about passing arguments from a command line on to a program? For example if you use the 'Open with > program' sortof thing in a file browser, how could I read that. Or if at a command line I typed 'myprog.exe -c "Stuff1" filename.txt', how could my program read that? Thanks in advance!
  #2  
Old 09-Jul-2008, 02:03
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Default file input.


Quote:
Originally Posted by zsturgeon
Or if at a command line I typed 'myprog.exe -c "Stuff1" filename.txt', how could my program read that?
This is the simplest of your two questions. Consider the following:
CPP / C++ / C Code:
#include <iostream>

using namespace std;

int
main(int argc, char *argv[]) {
    cout << "application =\t" << argv[0] <<  endl;

    for (int i = 1; i < argc; ++i)
        cout << i << " =\t" << argv[i] << endl;

    return 0;
}
The arguments to main() define the number & array of strings passed to the application/executable. Note that passing strings to an application is part of the C++ language standard.
Quote:
For example if you use the 'Open with > program' sortof thing in a file browser, how could I read that.
This is much more complicated & is dependent upon the GUI environment used. In other words, Windows does it differently than GNOME which does it differently than KDE which does it differently than OS X, etc.

By selecting a menu item, code within the browser is able to determine either by the file extension (Windows, GNOME, KDE...) or resource fork (OS X) which application has been predefined to interpret a particular file format. At that point, the application is spawned usually with the name of the selected file as its argument, in a similar manner to how C++ allows strings to be passed to a console application as demonstrated above. At that point, it is up to the spawned application to handle its file argument in whatever manner is appropriate.

In comparison to accepting strings as demonstrated above, GUI code can take significantly more lines of code & are dependent upon the environment used. It is left to the reader as an exercise to find code demonstrating similar functionality.
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
After execution - Error cannot locate /Skin File? WSCH C++ Forum 1 05-Mar-2005 21:03
Apache2 config issues monev Apache Web Server Forum 2 28-Jun-2004 07:19
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 03:26
CD Buring Failed skanth2000 Computer Hardware Forum 1 15-Nov-2003 04:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:44.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.