I'm trying to count same string/words from a text file in C++.
This is my text file
one two three two
test testing 123
1 2 3
This is my main program
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, const char** argv)
{
int counter = 0;
int ncounter = 0;
string str;
ifstream input(argv[1]);
while (getline(input, str))
{
if(str.find("two") != string::npos){counter++;}
if(str.find('\n') != string::npos){ncounter++;}
cout << str << endl; //To show the content of the file
}
cout << endl;
cout << "String Counter: " << counter << endl;
cout << "'\\n' Counter: " << ncounter << endl;
return 0;
}
I'm using the .find() function to find the string.
When I insert an non-existant word, it doesn't count.
When I insert the word "two", it counts, but only once.
How come it didn't count 2 times?
And for the carriage return (or return line; \n), it can't count any. Why is that?
Because the two twos are on the same line and you are searching the line only for one substring.
You can't find the '\n' because the getline function reads the line up to and without the '\n'.
Why not use a std::multiset to store the words? It would do the counting for you, and reading the file into it can be done in one line:
#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include <iterator>
int main(int argc, const char** argv)
{
// Open the file
std::ifstream input(argv[1]);
// Read all the words into a set
std::multiset<std::string> wordsList =
std::multiset<std::string>( std::istream_iterator<std::string>(input),
std::istream_iterator<std::string>());
// Iterate over every word
for(auto word = wordsList.begin(); word != wordsList.end(); word=wordsList.upper_bound(*word))
std::cout << *word << ": " << wordsList.count(*word) << std::endl;
// Done
system("pause");
return 0;
}
Note the last for part - word=wordsList.upper_bound(*word). Technically you can switch it to simply word++ (then actually it would be better to shorten it to simply for(auto word: wordList). It ensures each value from the set will only be output once.
It will also list the words themselves without you needing to do it like now inside your current while loop.
Your best bet is going to be to read each line, then tokenize along the white space so you can examine each word individually.
I suspect we're talking about a homework assignment here, so my best answer is to direct you to the c++ reference for std::strtok: http://en.cppreference.com/w/cpp/string/byte/strtok
Related
Idea:
I'm trying to create a program that searches for user-entered-word in a .txt file. Size of the word is not given. I want to find a way to dynamically store user's word to be able to compare it to the other words from file.
The entire program is huge so i only attach a part related to my queasions.
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
void vectorfill(vector<char>& newword) //filling char vector
{
char input;
scanf_s("%c", &input);
while (input != -1)
{
newword.push_back(input);
scanf_s("%c", &input);
}
}
int main (void)
{
vector<char> word;
printf("Enter a word: (-1 to finish)");
vectorfill(word);
}
Questions:
1) Is char vector a best idea in this case ?
2) (In case we're good with char vector)How to make compiller understand that user finished writing their word? Can we ask him to put (-1) at the end? Is there a better way to mark the end of the input?
1> No. Use a std::string
2> Yes. Use whitespace.
Example:
#include <iostream>
#include <string>
int main ()
{
std::string word;
std::cout << "Enter a word" << std::endl;
std::cin >> word;
// do something with word. For example,
std::cout << "You entered" << word << '\n';
}
As soon as the user types in at least one number, letter, or other non-whitespace character followed by a whitespace character a word will have been captured in word. If you have special requirements like this word can only contain letters (no numbers, bells, ASCII art characters, etc...) a simple loop with isalpha can sort that out in a few lines of code, but not as few as std::find_if and isalpha.
If the searching content is in txt file. Using std::vector<std::string> may be better. You can use the split char to split the words.
If the content is from user's keyboard input. you can also use std::string to store every word typed, and store it in std::vector<std::string>. Just like this:
std::string s;
std::vector<std::string> vec;
std::cout << "Please enter somestring" << std::endl;
while (cin >> s)
{
vec.push_back(s);
cout << "You have entered : " << s << endl;
}
I need help, I wrote the code, did the reverse thing but I can't get it written on another file.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream par2("C:/fajllat/f1.bin", ios::in);
string line;
for (int i = 1; !par2.eof() ; i++)
{
getline(par2, line);
if (i < 5 || i >14) continue;
line = string(line.rbegin(), line.rend());
}
par2.close();
ofstream mbrapsht ("C:/fajllat/f3.bin", ios::out);
mbrapsht << line;
mbrapsht.close();
cin.get();cin.get();
return 0;
}
When I check the files the f3.bin file is empty
You have the right idea. What you're missing is that if you want to write the reversed lines, you need to either write them inside the loop or store them for after. You are doing neither of these things.
Currently what happens is you overwrite line every loop. And whatever is left in that string is what you write afterwards. Turns out that for your case, that's an empty string.
Let's make minimal changes to what you have:
// (*) Open the output file before looping
ofstream mbrapsht("C:/fajllat/f3.bin", ios::out);
for (int i = 1; !par2.eof() ; i++)
{
getline(par2, line);
if (i < 5 || i > 14) continue;
line = string(line.rbegin(), line.rend());
// (*) output the line - you also probably want an end-of-line
mbrapsht << line << std::endl;
}
Now, it's okay-ish. But it does have a problem where if getline fails, your code still runs the loop body one more time. This happens if getline hits the end of file (or some other error state), which your loop doesn't pick up until the next iteration (or possibly never, if the error is not EOF).
So, a somewhat better choice might be:
for(int lineNo = 1; std::getline(par2, line); ++lineNo)
{
if (lineNo >= 5 && lineNo <= 14)
{
std::reverse(line.begin(), line.end()); // (*) requires <algorithm>
mbrapsht << line << std::endl;
}
}
Note that I also inverted your test condition and removed the continue. In general, I avoid continue and break in loops unless not using them results in code that is hard to follow or understand at a glance. It's a style/maintainability thing. Take it or leave it.
See this snippet . For line-by-line reversal, you can use getline() instead and reverse before pushing into vector<string>.
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
int main()
{
string str;
ifstream par2("D:\\MyFolder\\try.txt", ios::in);
if (par2.is_open())
{
stringstream strStream;
strStream << par2.rdbuf();
str = strStream.str();
cout << str << endl;
par2.close();
}
cout << "\n\nReversing ...\n\n";
std::reverse(str.begin(), str.end());
cout << str << endl;
ofstream mbrapsht("D:\\MyFolder\\try2.txt", ios::out);
mbrapsht << str;
mbrapsht.close();
return 0;
}
Output:
I am trying to count the characters in my program. Initially my variable "words" was a char and the file read just fine. When trying to determine the length of the variable, it wouldn't work with .length(). Can you explain how I can make my "words" variable as a string so that the words.length() executes correctly?
error on line words = readFile.get(); is:
no match for ‘operator!=’ in ‘words != -0x00000000000000001’
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <string>
#include <stdio.h>
#include <math.h>
using namespace std;
int main() {
//buff array to hold char words in the input text file
string words;
//char words;
//read file
ifstream readFile("TextFile1.txt");
//notify user if the file didn't transfer into the system
if (!readFile)
cout <<"I am sorry but we could not process your file."<<endl;
//read and output the file
while (readFile)
{
words = readFile.get();
if(words!= EOF)
cout <<words;
}
cout << "The size of the file is: " << words.length() << " bytes. \n";
return 0;
}
char c;
while (readFile.get(c))
{
words.insert(c);
}
Of course, if you were solely doing this to count the number of characters (and were intent on using std::istream::get) you'd probably be better off just doing this:
int NumChars = 0;
while (readFile.get())
{
NumChars++;
}
Oh, and by the way, you might want to close the file after you're done with it.
You should read some reference.. try cppreference.com and look for std::instream::get
I'm not sure what do you want, but if you wanna just count words, you can do something like this:
std::ifstream InFile(/*filename*/);
if(!InFile)
// file not found
std::string s;
int numWords = 0;
while(InFile >> s)
numWords++;
std::cout << numWords;
Or if you want to get to know how many characters are in file, change std::string s to char s and use std::ifstream::get instead:
std::ifstream InFile(/*filename*/);
if(!InFile)
// file not found
char s;
int numCharacters = 0;
while(InFile.get(s)) //this will read one character after another until EOF
numCharacters++;
std::cout << numCharacters;
The second approach is easier:
If file uses ASCII, numCharacters == fileSize;
Otherwise if it uses UNICODE, numCharacters == fileSize / 2;
get() returns an int, to do what you're doing, you must check that int before appending to "words" instead of checking words against EOF, e.g.:
...
//read and output the file
while (readFile)
{
const int w = readFile.get();
if (w!= EOF) {
words += w;
cout <<words;
}
}
...
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <string>
#include <iomanip>
#include <fstream>
#include <stdio.h>
using namespace std;
int main()
{
ifstream file;
string filename;
char character;
int letters[153] = {};
cout << "Enter text file name: ";
cin >> filename;
file.open(filename.c_str());
if (! file.is_open())
{
cout << "Error opening file. Check file name. Exiting program." << endl;
exit(0);
}
while (file.peek() != EOF)
{
file >> character;
if(!file.fail())
{
letters[static_cast<int>(character)]++;
}
}
for (int i = 0; i <= 153; i++)
{
if (letters[i] > 0)
{
cout << static_cast<char>(i) << " " << letters[i] << endl;
}
}
exit(0);
}
#endif
Hi everyone, my current code counts the frequency of each letter from a text file. However, it does not count the number of blank spaces. Is there a simple way to printout the number of blank spaces in a .txt file?
Also, how come when I'm trying to access a vector item, I run into a seg fault?
For example, if I use:
cout << " " + letters[i] << endl;, it displays a segfault. Any ideas?
Thank you so much.
By default, iostreams formatted input extraction operations (those using >>) skip past all whitespace characters to get to the first non-whitespace character. Perhaps surprisingly, this includes the extraction operator for char. In order to consider whitespace characters as characters to be processed as usual, you should alter use the noskipws manipulator before processing:
file << std::noskipws;
Don't forget to set it back on later:
file << std::skipws;
What if you're one of those crazy people who wants to make a function that leaves this aspect (or in even all aspects) of the stream state as it was before it exits? Naturally, C++ provides a discouragingly ugly way to achieve this:
std::ios_base::fmtflags old_fmt = file.flags();
file << std::noskipws;
... // Do your thang
file.flags(old_fmt);
I'm only posting this as an alternative way of doing what you're apparently trying. This uses the same lookup table approach you use in your code, but uses an istreambuf_iterator for slurping unformatted (and unfiltered) raw characters out of the stream buffer directly.
#include <iostream>
#include <fstream>
#include <iterator>
#include <climits>
int main(int argc, char *argv[])
{
if (argc < 2)
return EXIT_FAILURE;
std::ifstream inf(argv[1]);
std::istreambuf_iterator<char> it_inf(inf), it_eof;
unsigned int arr[1 << CHAR_BIT] = {};
std::for_each(it_inf, it_eof,
[&arr](char c){ ++arr[static_cast<unsigned int>(c)];});
for (int i=0;i<sizeof(arr)/sizeof(arr[0]);++i)
{
if (std::isprint(i) && arr[i])
std::cout << static_cast<char>(i) << ':' << arr[i] << std::endl;
}
return 0;
}
Executing this on the very source code file itself, (i.e. the code above) generates the following:
:124
#:4
&:3
':2
(:13
):13
*:1
+:4
,:4
/:1
0:3
1:2
2:1
::13
;:10
<:19
=:2
>:7
A:2
B:1
C:1
E:2
F:1
H:1
I:3
L:1
R:2
T:2
U:1
X:1
[:8
]:8
_:10
a:27
b:1
c:19
d:13
e:20
f:15
g:6
h:5
i:42
l:6
m:6
n:22
o:10
p:1
r:37
s:20
t:34
u:10
v:2
z:2
{:4
}:4
Just a different way to do it, but hopefully it is clear that usually the C++ standard library offers up elegant ways to do what you desire if you dig deep enough to find whats in there. Wishing you good luck.
I'm trying to write a program that keeps taking input from the user until the user enters "quit." Each time the user enters input, I want the program to print out the number of words the user has entered. So the following input on the user's part:
hello how are you
would yield the following output:
You entered 4 words.
However, I am having trouble writing the program so that it counts the number of words on just one line; it doesn't clear the number before going onto the next line. So, if it took input from the user three times, it would add up the total number of words on those three lines. For example, the following input:
how are you
i am good thank you
quit
would yield the following output:
You entered 9 words.
when I want it to output the number of words following each line the user enters (except quit), i.e.
>>how are you
<<You entered 3 words.
>>i am good thank you
<<You entered 5 words.
>>quit
Here's the relevant bit of my code:
char *input;
int inum;
int inputLoop()
{
char quit[] = "quit";
inum = 0; //counts number of words
while (strcmp(input, quit) != 0)
{
cin >> input;
inum++;
}
cout <<"You entered " <<inum <<" words." <<endl;
I'd rather not use something like a vector; whatever I use will need to be converted to a *char eventually because my global variable is a *char. (And my global variable is a *char because, depending on certain conditions, *input may be set to *argv[] from main.)
I've tried all sorts of things, but I just can't seem to get past the fact that strcmp(input, quit) compares one word of the input at a time to quit rather than comparing the entire input line to quit. HELP.
None of your requirements precludes the use of std::string and std::vector. I recommend you use them.
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
std::vector<std::string> words;
int inputLoop()
{
char quit[] = "quit";
total_words = 0;
std::string line;
// grab a line at a time
while(std::getline(std::cin, line) && line != quit) {
// clear the vector of words
words.clear();
// make a string stream to read words from that line
std::stringstream ss(line);
// grab all the words into a vector
std::string word;
while(ss >> word) {
words.push_back(word);
}
std::cout <<"You entered " <<words.size() <<" words." <<endl;
}
}
int main(int argc, char** argv) {
// get the data from argv
words = std::vector<std::string>(argv, argv + argc);
}
You should use getline() to get an entire line of input into some buffer. Then, process that buffer of input to count the number of words in it. Assuming you define each word to be a block of characters separated by a space. Myself, I am a fan of strtok() for breaking up a buffer.
An alternative approach, just for fun:
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
unsigned num = 0;
std::for_each(
std::istream_iterator<std::string>(std::cin),
std::istream_iterator<std::string>(),
[&num](const std::string& s)
{
if (s == "quit")
std::cin.setstate(std::ios::eofbit);
++num;
if (std::cin.peek() == '\n') {
std::cout << "You entered "
<< num
<< " word"
<< ((num == 1) ? "." : "s.")
<< '\n';
num = 0;
}
});
}
Doesn't waste resources by tokenizing a line into a vector :)
I would call distance
#include <string>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <sstream>
int main()
{
std::string line;
while(std::getline(std::cin, line) && line != "quit")
{
std::stringstream linestream(line);
std::cout << "You entered "
<< std::distance(std::istream_iterator<std::string>(linestream),
std::istream_iterator<std::string>())
<< " words\n";
}
}