Tuesday, July 28, 2009

Please need help urgently with c++ code?

how to make a c++ program to count lines in text file


please i need a simple way

Please need help urgently with c++ code?
That is going to be quite difficult. If you are still stuck with your project assignment may be you can contact a programming expert live at website like http://askexpert.info/ .
Reply:check http://www.planet-source-code.com
Reply:There's a tool that will help you find it. Its called Google. (Or Yahoo! Search)
Reply:first u open the file using io.open function.


then chek until the null character is found u will increment a counter suppose c.


then if compiler will found null character it will also increment another counter suppose l and then it will come to next line


in this way finally whatever the value of l that will be your answer
Reply:The simplest way I can think of is to read each line, and count how many you get. Maybe easier for me to say than for you to do, so I wrote this example to illustrate:





#include %26lt;fstream%26gt;


#include %26lt;iostream%26gt;





using namespace std;





const int MAX_LINE = 256;





int main(int argc, char *argv[]) {


char line[MAX_LINE];


int count = 0;





if (argc %26gt; 1) {


ifstream in(string(argv[1]).c_str());


if (in.good() == true) {


while (in.getline(line,MAX_LINE,'\n') != NULL) {


++count;


}


}


}


cout %26lt;%26lt; count %26lt;%26lt; endl;


return 0;


}


No comments:

Post a Comment