| | |
Searching for words in a file
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2007
Posts: 12
Reputation:
Solved Threads: 0
Hi,
I'm a beginner, so please forgive my inadequate knowledge.
I'm am trying to write a program that searches one file for specific words and their replacements found in another file. As of yet, all I have is a program that opens my files and counts the words in them. I think I need to put the words in a string array in order to search through them, but I don't know how to do this. If someone could help get me started on this part, I can play around with the rest of the program and hopefully have some luck.
Thanks
I'm a beginner, so please forgive my inadequate knowledge.
I'm am trying to write a program that searches one file for specific words and their replacements found in another file. As of yet, all I have is a program that opens my files and counts the words in them. I think I need to put the words in a string array in order to search through them, but I don't know how to do this. If someone could help get me started on this part, I can play around with the rest of the program and hopefully have some luck.
Thanks
show us the code you have written so far. Do you know how to use vector class? That is the easiest array to use. You can make a vector of string objects
C++ Syntax (Toggle Plain Text)
std::vector<std::string> theArray;
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Oct 2007
Posts: 12
Reputation:
Solved Threads: 0
This is what I have so far, like I said, basically all it does is open the files and counts the words.
I am not familiar with the vector class string, but if you could elaborate on it a little I may be able to figure it out.
Thanks
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <fstream> using namespace std; int main () { ifstream potterfile, keywordfile; ofstream corrected; string word, text, file[300]; int count; count = 0; //Open Potter text file, check if open potterfile.open("potter.txt"); if (!potterfile.is_open()){ cout << "Error, could not open the file" << endl; return -1; } //Count words in Potter text file potterfile>>word; while (!potterfile.eof()){ count = count + 1; potterfile >> word; } cout <<"The number of words in the file is:" << count << endl; //Open Keyword file check if open keywordfile.open("keyword.txt"); if (!keywordfile.is_open()){ cout << "Error, could not open the file" << endl; return -1; } return 0; }
Thanks
Last edited by Ancient Dragon; Oct 4th, 2007 at 8:21 pm. Reason: add code tags
•
•
•
•
I am not familiar with the vector class string, but if you could elaborate on it a little I may be able to figure it out.
Thanks
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Replace words in a file (Python)
- Reading specific line in a file. / Searching (C++)
- Reading pascal file and searching for a particular word in that file (Pascal and Delphi)
- Scan text file to find all words of 4 characters or less (Shell Scripting)
- Searching a file for a string (C++)
- getting words from .txt file (C++)
Other Threads in the C++ Forum
- Previous Thread: error C2664
- Next Thread: urgent help postfix evaluation
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow display dll encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream input int integer java lazy lib linux loop looping loops map math matrix memory multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return string strings struct studio system template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






