| | |
Find word in file!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2006
Posts: 43
Reputation:
Solved Threads: 0
Hi everyone,
Im trying to solve this question:
write a C++ program to find a given word in a file. It should display all the line numbers where the word occurs.
this is the code that i did so far, the problem is it wont display all the right line number, it only displays "1"
I hope that you guys can help me fix it up.
Thanks in advance,
itchap
Im trying to solve this question:
write a C++ program to find a given word in a file. It should display all the line numbers where the word occurs.
this is the code that i did so far, the problem is it wont display all the right line number, it only displays "1"
I hope that you guys can help me fix it up. C++ Syntax (Toggle Plain Text)
#include <iostream> #include <conio.h> #include <fstream> using namespace std; int main() { string s1; ifstream in ("file.txt"); int lines = 0; cout << "Enter your search string: " <<endl; cin >> s1; in.seekg(0,ios::beg); lines ++; cout<<"The string occured at lines: " << lines<<endl; in.close(); getch(); return 0; }
Thanks in advance,
itchap
•
•
Join Date: Dec 2006
Posts: 43
Reputation:
Solved Threads: 0
You should ask this question in a new thread, so others dont get confused on which of us to help.
•
•
•
•
Originally Posted by itchap
in.seekg(0,ios::beg);
lines ++;
so something like:
C++ Syntax (Toggle Plain Text)
While (not the end of the file) { look for searchstring lines++; if string is found { cout << "found at line" << lines << endl } }
regards Niek
Last edited by niek_e; Dec 8th, 2006 at 8:18 am.
•
•
Join Date: Dec 2006
Posts: 43
Reputation:
Solved Threads: 0
•
•
•
•
You should put this in a loop, now it will find 1 occurence and continue with the rest of your code.
so something like:
C++ Syntax (Toggle Plain Text)
While (not the end of the file) { look for searchstring lines++; if string is found { cout << "found at line" << lines << endl } }
regards Niek
Thanks Niek!!!!!!!!!!!!!!!!!!!!
![]() |
Similar Threads
- Find a word in a file (Python)
- find strings in file.txt (C++)
- Scan text file to find all words of 4 characters or less (Shell Scripting)
- Searching a file for a string (C++)
- about:blank hijack (can't find any .dll-file) (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Need Help!
- Next Thread: Inheritance Questions
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






