Need to print out number of words and lines in a file
My instructor kind of assumed we'd know how to do this. I kind of get the idea, but I don't know what to do with the cstring I generate when I tokenize it.
He said to:
Write a program that prompts the user for the name of a file. Then it opens the file, and counts the number of words and lines in the file, and prints out those counts.
I think I possibly could somehow use a counter to increment using getLIne() until getLine() returns NULL, but the problem is, I'm worried if I try that, a file that looks like this:
Bla bla bla bla bla lkfdljkfaklafdskjladsjkdfkjlkdfjdfshafdsjkjrerjkkjfaddjkfsafkjdjakdfsjkasfjkjkfdskjldfjkfjkdjfkdsakdjfkjfdkjdfskjfdskjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjdfkerea
blkjadkjlfdskjldfkjlfdkjfdjkdfsjkldfskljfksfdljfd
Only register four lines and not get the one after the fourth line.
So I was thinking of something like
Code:
#include <iostream>
#include<ifstream>
#include<cstring>
#include<string>
#include<vector>
#include <cstdlib>
using namespace std;
iostream keyboard;
ifstream file;
iostream cin;
iostream cout;
vector<int> v;
int main()
{
int lineCount = 0;
string filename;
cout << "Enter a file name.";
cin >> filename;
file(filename);
char* ch;
int tempWord = 0;
ch = strtok(line, "%s\");
while (ch != NULL)
{
tempWord++;
}
v.push_back(tempWord);
lineCount++;
while(!file.eof())
{
string line = file.nextLine();
char* ch;
int tempWord = 0;
ch = strtok(line, "%s\");
while (ch !=NULL)
{
tempWord++;
}
v.push_back(tempWord);
lineCount++;
}
cout <<"Line count: " << lineCount;
int total;
for (int i =0; i < v.size(); i++)
{
total = total + v[i];
}
cout << "Word count: " << total;
return 0;
}
Did I do that right?
Will that count the number of words and number of lines correctly?
I really need help now. It's due tomorrow. http://www.javaprogrammingforums.com/yemotions/63.gif
Re: Need to print out number of words and lines in a file
I've updated it to this and it's still getting a bunch of errors:
I really need this solved today.
Code:
#include <iostream>
#include<fstream>
#include<istream>
#include<string>
#include<vector>
#include <cstdlib>
#include "string.h"
using namespace std;
iostream keyboard;
ifstream file;
vector<int> v;
int main()
{
int lineCount = 0;
string filename;
cout << "Enter a file name.";
cin >> filename;
file(filename);
string line;
line = file.getline();
char* ch;
int tempWord = 0;
ch = strtok(line, " ");
while (ch != NULL)
{
tempWord++;
}
v.push_back(tempWord);
lineCount++;
while(!file.eof())
{
file >> file.getline();
char* ch;
int tempWord = 0;
ch = strtok(line, " ");
while (ch !=NULL)
{
tempWord++;
}
v.push_back(tempWord);
lineCount++;
}
cout <<"Line count: " << lineCount;
int total;
for (int i =0; i < v.size(); i++)
{
total = total + v[i];
}
cout << "Word count: " << total;
return 0;
}
/usr/include/c++/4.3/istream:801: error: ‘std::basic_iostream<_CharT, _Traits>::basic_iostream() [with _CharT = char, _Traits = std::char_traits<char>]’ is protected
WordCount.cpp:10: error: within this context
WordCount.cpp: In function ‘int main()’:
WordCount.cpp:21: error: no match for call to ‘(std::ifstream) (std::string&)’
WordCount.cpp:24: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::getline()’
/usr/include/c++/4.3/istream:598: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:409: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]
WordCount.cpp:27: error: cannot convert ‘std::string’ to ‘char*’ for argument ‘1’ to ‘char* strtok(char*, const char*)’
WordCount.cpp:38: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::getline()’
/usr/include/c++/4.3/istream:598: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:409: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]
WordCount.cpp:42: error: cannot convert ‘std::string’ to ‘char*’ for argument ‘1’ to ‘char* strtok(char*, const char*)’
Re: Need to print out number of words and lines in a file
Quote:
Originally Posted by
jedipenguin
I really need this solved today.
Never mention your time frame. Your post is no more important or urgent than any other post.
Code:
#include <iostream>
#include<fstream>
#include<istream>
#include<string>
#include<vector>
#include <cstdlib>
#include "string.h"
using namespace std;
iostream keyboard;
I have never heard of this type called "iostream", and neither has the compiler (your very first error). There is a header called <iostream>, but there is no such type.
Regards,
Paul McKenzie
Re: Need to print out number of words and lines in a file
When I removed that from the header, it could no longer figure out what cin and cout are. As I said, I've very little knowledge of C++ and am quite furious, if more with the instructor for daring to give us the difficult assignment, especially as this isn't a C++ course even, than with anyone else.
I cooled down a bit and was able to eliminate some of the errors. Actually, there is most definitely a legal include like:
#include<iostream>
I've had it legally in other C++ classes that I wrote before.
Now I have these left as errors:
WordCount.cpp: In function ‘int main()’:
WordCount.cpp:23: error: no match for call to ‘(std::ifstream) (std::string&, const std::_Ios_Openmode&)’
WordCount.cpp:26: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::getline()’
/usr/include/c++/4.3/istream:598: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:409: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]
WordCount.cpp:41: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::getline()’
/usr/include/c++/4.3/istream:598: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:409: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]
Re: Need to print out number of words and lines in a file
Ok, it's nearly there, but what's wrong now?
Code:
#include<fstream>
#include<istream>
#include<string>
#include<vector>
#include<iostream>
#include<stdio.h>
#include "string.h"
using namespace std;
ifstream keyboard;
ifstream& file;
vector<int> v;
int main()
{
string& str;
int lineCount = 0;
char* filename;
cout << "Enter a file name.";
cin >> filename;
file(filename, ifstream::in);
char* line;
file = getline(file, str );
char* ch;
int tempWord = 0;
ch = strtok(line, " ");
while (ch != NULL)
{
tempWord++;
}
v.push_back(tempWord);
lineCount++;
while(!file.eof())
{
file = getline(file,str);
char* ch;
int tempWord = 0;
ch = strtok(line, " ");
while (ch !=NULL)
{
tempWord++;
}
v.push_back(tempWord);
lineCount++;
}
cout <<"Line count: " << lineCount;
int total;
for (int i =0; i < v.size(); i++)
{
total = total + v[i];
}
cout << "Word count: " << total;
return 0;
}
WordCount.cpp:12: error: ‘file’ declared as reference but not initialized
WordCount.cpp: In function ‘int main()’:
WordCount.cpp:17: error: ‘str’ declared as reference but not initialized
WordCount.cpp:24: error: no match for call to ‘(std::basic_ifstream<char, std::char_traits<char> >) (char*&, const std::_Ios_Openmode&)’
WordCount.cpp:27: error: no match for ‘operator=’ in ‘file = std::getline [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((std::basic_istream<char, std::char_traits<char> >&)(&((std::ifstream*)file)->std::basic_ifstream<char, std::char_traits<char> >::<anonymous>)), ((std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((std::string*)str)))’
/usr/include/c++/4.3/iosfwd:86: note: candidates are: std::basic_ifstream<char, std::char_traits<char> >& std::basic_ifstream<char, std::char_traits<char> >::operator=(const std::basic_ifstream<char, std::char_traits<char> >&)
WordCount.cpp:42: error: no match for ‘operator=’ in ‘file = std::getline [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((std::basic_istream<char, std::char_traits<char> >&)(&((std::ifstream*)file)->std::basic_ifstream<char, std::char_traits<char> >::<anonymous>)), ((std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((std::string*)str)))’
/usr/include/c++/4.3/iosfwd:86: note: candidates are: std::basic_ifstream<char, std::char_traits<char> >& std::basic_ifstream<char, std::char_traits<char> >::operator=(const std::basic_ifstream<char, std::char_traits<char> >&)
Re: Need to print out number of words and lines in a file
Quote:
Originally Posted by
jedipenguin
Now I have these left as errors:
WordCount.cpp: In function ‘int main()’:
WordCount.cpp:23: error: no match for call to ‘(std::ifstream) (std::string&, const std::_Ios_Openmode&)’
Please post your updated code.
What are you trying to do on that line (also, it helps if you showed us where line 23 is, as you didn't post your code, and no one is going to count 23 lines).
Code:
cin >> filename;
file(filename);
Is that how you open a file? So how do you open a file:
http://www.cplusplus.com/reference/iostream/fstream/
I see an open() function. So let's try it:
Code:
cin >> filename;
file.open(filename);
OK, it still is an error, since "filename" is a std::string, and open() requires a const char*. So how do we fix that?
http://www.cplusplus.com/reference/string/string/
I see a c_str() that returns exactly what we want. So adding this all up:
Code:
cin >> filename;
file.open(filename.c_str());
And bingo, this compiles.
All of this information I showed you is available right on the web.
Regards,
Paul McKenzie
Re: Need to print out number of words and lines in a file
Perhaps I've altered it since then, but right now it's not compiling.
Code:
#include<fstream>
#include<istream>
#include<string>
#include<vector>
#include<iostream>
#include<stdio.h>
#include<ios>
#include "string.h"
using namespace std;
ifstream keyboard;
ifstream file;
vector<int> v;
int main()
{
string str;
int lineCount = 0;
string filename;
cout << "Enter a file name.";
cin >> filename;
file.open(filename.c_str());
char* line;
file = getline(line, streamsize(10000), str );
char* ch;
int tempWord = 0;
ch = strtok(line, " ");
while (ch != NULL)
{
tempWord++;
}
v.push_back(tempWord);
lineCount++;
while(!file.eof())
{
file = getline(line, streamsize(10000), str);
char* ch;
int tempWord = 0;
ch = strtok(line, " ");
while (ch !=NULL)
{
tempWord++;
}
v.push_back(tempWord);
lineCount++;
}
cout <<"Line count: " << lineCount;
int total;
for (int i =0; i < v.size(); i++)
{
total = total + v[i];
}
cout << "Word count: " << total;
return 0;
}
WordCount.cpp:29: error: cannot convert ‘char*’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
WordCount.cpp:44: error: cannot convert ‘char*’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
Line 29: file = getline(line, streamsize(100000), str );
Line 44: file = getline(line, streamsize(100000), str );
Re: Need to print out number of words and lines in a file
Quote:
Originally Posted by
jedipenguin
Perhaps I've altered it since then, but right now it's not compiling.
Why are you using a char pointer for string data? Why isn't filename of type string?
That's the problem -- you're using a char*.
Code:
string filename;
cout << "Enter a file name.";
cin >> filename;
Again, all of your mistakes are easily worked out by just taking time and seeing what the problem is.
Regards,
Paul McKenzie
Re: Need to print out number of words and lines in a file
I'd already figured that one before you posted. However, I'm also getting two more errors:
What's wrong with this bit of code:
file = getline(line, streamsize(100000), str );
WordCount.cpp:29: error: cannot convert ‘char*’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
WordCount.cpp:44: error: cannot convert ‘char*’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
What's that supposed to mean?
Re: Need to print out number of words and lines in a file
Code:
char* line;
file = getline(line, streamsize(10000), str );
Where is the buffer that is supposed to contain the data going to come from?
A char* is just a pointer. It does nothing except point to a character. It doesn't have properties where it will magically create buffers for you.
http://www.cplusplus.com/reference/i...tream/getline/
Look at the example from the link above.
Regards,
Paul McKenzie
Re: Need to print out number of words and lines in a file
Still not making sense.
What's wrong?
Re: Need to print out number of words and lines in a file
Quote:
Originally Posted by
jedipenguin
Still not making sense.
What's wrong?
What did you get out of that example you saw at the link? Comparing it to your code, what is different about that example?
Honestly, you're not taking your time and look at the clear examples that I posted from that website. The examples show that getline() is a member of the stream class. Second, it shows how to use it. The "cin" is the stream, so what is your stream? Isn't it "file" in your example?
If you get this thing to compile, can you imagine what happens if you try to run it and it doesn't produce the results you want?
Regards,
Paul McKenzie
Re: Need to print out number of words and lines in a file
It runs forever.
I figured it out. I was having trouble trying to figure out what to do exactly with the syntax but I got it before you posted. However, it appears to be going into an infinite loop.
I added the line
file.close() and it's still going on forever.
Re: Need to print out number of words and lines in a file
Another reason I'm so angry is because if something goes wrong, it's likely I could only get a 60% at best, which I'm taking as a personal insult from the professor. And I feel that that is far too low a grade for all the time I'm spending, hence my anger and impatience.
I've spent all day on this. I could've been doing other assignments.
I've been on this for over 4 hours.
Re: Need to print out number of words and lines in a file
Quote:
Originally Posted by
jedipenguin
It runs forever.
I figured it out. I was having trouble trying to figure out what to do exactly with the syntax but I got it before you posted. However, it appears to be going into an infinite loop.
http://stackoverflow.com/questions/5...ines-from-file
You should also have had a small, simple, test program that just opens a file, and reads until the end. Then when you get that working, you concentrate on the other aspects of the program. Trying to get 10 things to work at the same time, and the basics are still not working is not the way to complete assignments.
Also, making small test programs makes asking questions here easier for you. You now know what doesn't work, and the question becomes more targeted, as opposed to "my program doesn't work and here is all of my code".
Regards,
Paul McKenzie