S
sahm
does anyone know how to do it..i need help
Show us the code you've written so far.sahm said:does anyone know how to do it..i need help
sahm said:#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string fileName;
char c;
cout << endl;
cout << "Enter the name of the file: ";
cin >> fileName;
cout << endl << endl;
//declares filename
ifstream d_file;
d_file.open(fileName.c_str()); //attempts to open file
if (!d_file.is_open())
{
//if file doesn't exist; don't create a new one
cout << "File " << fileName << " does not exist in the
client's current directory" << endl;
system("pause");
exit(1);
}
else
{
d_file.get(c);
d_file.seekg(0, ios::end);
int charCount = d_file.tellg();
d_file.seekg (0, ios::beg);
//prints the number of characters in a file;
cout << "the number of characters in the " << fileName;
cout << " is " << charCount << endl;
//prints the number of lines in a file
string t;
int lineCount=0;
while(getline(d_file, t, '\n'))
++lineCount;
cout << "The number of lines in the file is " <<
lineCount << endl;
//reads the number of lines in a file, then print
int wordCount=0;
string word;
for (;
{
d_file >> word;
if ( d_file.eof() ) { break;}
wordCount++;
}
cout << wordCount << endl;
}
d_file.close();
system("pause"):
return 0;
}
******
i got the read number of lines and characters in d file worked right
except for the wordCount, it kept on giving me a 0....
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.