How to about cmds on linux

Hello.
I'am new at this language.
So i was trying to find over the internet how-to create something like cmds what linux haves. Like " mc /home/someone ", and mc will open that directory. I need to do almost the same, only i want to open image with Gtk.

My finished program should be executed like this "gimg /home/someone/pic.gif"
C++ programs are capable of accepting arguments from the command line. You can access them like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
//gimg.cpp- opens files in Gtk
#include <iostream>

int main(int args, char** argv){
	if (args == 1){
		std::cout << "\nspecify a filename";
	}
	else{
		char* filename = argv[0];
		//code that opens Gtk goes here.
	}
	return 0;
}
Thanks, you helped me a lot.

My gimg never worked better!
Hi
My question is also on a similar note. Though in my program i want to mount the usb drive and then write some files into the usb drive. How is it possible to give the prompt commands inside the program? Any help would be appreciated.
Thanks
Phro
Prompt commands... Do you mean like cin?

1
2
3
std::cout << "Please enter some input: ";
std::string userInput;
std::cin >> userInput;   //userInput now contains the text that the user typed 
Hello, Just a little correction for other people that may use this as a reference.
In your example argv[0] is the name of the calling program not the command line argument. the argv[1] argv[2] etc is the arguments.

1
2
3
4
5
6
7
8
9
#include <iostream>
using std::cout;
using std::endl;

int main(int args, char** argv)
{
 cout << "Hi this is " << argv[0] << endl;
return 0;
}


Jeff
Last edited on
Hey magicalblender
I dint mean program input (cout/cin), I mean to ask about commands like mkdir or cd .. can they be done inside a C program. Because at the moment i am owkring with linux. And i need to mount a usb drive before i can write information in it. What i want to do through my program is to Mount a usb drive write some files into it and unmount the usb drive. That command do unmount or mount can only be given through the cmd prompt ( mount /dev... /mnt/flash). So is there anyway to do so through the C program instead of going to cmd prompt and entering these commands.
Thanx for ur reply
Phro
closed account (L0M4jE8b)
phro, check out the system call to mount() if you're looking for a way to mount drives under GNU/Linux:

http://www.penguin-soft.com/penguin/man?q=mount&section=2&action=man

Also, to execute other commands you can run system():

http://linux.die.net/man/3/system
Last edited on
Dear fefe
Ty very much. This is the command i was looking for. I found a similar command execv but this is the exact one. Thanx again for your help

Phro
I also feel the need to make sure that the user input is gathered correctly. Typically, the user expects the input to be terminated by pressing ENTER, not by any old whitespace.
1
2
3
std::cout << "Please enter some input: ";
std::string userInput;
std::getline( std::cin, userInput );  //userInput now contains ALL the text that the user typed 

Hope this helps.
Topic archived. No new replies allowed.