c++ - Is there a way to read in data after a specified word is encountered in a file? -
The program I am creating will be read in the data from a text file, which contains a complete group of addresses and zip codes
My question is: whenever the file reads in "zip:" (if (text == "zip:")
, the program should print that token which after It comes in (specifications ask for token oriented input) , meaning zip code numbers.
Is there any such work Which will print only the zip code and any other lesson that comes after this? For long term please want to give as much detail as possible to the program. Please tell me if there is any other information then please join me. I am not looking for anyone to fulfill me. The program is just something that guidance on specific problem would be greatly appreciated.
# include & lt; iostream & gt; # evening L'or & lt; fstream & gt; # include & lt; String & gt; The name used for std; int main () (String text; Ifstream inFile; InFile.open ("zips"); While (! InFile.fail ()) {inFile & gt; & Gt; Text; If (text == "zip:") {}} inFile.close (); Return 0;}
Input is the file through which it is being stopped, the user does not enter any input. My desired output is the top ten most zip code. Ex:
zip: frequency: 11204 39 11234 33 22098 27 .... etc.
Here is a sample of what happens to the file
& Lt; 8975.37428190 @ 62997216886.XmT.srvr @ n325.xnot.com> CC: Visas addr: 488 Sikada Avenue = 4 = Z. City: Edmonton Zip: T5T4M4 $ 20.00 & lt; 833.3374281 9 0 @ 2997439800.XmT.srvr @ n324.xnot.com> CC: Visa addr: 48030 Nevada Boulevard = 4 = Z. City: Montecito Zip: 95,041 $ 15.00 & lt; 8354.374281 9 @ 63001226169.XmT.srvr @ n326.xnot.com> CC: Visa addr: 493 Park meadra drive = 4 = Z. City: Alamo Zip: 94521 $ 10.00 & lt; 8857.374281 9 @ 63001517062.XmT.srvr @ n326.xnot.com> cc: MasterCard Edit: 893 Moraga Avenue = 4 = Z City: San Bruno Zip: 94012 $ 15.00
By assuming your input file you will always be formatted like a post, which means that there will always be a value for zip (I have a corner case Did not check), should do this:
#include & lt; Iostream & gt; #include & lt; String & gt; # Include & lt; Fstream & gt; # Include & lt; Map & gt; using namespace std; Int main () {ifstream inFile; InFile.open ("test"); String text; Map & lt; String, int & gt; Frequencies; While (! InFile.fail ()) {inFile & gt; & Gt; Text; If (text == "zip:" & amp; inFile.fail ()) {string zip; InFile & gt; & Gt; Zip; If (Frequency.Find (zip) == Frequency.and ()) frequencies [zip] = 1; Other frequencies [ZIP] ++; }} Map & lt; String, int & gt; :: Iterator = frequencies. Bbi (); While (this! = Frequencies.and ()) {cout & lt; & Lt; (* this). First & lt; & Lt; ":" & Lt; & Lt; (* This) .second & lt; & Lt; Endl; This ++; } Return 0; }
It ran on your example file with 1 duplicate and this is the output:
94012: 1 94521: 1 95,041: 2 T5T4M4: 1
Formatting and sorting is missing though. For example, in a container support type such as set
or vector
can be implemented by inserting values from the map.
Take a look at these answers to see how this can be done:
vector:
Set:
Comments
Post a Comment