Dec 20, 2013 at 7:56am UTC
Hi All,
I have written below program to count number of words and lines and print the all the words.
#include <iostream>
using namespace std;
#include<fstream>
#include<string.h>
int main()
{
ofstream outfile;
outfile.open("venkat",ios::out);
if(!outfile)
{
cout<<"failed in opening file";
return 1;
}
string buff("I am wrting a c++ program in filehandling");
outfile<<buff;
outfile.close();
ifstream infile;
infile.open("venkat",ios::in);
if(!infile.is_open())
{
cout<<"unable to open file"<<endl;
return 1;
}
char*s=new char[350];
char*tempw;
int wordcount,linecount;
while(infile)
{ infile.getline(s,255);
while(tempw)
{
tempw=strtok(s," ");
cout<<tempw<<endl;
wordcount ++;
}
linecount++;
}
cout<<"word count is"<<wordcount<<"linecount is"<<linecount<<endl;
infile.close();
return 0;
}
Its compiling fine but when executed its dispplaying I infinite times...
No clue where I went wrong.
Guys Please help???
Thanks in advance.
Prasad
Dec 20, 2013 at 10:50am UTC
Please use code tags when posting code, to make it more readable.
Dec 20, 2013 at 11:57am UTC
@tipaye
Thanks for your reply.
I wrote that in haste,didnt mind much about all these
Thanks for the comments....I will try to improve.