473,320 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

reading numbers of words and lines in text file in C++

does anyone know how to do it..i need help

Jul 22 '05 #1
3 4300
"sahm" <ab*******@yahoo.com> writes:
does anyone know how to do it..i need help

Show us the code you've written so far.

Jul 22 '05 #2
#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....

Jul 22 '05 #3
sahm wrote:
#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....


That's logical. You open a file, read it until the end to get the line
count, then you try to read on to get the word count. Since you're already
at the end of file, you break out of the loop immediately and the count
stays at 0. Seek to the beginning and restet the stream's eof flag after
the first loop.

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: adpsimpson | last post by:
Hi, I have a file which I wish to read from C++. The file, created by another programme, contains both text and numbers, all as ascii (it's a .txt file). A sample of the file is shown below: <<...
15
by: djj858 | last post by:
Another newbie question: How do I begin reading data, but starting from the xth line down a list? In other words, how do I skip the first lines and not read in those values?
1
by: Mr. B | last post by:
VB.net 2003 c/w Framework 1.1 and MS Access db We have a commercial program that does our Acounting and Time Sheets (Timberline). At least once a day our Accounting department runs a Script...
2
by: Eric Lilja | last post by:
Hello, I have a text file with following format: number one_word string number number char string There may be several lines of the last type (number number char string), the number of such...
11
by: Girish Sahani | last post by:
I wrote the following code to concatenate every 2 keys of a dictionary and their corresponding values. e.g if i have tiDict1 = tiDict1 = {'a':,'b':} i should get tiDict2={'ab':} and similarly for...
1
by: ychen020 | last post by:
Hi all, I'm trying to read in a config file (text file) that looks something like this: %Number1 Paragraph with words. Paragraph with words. Paragraph with words.
5
by: lim4801 | last post by:
I am currently in doing a program which is given by my tutor: Contemplate that you are working for the phone company and want to sell "special" phone numbers to companies. These phone numbers are...
1
by: shrimpy | last post by:
hi every one, i am new to python, and coz i want to write a handy command for my linux machine, to find a word in all the files which are under the current folder. the code is half done, but...
32
by: Bill Cunningham | last post by:
I am interested in writing a numeric text reader. This only reads numbers of securities and stores them. Nice practice. I have determined that these functions are needed. isalpha, isdigit,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.