# Command-line using Dev-C++

## Recommended Posts

Carolina    176
Hi there! I´d like to know how can I use command line in Dev-C++.I´m making a program that uses argc and argv[].However,I have no idea how can I call the program using Windows.My compiler is Dev-C++.Any ideas? Carol

##### Share on other sites
SlippyPulse    100
Hey, i used dev cpp and i didn't have a compiler so i got borland c++ compiler. Borland is a command-line compiler and it works. Try borland, it might help, I dont know if it will work for your program because i just used it on a simple program i made. Hope this helps!

##### Share on other sites
Rayno    511
I haven't really used dev-c++, but I know it uses the MinGW compiler. Which is a windows port of gcc. So to compile stuff, you just type the command "gcc". Type "gcc --help" to learn how to use it. More commonly you use makefiles and run the "make" command (again "make --help" will tell you how to use it). I don't know if dev-c++ auto-generates make files, or what.

If non of those commands work, it means that you have to add the directory that they reside to your "path" variable. To do this on XP:
Right click My Computer,
click properties,
click "Enviroment Variables",
Edit the "Path" variables under "System variables",
add ";c:\mingw\bin", or wherever its installed in (just search for gcc.exe, or make.exe).

##### Share on other sites
xidis    127
There is an option to set parameters (I forget exactly what its called or what menu roll out its in) but the option will let you pass commands to argv. Its built into dev-cpp.

##### Share on other sites
Rayno    511
Oh, ha! I guess I misunderstood the question. Sorry.

##### Share on other sites
sSimontis    100
I'm not sure what your asking, a lot of people seem to be thinking that you are talking about calling the compiler from the command line. However, if you are wondering how you run the program so that you can add the arguments, you simply go to the command line. On Windows 2000/XP, Go to Start->Run and type in 'cmd' without the quotes. On 9x/ME, type in 'command' without the quotes. You then just need to navigate to the directory where your program is, through the cd command, and then you just run your program with the arguments after the program name.

##### Share on other sites
ukdeveloper    264
Or, you can open your project, and go to Execute -> Parameters and type in the parameters you want into the little box that appears.

That has the same effect, but from within the IDE without you having to open Command Prompt and do all the dirty work yourself.

HTH,

ukdeveloper.

##### Share on other sites
thannett    214
Or you can right click the compiled executable, click create shortcut. Then right click the shortcut, go to properties, then add your parameters to the end of the Target field.

##### Share on other sites
Carolina    176
Thanks for the replies! I still haven´t solved my problem...I mean,now I´m using the DOS-Prompt to have the program activated,like this:
C:\Documents and Settings\User>Desktop\simulator 10 5 fatorial.asm

My program is a CPU processor simulator and reads some data from a file(in this case,fatorial.asm,which is a parameter).The parameters 10 and 5 are sizes of vectors I use later in the program.Anyway,the problem is, the program doesn´t work and I have a guess it is because of this command:

FILE *fp=fopen(argv[3],"r");

This is the command I use to read the file name from argv[3] and open the "fatorial.asm" file. The program goes fine up to this command,then it stops.And YEAH,´the file exists and is in the same directory as the executable file.So,what should I do? Y.Y

Thanks a lot!

##### Share on other sites
Carolina    176
Hey,I´ve tested using the Execute->Parameters of Dev-C++ and it works fine...Why the hell DOS-Prompt doesn´t?¬¬

##### Share on other sites
MSalley    109
Quote:
 Original post by SlippyPulseHey, i used dev cpp and i didn't have a compiler

Dev Cpp is a compiler too. Click on the "execute" menu and then click "compile and run."

May the force be with you. All of you.

##### Share on other sites
Quote:
 Original post by MSalleyDev Cpp is a compiler too. Click on the "execute" menu and then click "compile and run."

No, it isn't a compiler. It's an IDE. But you can download it with a compiler (gcc).

printf("Here is what argument 3 is: %s",argv[3]);
Then see what it says.

##### Share on other sites
Icefox    238
Quote:
 Original post by RaduprvAdd this at the beginning of your program:printf("Here is what argument 3 is: %s",argv[3]);Then see what it says.

Probably something along the lines of "Segmentation fault: core dumped" if you don't make sure it has that many arguments...

##### Share on other sites
Carolina    176
Hi!

The program is working fine now,when I use Dev-C++ and insert parameters before compiling(through Execute->Parameters).But now I´ve got another(simpler ^_^) question: how can I count written lines from a file?I was thinking about using fscanf(filename,"%c\n",&character) and then verifying if the result is a letter(since all the written lines at the file start with a letter).If it is,then a counter would be incremented,corresponding to the number of lines. Is this a possible solution?What you guys think?

Thanks again,

Carol

##### Share on other sites
#include <fstream>#define MAX_LENGTH 255//MAX_LENGTH is the length of the longest line in your file or longer.ifstream file;file.open("whatever.wht");int ctr = 0;char *t[MAX_LENGTH];while(!file.eof())   {      file.getline(t, MAX_LENGTH);      ctr++;   }

That will probably work to count lines in a file.

##### Share on other sites
Carolina    176
Huh...I dunno,I´ve never used this fstream header.Is ifstream a variable type?Does it work like a pointer to a file?I´ve searched around my Dev-Cpp directory and there´s no fstream file at the Include folder

##### Share on other sites
Well, fstream.h should be here
C:\Dev-Cpp\include\c++\3.3.1\backward\fstream.h

Ifstream is basically a "File input stream class"

ifstream file;

So file, is an instance of that class

##### Share on other sites
Zahlman    1682
If you look around a bit more you should be able to find the proper "fstream" file (no .h) too :) (I don't have Dev-cpp so I can't tell you exactly where, sorry.) Anyway, the ifstream object is simply the C++ version of FILE* - although cleaned up a fair bit.

Of course, if we're going to use C++ we may as well *really* use C++:

// In C++ we should use const identifiers for constants rather than #defines// where possible: thus, "const int MAX_LENGTH = 255;"// However, we won't need this value anyway :)#include <fstream>// Note that new C++ headers place all this stuff in the std namespace.// fstream.h does not, but it's a backwards-compatibility hack for old code and// should not be used these days (marked deprecated).#include <limits> // for version A#include <string> // for version B// The ifstream offers a constructor accepting the same parameters as a .open()// call, so in the normal case we may as well use it directlystd::ifstream file("whatever.wht");int counter = 0;std::string buffer; // for version B// VERSION Awhile(file.ignore(std::numeric_limits<streamsize>::max(), '\n')) { counter++; }// I think I "spelled" all of that correctly. We "ignore" (skip past) lines// until the return value (which is '*this' - i.e. 'file') evaluates false// (which happens at end of file, or if there is some other serious problem).// VERSION Bwhile(std::getline(file, buffer)) { counter++; }// Similarly, we read lines into a string (that will resize itself appropriately// regardless of the data) until EOF.

Someone else is sure to come along now and redo this in functional style ;s

Carolina    176