File String Search

7

Click here to load reader

Transcript of File String Search

Page 1: File String Search

view source

print?001 //************************************

002 // NOTE: ASCII Table, '0' to '9' are represented as 48 to 57

003 //************************************004 // This program will search for a specific string (based on user input)

005 // in a file and then return all lines that include that string. 006 //************************************

007 #include <cstring>

008 #include <ctime>009 #include <ctype.h>

010 #include <fstream> // file I/O011 #include <iomanip> // format manipulation

012 #include <iostream>013 #include <limits>

014 #include <map>

015 #include <math.h>016 #include <sstream>

017 #include <stddef.h>018 #include <stdexcept>

019 #include <stdio.h>

020 #include <stdlib.h> // converter functions021 #include <string>

022 using std::istringstream;023 using std::ifstream;024 using std::string;025 using std::cout;026 using std::cin;027 using std::cerr;028 using std::map;029 using std::multimap;030 031 using namespace std;032 //************************************033 //In general, C++ strings are preferred over C strings.

034 //************************************

035 void CreateExampleTxtFile();036 void PrintExampleTxtFile();037 void SizeExampleTxtFile();038 int SearchExampleTxtFile();039 //************************************

040 int main(int argc, char* argv[])

http://www.dreamincode.net/forums/topic/109268-string-search-in-a-file/

1 of 7 1/20/2014 2:31 PM

Page 2: File String Search

041 {

042 int choice = 0;043 bool exitt = false;044 045 do046 {

047 system("CLS");

048 049 cout << endl << endl << endl;050 cout << " MENU" << endl;051 cout << " ************************" << endl << endl;052 cout << " 1 Create new file." << endl;053 cout << " 2 Print new file" << endl;054 cout << " 3 Size new file" << endl;055 cout << " 4 Search new file." << endl << endl;056 cout << " Enter 0 to exit" << endl << endl;057 058 //test validity of user input data

059 060 061 //Get customer input

062 063 cout << " Please type a number from 0 to 4 and then press enter ==> ";064 065 cin >> choice;066 067 068 switch(choice)

069 {070 //Call class member functions

071 072 case 0:073 074 break;

075 076 case 1:077 CreateExampleTxtFile();078 break;

079 080 case 2:081 PrintExampleTxtFile();

http://www.dreamincode.net/forums/topic/109268-string-search-in-a-file/

2 of 7 1/20/2014 2:31 PM

Page 3: File String Search

082 break;

083 084 case 3:085 SizeExampleTxtFile();086 break;

087 088 case 4:089 SearchExampleTxtFile();090 break;

091 092 default:

093 cout << endl << endl << endl;094 cout << " Please select again." << endl << endl;095 break;096 }

097 098 cout << endl << endl << endl;

099 cout << " Type the number 1 and press return to run this program again." <<endl << endl << endl;

100 cout << " Type any key and press return to exit ==> ";101 102 cin >> choice;103 104 if (choice == 1)105 exitt = false;

106 else107 exitt = true;

108 }while(exitt == false);

109 110 cout << endl << endl << endl;111 cout << " ";112 system("PAUSE");

113 114 return 0;115 }

116 //************************************

117 //This function creates a file named example.txt118 //and inserts six sentences into example.txt.

119 void CreateExampleTxtFile()120 {121 ofstream myfile;

122 myfile.open ("example.txt");

http://www.dreamincode.net/forums/topic/109268-string-search-in-a-file/

3 of 7 1/20/2014 2:31 PM

Page 4: File String Search

123 if (myfile.is_open())124 {125 myfile << "one two three four five\n";

126 myfile << "two three four five six\n";

127 myfile << "three four five six seven\n";128 myfile << "four five six seven eight\n";

129 myfile << "five six seven eight nine\n";130 myfile << "six seven eight nine ten\n";

131 myfile.close();

132 }

133 else cout << " Unable to open file";134 }

135 136 //************************************137 //This function reads a file named example.txt

138 //and prints contents to console.

139 void PrintExampleTxtFile()140 {141 string line;

142 143 ifstream myfile ("example.txt");

144 system("CLS");

145 if (myfile.is_open())146 {

147 while (! myfile.eof() )148 {

149 getline (myfile, line);150 cout << " " << line << endl;151 }152 myfile.close();

153 }

154 155 else cout << " Unable to open file";156 }

157 158 //************************************

159 //This function prints the size of the example.txt file,160 //on the console.

161 void SizeExampleTxtFile()162 {

163 long begin;164 long end;165 ifstream myfile ("example.txt");

http://www.dreamincode.net/forums/topic/109268-string-search-in-a-file/

4 of 7 1/20/2014 2:31 PM

Page 5: File String Search

166 begin = myfile.tellg();167 myfile.seekg (0, ios::end);

168 end = myfile.tellg();

169 myfile.close();170 system("CLS");

171 cout << endl << endl << endl;172 cout << " File size is: " << (end - begin) << " bytes.\n";173 }

174 175 //************************************

176 int SearchExampleTxtFile()177 {

178 const int SIZE = 200; // constant for size of character string179 char fileName[SIZE]; // to hold the file name180 ifstream inputFile; // input file

181 int counter = 0; // counter for counting number of stringoccurences

182 183 system("CLS");

184 185 //************************************

186 // Get user's file name

187 cout << endl << endl << endl;188 cout << " Enter the name of the file you want\n";189 cout << " to search and press return ==> ";190 cin >> fileName; //fileName is example.txt191 192 // Open user's file.193 inputFile.open(fileName);

194 if(!fileName)

195 {

196 cout << endl << endl << endl;197 cout << " Cannot open " << fileName << endl;198 cout << endl << endl << endl;199 cout << " ";200 system("pause");

201 }

202 203 //************************************204 //reads a file named example.txt searches for user defined string

205 //and prints results to console.206 multimap<string, int> words;

207 map<int, string> lines;

http://www.dreamincode.net/forums/topic/109268-string-search-in-a-file/

5 of 7 1/20/2014 2:31 PM

Page 6: File String Search

208 string str;209 ifstream input("example.txt");

210 if(input.fail())

211 {212 cerr<<"\n The file could not be opened.";

213 return -1;214 }

215 int i=1;216 while(getline(input,str))

217 {

218 istringstream in(str);219 string s;

220 while(in>>s)221 {

222 words.insert(make_pair(s,i));

223 }224 lines.insert(make_pair(i,str));

225 i++;226 }

227 string search;

228 cout << "\n Enter a word to search: ";229 cin >> search;230 231 cout << "\n The number of matches = " << words.count(search) << '\n';232 multimap < string,int>::iterator it1=words.lower_bound(search);233 multimap < string,int>::iterator it2=words.upper_bound(search);

234 while(it1 != it2)

235 {

236 int x=it1->second;237 map<int,string>::iterator iter = lines.find(x);

238 cout << '\n' << x << " ) " << iter->second << '\n';239 it1++;240 while(true)

241 {

242 if(it1 != it2 && it1->second == x)243 {

244 it1++;

245 246 }

247 else

248 {249 break;

250 }251 }

http://www.dreamincode.net/forums/topic/109268-string-search-in-a-file/

6 of 7 1/20/2014 2:31 PM

Page 7: File String Search

252 }

253 return 0;254 }

http://www.dreamincode.net/forums/topic/109268-string-search-in-a-file/

7 of 7 1/20/2014 2:31 PM