Command-line using Dev-C++

Started by
17 comments, last by Carolina 18 years, 9 months ago
Quote:Original post by SlippyPulse
Hey, 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.
-Mat² §alley©-
Advertisement
Quote:Original post by MSalley
Dev 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).

To answer the original question:
Add this at the beginning of your program:
printf("Here is what argument 3 is: %s",argv[3]);
Then see what it says.
Quote:Original post by Raduprv
Add 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...
-----http://alopex.liLet's Program: http://youtube.com/user/icefox192
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
#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.
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

But thanks anyway,I´ll adapt your idea^^
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
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
Thanks,Ipve found the fstream header already^^

This topic is closed to new replies.

Advertisement