Why do I bring this up? Well, my program takes advantage of fstream, a coder's good friend or enemy, depending on your understanding of it.
In my next posting, I will show you the finer points of using fstream, but for now, here is the code for the dictionary shortening program:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string word;
ifstream infile;
ofstream outfile;
int count=0;
infile.open("fulldictionary.txt");
if(infile.fail())
{
cout<<"File could not be opened\n";
system("PAUSE");
return 0;
}
outfile.open("dictionary_short.txt");
if(outfile.fail())
{
cout<<"File could not be opened\n";
system("PAUSE");
return 0;
}
for(int i=0;!infile.fail();i++)
{
getline(infile,word);
if(word.length()>=3&&word.length()<=8)
{
count+=1;
cout<<count<<": "<<word<<endl;
outfile<<word<<endl;
}
}
infile.close();
outfile.close();
cout<<"There were "<<count<<" words saved in dictionary_short.txt\n";
system("PAUSE");
return 0;
}
The above code assumes that you have a file called "fulldictionary.txt" in the project folder and will automatically create the output file. There's no need to make that one.
Pretty Neat Code Kiebler.
ReplyDelete"Efficiency is the Key to Success in Programming."
What? No multi-threading?
ReplyDeleteJust kidding - very nice and simple.