I am currently working on an email filter, and it will either get input from a file and then filter junk mail out, or get mail from cin. I have the file part working correctly, and I even got as far as to get basic re-direction and piping working.
The only problem I have is when the user decides not to use re-direction, or a file, and instead they decide to enter text directly in the Terminal.
I have this loop for looping through a re-directed or piped in file: -
//--Create a char[] to hold data since we are using cin
-
char line[128];
-
-
//--Keep reading until no more lines are found to read
-
while(cin.getline(line,128))
-
{
-
//--Place the line in a stringstream
-
stringstream ss(line);
-
-
//--Tokenize it
-
while(ss >> buffer)
-
{
-
tokens.push_back(buffer);
-
}
-
-
//--Check it to see if it is a From line
-
if(tokens[0] == "From")
-
{
-
for(int i = 0; i < emailAddresses.size(); i++)
-
{
-
if(emailAddresses[i] == tokens[1])
-
{
-
//--Print the current line to the .junk file
-
outputFileJ << line << "\n";
-
-
//--Set the flag
-
junkAddress = 1;
-
}
-
else
-
{
-
//--Print the current line to the .good file
-
outputFileG << line << "\n";
-
-
//--Clear the flag
-
junkAddress = 0;
-
}
-
}
-
}
-
else
-
{
-
if(junkAddress == 1)
-
{
-
//--We know this line isn't a From line
-
outputFileJ << line << "\n";
-
}
-
else
-
{
-
//--We know this line isn't a From line
-
outputFileG << line << "\n";
-
}
-
}
-
-
//--Clear the Vector, so we're only storing one line
-
tokens.clear();
-
}
As I mentioned before, this works fine for re-directed files. However, if a user decides to type directly into the terminal, the program goes into an infinite loop.
Is there some way I can get it to either read from a re-directed file, or from the terminal in the same block of code? I just want to read whatever the user types in into a vector and tokenize it so it can be processed.
If it helps any, emailAddresses is a vector that contains only junk email, and junkFlag determines where the current line is written to (0 = it's good, 1 = junk).
1 1404
EDIT: Just looking at my code a bit more and I came up with this: -
//--Declare a vector to hold junk email
-
vector<string> emailAddresses;
-
-
//--Declare a flag to determine what input we are using (0 = file, 1 = standard in)
-
inputflag = 0;
-
-
if(inputflag = 0)
-
{
-
//--For file
-
writeFiles(file, emailAddresses, outputFileJ, outputFileG);
-
}
-
else
-
{
-
//--For standard in
-
writeFiles(cin, emailAddresses, outputFileJ, outputFileG);
-
}
-
-
void writeFiles(istream& stream, vector<string> junkAddresses, ofstream& junkOutput, ofstream& goodOutput)
-
{
-
//--Create a vector to hold tokens
-
vector<string> tokens;
-
-
//--Create a string to hold the current line
-
std::string line = "";
-
-
//--Create a buffer to hold the current token
-
std::string buffer = "";
-
-
//--Create a flag for junk addresses
-
int junkAddress = 0;
-
-
//--Keep reading until no more lines are found to read
-
while(getline(stream,line))
-
{
-
//--If the user is done typing in the terminal, need a way to
-
// exit, or the loop will go on forever
-
if(line == "exit")
-
{
-
break;
-
}
-
-
//--Place the line in a stringstream
-
stringstream ss(line);
-
-
//--Tokenize it
-
while(ss >> buffer)
-
{
-
tokens.push_back(buffer);
-
}
-
-
//--Check it to see if it is a From line
-
if(tokens[0] == "From")
-
{
-
for(int i = 0; i < junkAddresses.size(); i++)
-
{
-
if(junkAddresses[i] == tokens[1])
-
{
-
//--Print the current line to the .junk file
-
junkOutput << line << "\n";
-
-
//--Set the flag
-
junkAddress = 1;
-
}
-
else
-
{
-
//--Print the current line to the .good file
-
goodOutput << line << "\n";
-
-
//--Clear the flag
-
junkAddress = 0;
-
}
-
}
-
}
-
else
-
{
-
if(junkAddress == 1)
-
{
-
//--We know this line isn't a From line
-
junkOutput << line << "\n";
-
}
-
else
-
{
-
//--We know this line isn't a From line
-
goodOutput << line << "\n";
-
}
-
}
-
-
//--Clear the Vector, so we're only storing one line
-
tokens.clear();
-
}
-
}
-
Now, assuming file is of type ifstream, inputFlag gets changed throughout the program, and that emailAddresses is filled at some point, could I use this as a general function to read in from either cin or a file?
Thing is, I'm not sure how to pass cin to writeFiles(). Can I pass it like I'm doing above, or is there some other way?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Grey Hangman |
last post by:
Hi.
I'm a veteran mainfamer, MQ'er, etc, with a little Java that I've picked
up here & there over the last few years. I'm planning on taking the Sun
Certified Business Component Developer exam...
|
by: Jolly Paily |
last post by:
NEED A DB2 DBA
>
> Project Description and Tasks : DB2 EE administration, unix shell
> scripts, backup, recovery, failover, problem solving, customer
> interaction. Also, nice to have skill in...
|
by: Martin Marcher |
last post by:
Hi,
I've read several questions and often the answer was
'C knows nothing about .'
So if C knows that little as some people say, what are the benefits, I
mean do other languages know more...
|
by: Sharon |
last post by:
is microsoft going to develop .Net for Unix?
or at lest CLR for Unix?
10x
|
by: niket patel |
last post by:
Hi
There is a unix server and and we access unix and perform its commond using
exceed or ftp through dos.
Now i want to create an application in Visual Studio C#.Net 2003 such that i
can create...
|
by: Xah Lee |
last post by:
Software Needs Philosophers
by Steve Yegge, 2006-04-15.
Software needs philosophers.
This thought has been nagging at me for a year now, and recently it's
been growing like a tumor. One...
|
by: Xah Lee |
last post by:
The Nature of the “Unix Philosophy”
Xah Lee, 2006-05
In the computing industry, especially among unix community, we often
hear that there's a “Unix Philosophy”. In this essay, i...
|
by: =?Utf-8?B?UHVjY2E=?= |
last post by:
I need to retrieve and open a file stored on a Unix server on a network and
opens the file as a delimited text file. I'm using VS2005, .net 2.0 and C#.
What's the best way to do this and if...
|
by: techquest |
last post by:
Hi,
I want to connect into oracle database and export the table data into a flat file using UNIX shell scripts. I cant use other GUI tools to do this, as the dataload will be in millions. hence if...
|
by: Angus |
last post by:
Hello
We have a lot of C++ code. And we need to now create a library which
can be used from C and C++. Given that we have a lot of C++ code
using classes how can we 'hide' the fact that it is...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shllpp 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |