does anyone know how to do it..i need help 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.
#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....
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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:
<<...
|
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?
|
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...
|
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...
|
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...
|
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.
|
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...
|
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...
|
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,...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
| |