The idea is to take a file and print out the amount of words in the file. Then prompt the user to enter a word, the program will then count how many times that word is iterated. However I am having trouble with being able to pick out the chosen word from the file, no matter what it still returns 0.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
fstream infile;
int i = 0;
string word_counter;
string file_name;
bool opened = false;
while (opened == false){
cout << "Enter the name of the file to read: ";
cin >> file_name;
infile.open(file_name, fstream::in);
opened = true;
if (!infile.is_open()) {
cout << "ERROR: CANNOT OPEN INPUT FILE" << endl;
cout << endl;
opened = false;
}
}
while (!infile.eof()){
infile >> word_counter;
cout << word_counter << endl;
i++;
}
cout << "Read in " << i << " words\n";
bool done = false;
while (!done){
string word;
string quit;
int x = 0;
cout << "Enter a word to count how many times it occurs: ";
cin >> word;
while (!infile.eof()){
infile << word_counter;
if (word_counter == word){
x++;
}
}
cout << "The word \"" << word << "\" occurs " << x << " times" << endl;
cout << "Press any key to continue, or press Q to quit: ";
cin >> quit;
if (quit == "q" || quit == "Q"){
done = true;
}
}
infile.close();
return 0;
}
You forgot to rewind the file.
Add the following lines
infile.clear(); // Clear the EOF flag
infile.seekg(0); // rewind
right after
cin >> word;
Also, you are using << instead of >> in the line
infile << word_counter;
Since you are not reading anything from the file, the enclosing while block stays in an infinite loop. Change that line to:
infile >> word_counter;
Related
I'm practicing ifstream usage. I want the user to enter the file they want to read, in this example num1.txt specifically. I want the console to read one letter from num1.txt and output it on its own line.
I've ran the code below, and after entering "num1.txt" into the console, I get nothing back. I've tried moving around cout << num << endl; to the inner do statement, but it ends up repeating the number 10 an infinite amount.
What am I doing wrong here?
Contents in num1.txt:
2 4 6 8 10
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string fileName, cont;
ifstream inputFile;
do {
int num = 0;
int total = 0;
cout << "Please enter the file name: ";
cin >> fileName;
inputFile.open(fileName);
if (inputFile.is_open()) {
do {
inputFile >> num;
total += num;
}
while(num > 0);
if (total != 0) {
cout << num << endl;
cout << "Total is: " << total << endl;
}
}
else {
cout << "Failed to open file." << endl;
}
inputFile.close();
cout << "Do you want to continue processing files? (yes or no): " << endl;
cin >> cont;
}
while (cont == "yes");
}
Your inner do loop is not correctly validating that operator>> is actually successful before using num. It should be looking at the stream's error state after each read. The easiest way to do that is to change your do loop into a while loop that uses the result of the read as its loop condition, eg:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string fileName, cont;
ifstream inputFile;
do {
cout << "Please enter the file name: ";
cin >> fileName;
inputFile.open(fileName);
if (inputFile.is_open()) {
int num = 0;
int total = 0;
while (inputFile >> num) {
total += num;
}
inputFile.close();
cout << "Total is: " << total << endl;
}
else {
cout << "Failed to open file." << endl;
}
cout << "Do you want to continue processing files? (yes or no): " << endl;
}
while ((cin >> cont) && (cont == "yes"));
return 0;
}
I'm new to C++, and I need to find the one word inputted (searchWord) within the text file "input.txt".
I have it now where it will output every word in the file, but I need it to just output the inputted string and find it.
#include<iomanip>
#include<fstream>
#include<string>
using namespace std;
int main(){
// Variable Declaration
string fileName;
string searchWord;
int exitValue = 0;
// Name the file to open
ifstream inputFile;
// Prompt the user to enter the file name
cout << "Enter the name of the file: ";
cin >> fileName;
// Logic to determine if the file name equals "input.txt"
if (fileName == "input.txt"){ // if user input = input.txt
inputFile.open("input.txt"); // opening input.txt
}
else{
cout << "\nCould not open '" << fileName << "'." << endl;
exit(0);
}
// Prompt the user for a word to search for in the file
cout << "Enter a value to search for: ";
cin >> searchWord;
// Logic to determine if the inputted string is in the file or not
while (inputFile >> searchWord){
cout << "\n'" << searchWord << "' was found in '"
<< fileName << "'." << endl;
}
inputFile.close();
return 0;
}
Do it just a bit different:
// Logic to determine if the inputted string is in the file or not
string inputWord;
while (inputFile >> inputWord){ // Do not overwrite the given searchWord
if(inputWord == searchWord ) { // Check if the input read equals searchWord
cout << "\n'" << searchWord << "' was found in '"
<< fileName << "'." << endl;
break; // End the loop
}
}
This worked for me:
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
// Returns the index of the value you are looking for.
// If the value isn't found, it returns -1.
int searchFile(string filename, string value) {
FILE *fp;
int c, i = 0;
string contents = "";
fp = fopen(filename.c_str(), "r");
while (true) {
c = fgetc(fp);
contents += c;
if (feof(fp)) {
break;
}
i++;
if (i >= value.length() && contents.substr(i - value.length(), i) == value) {
return i;
}
}
fclose(fp);
return -1; // value not found in file
}
int main() {
string fileName;
string value;
cout << "Enter file name: ";
cin >> fileName;
cout << endl << "Enter search value: ";
cin >> value;
int index = searchFile(fileName.c_str(), value.c_str());
if (index == -1) cout << "Not found";
else cout << "Found at index " << index << endl;
cin.get();
cin.get();
return 0;
}
my assignment is to take a string from a text file and count the number of words in it. I've gotten that far but now we have to be able to take a certain number and display that number word to the console. Say my string is "Hello World" if I enter '2' it should give me the result "World". I'm not really sure how my function should look for this. This is my code so far.
void getFileInfo(ifstream &inFile);
string words(ifstream &inFile);
int numOfWords(ifstream& inFile);
int main() {
ifstream inFile;
string sentence, fileName;
int numCount, word;
getFileInfo(inFile);
numCount = numOfWords(inFile);
inFile.clear(); // resets file pointer from the beginning
inFile.seekg( 0 );
sentence = words(inFile);
cout << sentence << ": has " << numCount << " words in it" << endl;
cout << "Enter a number to extract a word: ";
cin >> word;
}
void getFileInfo(ifstream &inFile){
string fileName;
do{
cout << "Please enter the filename: " << endl;
cin >> fileName;
inFile.open(fileName.c_str());
if(!inFile){
cout << "Invalid try again" << endl;
}
}while(!inFile);
}
string words(ifstream &inFile){
string words, theWords;
getline(inFile, words);
cout << words;
return theWords;
}
int numOfWords(ifstream& inFile){
string fileName, words, str;
int numCount =0;
while(inFile >> words){
++numCount;
}
return numCount;
}
Any suggestions?
Thanks in advance
I would suggest slightly different code for your task. First, write some simple helper functions:
// Clear error flags (EOF, for example) and reset stream to the beginning.
void resetStream(ifstream& stream) {
stream.clear();
stream.seekg(0, ios_base::beg);
}
// Count the words in text file stream.
int getWordsCount(ifstream& stream) {
int count = 0;
while (stream) {
string tmp;
stream >> tmp;
if (!tmp.empty()) ++count;
}
resetStream(stream);
return count;
}
// Read word by specific number.
string getWordByNumber(int number, ifstream& stream) {
string word;
while (number--)
stream >> word;
resetStream(stream);
return word;
}
Now you can easily get the number of words in a file and display a specific word by its number. For example:
int main() {
string fileName;
cout << "Enter the file name: \n";
cin >> fileName;
ifstream stream(fileName);
if (!stream)
cout << "Failed to open file!" << endl;
else {
int totalCount = getWordsCount(stream);
int currentCount = 0;
cout << "Total words count: " << totalCount << "\n\n";
do {
cout << "Enter the word number (enter '0' to finish): ";
cin >> currentCount;
if (currentCount == 0) break;
else if (currentCount > totalCount)
cout << "Invalid value!\n";
else {
string wordByNumber = getWordByNumber(currentCount, stream);
cout << "Word by number: " << "'" << wordByNumber << "'\n";
}
cout << "\n";
}
while (true);
}
return 0;
}
Warning: This code is not very efficient and I have not tested it much. If you have any problems, be sure to write a comment.
I need this program to show the count of words from a file (text document). However, not reseting counter of words. its supposed to be 7 but it is displaying 62 words. I was told to close the file from the second last while and create a new while (which is the last one) and open the file again. I've working on this for a while. However, after making the changes it doesn't display the file anymore. the only thing display in the screen it s a th and account of 2 words.
Thanks for your help
#include<iostream>
#include<fstream>//step#1
#include<string>
using namespace std;
int main()
{
string word,fileName;
char character;
int charcounter = 0, wordcounter = 0;
ifstream inData;// incoming file stream variable
cout << " Enter filename or type quit to exit: ";
cin >> fileName;
//loop to allow for multiple files data reads
while (fileName != "quit")
{
inData.open(fileName.c_str());//open file and bind file to ifstream variable
//loop for file not found validation
while (!inData)//filestream is in fail state due to no file
{
inData.clear();//clear the fail state
cout <<"File not found. Enter the correct filename: ";
cin >> fileName;
inData.open(fileName.c_str());
}
inData >> character;//extract a single character from the file
cout << "\n*****************************\n";
while (inData)
{
cout << character;
inData.get(character);
charcounter++;
}
cout << "\n******************************\n";
cout << fileName << " has " << charcounter << " words." << endl;
inData.close();//close the ifstream conection to the data file
while (inData)
{
cout << word;
wordcounter++;
inData.open(fileName.c_str());
}
charcounter = 0; //reset character count
wordcounter = 0;
//port for next file or exit
cout << "Enter a filename or type quit to exit: ";
cin >> fileName;
}
return 0;
}
Do you want this?
#include<iostream>
#include<fstream>//step#1
#include<string>
using namespace std;
int main() {
string word,fileName;
char character;
int charcounter = 0, wordcounter = 0;
ifstream inData;// incoming file stream variable
cout << " Enter filename or type quit to exit: ";
cin >> fileName;
//loop to allow for multiple files data reads
while (fileName != "quit")
{
inData.open(fileName.c_str());//open file and bind file to ifstream variable
//loop for file not found validation
while (!inData)//filestream is in fail state due to no file
{
inData.clear();//clear the fail state
cout <<"File not found. Enter the correct filename: ";
cin >> fileName;
inData.open(fileName.c_str());
}
cout << "\n*****************************\n";
while (inData)
{
inData >> character;//extract a single character from the file
cout << character;
charcounter++;
}
cout << "\n******************************\n";
cout << fileName << " has " << charcounter << " chars." << endl;
inData.close();//close the ifstream conection to the data file
inData.open(fileName.c_str());
while (inData)
{
inData >> word;
cout << word;
wordcounter++;
}
cout << "\n******************************\n";
cout << fileName << " has " << wordcounter << " words." << endl;
inData.close();//close the ifstream conection to the data file
charcounter = 0; //reset character count
wordcounter = 0;
//port for next file or exit
cout << "Enter a filename or type quit to exit: ";
cin >> fileName;
}
return 0;
}
I am a beginning c++ student and am attempting to write a program that takes a word in a file and indexes it, listing each word only once and displaying the line numbers of every time that word appears. I have tried using a map but i found it impossible to get the line numbers for the words. Instead, I am using a vector of structs that has an integer vector and a string for each word. I am trying to read each word, place it into a stringstream, then output it into the string in the struct. Then I take the line number and push_back it into the vector in the struct. then I save everything to the vector of the struct and try to print out each word associated with the line number vector. I am getting nowhere and would like some help. Itd be much appreciated! Here is my source code:
#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <sstream>
#include <vector>
using namespace std;
struct words {
vector<int> lineNumbers;
string word;
};
int main() {
ifstream inFile, testStream;
ofstream outFile;
string temp, choice, inFileName, outFileName, word, trash, word2, tempString;
int idx = 0, count = 0, idxTwo = 0;
bool outputOpened = false;
//map <string,int> wordList;
/map <string,int>::iterator wordIt;
stringstream myStream(ios_base::in| ios_base::out);
vector<words> myIndex;
words data;
for (;;) {
cout << "Options: "<< endl << "1. Index" << endl << "2. Quit" << endl
<< "Please enter an option: ";
getline(cin, temp);
//cin >> temp;
//cin.ignore(8192, '\n');
choice.resize(temp.length());
transform(temp.begin(), temp.end(), choice.begin(), ::toupper);
if (choice.compare("INDEX") == 0 || choice.compare("1") == 0) {
do {
inFileName.clear();
cout << "Index Program" << endl
<< "==============" << endl << endl;
cout << "Input file name: ";
getline(cin, inFileName);
inFile.open(inFileName.c_str());
if(inFile.fail()) {
cout << "Can't open file" << endl;
if(inFile.bad()) {
cout << "Bad" << endl;
}
inFile.clear();
}
}
while (!inFile.is_open());
do {
cout << "Output file name: ";
getline( cin, outFileName);
testStream.clear();
testStream.open(outFileName.c_str());
if(testStream.good()) {
cout << "That file already exists, try again" << endl;
testStream.clear();
testStream.close();
}
else {
testStream.clear();
testStream.close();
outFile.open(outFileName.c_str());
if (outFile.good()) {
outputOpened = true;
}
}
}
while (!outputOpened);
while (getline(inFile, trash)){
count++;
myStream << trash;
//myStream >> tempString;
while(myStream >> data.word) {
data.lineNumbers.push_back(count);
myIndex.push_back(data);
}
}
for (idx = 0; idx < myIndex.size(); idx++) {
outFile << "Word: "<< " "<< myIndex[idx].word << ", ";
for (idxTwo = 0; idxTwo < myIndex[idx].lineNumbers.size(); idxTwo++) {
outFile << "Found on lines " << " " << myIndex[idx].lineNumbers[idxTwo];
}
}
inFile.close();
outFile.close();
}
else if (choice.compare("QUIT") == 0 || choice.compare("2") == 0) {
return 0;
}
else {
cout << temp << " is an unrecognized option, please try again" << endl;
}
}
return 0;
}
Your problem seems to be that you are always appending each word to the vector<words>.
What you probably want, is a map<string, vector<int>>. Use .insert to insert words in to the set, if it already exists then just append the current line number to the value.
For this part of your program:
while (getline(inFile, trash)){
count++;
myStream << trash;
//myStream >> tempString;
while(myStream >> data.word) {
// 2.
// data.lineNumbers.clear();
data.lineNumbers.push_back(count);
myIndex.push_back(data);
}
// 1. add:
// myStream.clear();
}
You are using a global myStream. But the state of this myStream will be in a bad state after processing each line, as it ran out of data. The state mush be cleared for it to work again.
The data is also global, which make the line number for every word to be saved in data.lineNumber, not only the line number of the current word. You may want to clear it at each run.
Here are some issues:
Add myStream.clear(); after the while(myStream >> data.word) loop.
Add "\n" to the end of the following line to print on a new line:
outFile << "Found on lines " << " " << myIndex[idx].lineNumbers[idxTwo]<<"\n";
Also, the following nested loop will not give you correct results. It will grow with line numbers and keep printing "Found on lines":
for (idx = 0; idx < myIndex.size(); idx++) {
outFile << "Word: "<< " "<< myIndex[idx].word << ", ";
for (idxTwo = 0; idxTwo < myIndex[idx].lineNumbers.size(); idxTwo++) {
outFile << "Found on lines " << " " << myIndex[idx].lineNumbers[idxTwo]<<"\n";
}