Saturday, May 22, 2010

For file io in c++ how do i count all the words in a file?

the question %26gt;%26gt;%26gt;6. Create a method named “totalWordCount()” that will return the value of how many TOKENS are on IN THE FILE. Display your answer to the screen.


my code is [code] # include %26lt;iostream%26gt;


# include %26lt;string%26gt;


# include %26lt;stdlib.h%26gt;


# include %26lt;stdio.h%26gt;


# include %26lt;fstream%26gt;


# include %26lt;ctype.h%26gt;


using namespace std;


void displayFile(ifstream %26amp;in);


void totalWordCount();


void main()


{


cout%26lt;%26lt;" Mateen Rehman"%26lt;%26lt;endl;


ifstream infile;


infile.open("I:/FILEIOLabDATAFILE.txt"... ios_base::in);


if(infile.fail( ))


{


cout%26lt;%26lt; " The File was not successfully open"%26lt;%26lt;endl;


exit(1);


}


displayFile(infile);


infile.close( );


{


void displayFile(ifstream %26amp;in)


}


while(!(in.eof()))


{


char line[200];


in.getline(line, 200, '\n');


}








for (int i=0; i%26lt;200; i++)


{


cout %26lt;%26lt; num1%26lt;%26lt;endl;


}


}


[/code]


my errors are 1%26gt;c:\documents and settings\hafizsahib\my documents\visual studio 2008\projects\file io\file io\file io.cpp(24) : error C2143: syntax error : missing ';' before '}'


1%26gt;c:\documents and settings\hafizsahib\my documents\visual studio 2008\projects\file io\file io\file io.cpp(25) : error C2065: 'in' : undeclared identifier


1%26gt;c:\documents and settings\hafizsahib\my documents\visual studio 2008\projects\file io\file io\file io.cpp(25) : error C2228: left of '.eof' must have class/struct/union


1%26gt; type is ''unknown-type''


1%26gt;c:\documents and settings\hafizsahib\my documents\visual studio 2008\projects\file io\file io\file io.cpp(25) : fatal error C1903: unable to recover from previous error(s); stopping compilation


thanks

For file io in c++ how do i count all the words in a file?
Okay. Let's start with the joke. Error number 1. You're using Visual Studio 2008 instead of a real compiler.





I'll assume this is homework, and you don't have a choice.


Major point here. Why do you have curly brackets AROUND the definition of the header on lines 24, 25?





The format of your program should be





//Declares


// in standard C++ they should be:


# include %26lt;iostream%26gt;


# include %26lt;string%26gt;


# include %26lt;cstdlib%26gt;


# include %26lt;cstdio%26gt;


# include %26lt;fstream%26gt;


# include %26lt;cctype%26gt;





using namespace std;





//Function Declarations


void displayFile(ifstream %26amp;in);





void main() //I use int main() and return 0


//But what do I know? I'm an old C programmer


// on Linux


{...


}





void displayFile(ifstream %26amp;in)


{


while...


}





If you look at your file, the errors start where it says -- and I am quoting:


{


void displayFile(ifstream %26amp;in)


}





DO NOT EVER declare the function name in brackets like that. They create a scope. When you close the brackets the scope ends and the name no longer exists. Further, you then have the body of the function as a series of commands in the lowest possible level. If they were to compile the program would have no means of accessing them. So the function should read:





void displayFile(ifstream %26amp;in)


{


while(!(in.eof()))


{


char line[200];


in.getline(line, 200, '\n');


}








for (int i=0; i%26lt;200; i++)


{


cout %26lt;%26lt; num1%26lt;%26lt;endl;


}


}





As for the specific assignment, I've put a link to the cstring or string.h library's strtok() function documentation in sources. Y'know around the turn of the millenium, when C++ added the STL library, a split between C and C++ programming happened -- specifically in the #includes. If it is a standard C library you are supposed to indicate it by putting a c in front and dropping the .h. If Microsoft is mixing the old libraries for C and the new ones for C++, then there is one more defective product out there to hate them for. My GCC library includes this in the cstring header file:





/** @file cstring


* This is a Standard C++ Library file. You should @c #include this file


* in your programs, rather than any of the "*.h" implementation files.


*


* This is the C++ version of the Standard C Library header @c string.h,


* and its contents are (mostly) the same as that header, but are all


* contained in the namespace @c std (except for names which are defined


* as macros in C).


*/


No comments:

Post a Comment