| | |
search in text file
Thread Solved
![]() |
•
•
Join Date: Oct 2007
Posts: 52
Reputation:
Solved Threads: 0
Hi am trying to make a simple application that write and read from and to a text file the problem is that i want to be able to aerch in the text file for an specific line.
the text file is a list of words with a meaning each word, the application ask for what word to search and i want to cout<< the specific line containing that word..
could someone can help me ??
the text file is a list of words with a meaning each word, the application ask for what word to search and i want to cout<< the specific line containing that word..
could someone can help me ??
yes -- read each line sequentiall from star to finish until you get the line you want. use fstream's getline() to read an entire line then check only the first word to see if the line is the one you want.
Don't attempt to code all of that at one time. First get your program to open the file for reading. After that is code and compiles without error, add a loop using getline() to read each line. Get that working then code the next part.
Don't attempt to code all of that at one time. First get your program to open the file for reading. After that is code and compiles without error, add a loop using getline() to read each line. Get that working then code the next part.
Last edited by Ancient Dragon; Oct 9th, 2007 at 9:49 pm.
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: 52
Reputation:
Solved Threads: 0
ok this is what i got
this is the part i know, now what ?
thanks in advanced !!
C++ Syntax (Toggle Plain Text)
#include "stdafx.h" #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string line; char word; fstream dict("c://example.txt",ios::in); if (!dict.is_open()){ cout << "Unable to open file"; exit(1); } cout<<"Enter word to search \n"; cin>>word; while(getline(dict,line)) cout<<line<<"\n"; return 0; }
this is the part i know, now what ?
thanks in advanced !!
expand the while loop to extract the first word (hint: use string's find() method to locate the first space), then compare that with the string that you entered from the command line. If they are the same then you can stop that loop.
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: 52
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include "stdafx.h" #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string line; char* word=""; size_t i; fstream dict("c://example.txt",ios::in); if (!dict.is_open()){ cout << "Unable to open file \n"; exit(1); } cout<<"Enter word to search \n"; cin>>word; while(getline(dict,line)) i = line.find(word); if (line.compare(word) != 0){ cout << "result " <<line << endl; }else cout<<"Not in list"<<"\n"; return 0; }
I have the idea of what you are saying but dont know how to put it in code
A little off. First you need to put braces around that entire while statement. Next, the find mentiond returns string::npos if the word is not found. You need to add a check to see if the find method worked or not.
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: 52
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include "stdafx.h" #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string line; char* word=""; size_t i; fstream dict("c://example.txt",ios::in); if (!dict.is_open()){ cout << "Unable to open file \n"; exit(1); } cout<<"Enter word to search \n"; cin>>word; { while(getline(dict,line)) //read line by line i = line.find(word); //try to find the var word in the string line an assign it to i if (i!=string::npos) // dont exactly know what it does cout<<i<<" not found"; else line.compare(word) != 0; //compare the string line to the var word cout << "result " <<line << endl; exit (1); } return 0; }
I know youre trying to make me understand but am a little lost here really !!
i put comments around the lines to see if i understand what each does correctly , please correct me if am wrong..
thanks a lot
Why not try a trivial example first without reading a text file. When you are confident with how it works try it with a text file.
Questions (hints)
Are you getting your logic muddled up? Do you even need the line.compare(word)?
http://www.cplusplus.com/reference/s...ring/find.html
Questions (hints)
Are you getting your logic muddled up? Do you even need the line.compare(word)?
http://www.cplusplus.com/reference/s...ring/find.html
Last edited by iamthwee; Oct 10th, 2007 at 2:18 pm.
*Voted best profile in the world*
•
•
•
•
C++ Syntax (Toggle Plain Text)
#include "stdafx.h" #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string line; char* word=""; size_t i; fstream dict("c://example.txt",ios::in); if (!dict.is_open()){ cout << "Unable to open file \n"; exit(1); } cout<<"Enter word to search \n"; cin>>word; { while(getline(dict,line)) //read line by line i = line.find(word); //try to find the var word in the string line an assign it to i if (i!=string::npos) // dont exactly know what it does cout<<i<<" not found"; else line.compare(word) != 0; //compare the string line to the var word cout << "result " <<line << endl; exit (1); } return 0; }
I know youre trying to make me understand but am a little lost here really !!
i put comments around the lines to see if i understand what each does correctly , please correct me if am wrong..
thanks a lot
C++ Syntax (Toggle Plain Text)
while(getline(dict,line)) //read line by line { // start of while // your code goes here } // end of while
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
- open search result in text file (ASP)
- search text in a doc file (VB.NET)
- Search string in a text file (C)
- How to save all search engine results urls in a text file (C)
- Binary Search on a text file (Java)
- write to text file using fprintf() (C)
- New User (C)
- Help with delphi project (Pascal and Delphi)
Other Threads in the C++ Forum
- Previous Thread: Questions re: 2D Array w/bubble sort
- Next Thread: how can i get dual interface of mouse and keyboard?
| Thread Tools | Search this Thread |
api application array based binary bitmap c# c++ c/c++ char class classes code coding compile compression console conversion count cpm delayload delete deploy deque desktop developer dialog directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer introductory java lib linkedlist linkednodes linker loop looping loops map math matrix memory multiple news node numbertoword output pointer problem program programming project python random read recursion reference rpg security sorting string strings temperature template test text text-file tree url variable vector video whyisthiscodecausingsegmentationfault win32 windows winsock wordfrequency wxwidgets






