473,326 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Need a little UNIX re-direction help

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:

Expand|Select|Wrap|Line Numbers
  1. //--Create a char[] to hold data since we are using cin
  2. char line[128];
  3.  
  4.             //--Keep reading until no more lines are found to read
  5.             while(cin.getline(line,128))
  6.             {
  7.                 //--Place the line in a stringstream
  8.                 stringstream ss(line);
  9.  
  10.                 //--Tokenize it
  11.                 while(ss >> buffer)
  12.                 {
  13.                     tokens.push_back(buffer);
  14.                 }
  15.  
  16.                 //--Check it to see if it is a From line
  17.                 if(tokens[0] == "From")
  18.                 {
  19.                     for(int i = 0; i < emailAddresses.size(); i++)
  20.                     {
  21.                         if(emailAddresses[i] == tokens[1])
  22.                         {
  23.                             //--Print the current line to the .junk file
  24.                             outputFileJ << line << "\n";
  25.  
  26.                             //--Set the flag
  27.                             junkAddress = 1;
  28.                         }
  29.                         else
  30.                         {
  31.                             //--Print the current line to the .good file
  32.                             outputFileG << line << "\n";
  33.  
  34.                             //--Clear the flag
  35.                             junkAddress = 0;
  36.                         }
  37.                     }
  38.                 }
  39.                 else
  40.                 {
  41.                     if(junkAddress == 1)
  42.                     {
  43.                         //--We know this line isn't a From line
  44.                         outputFileJ << line << "\n";
  45.                     }
  46.                     else
  47.                     {
  48.                         //--We know this line isn't a From line
  49.                         outputFileG << line << "\n";
  50.                     }
  51.                 }
  52.  
  53.                 //--Clear the Vector, so we're only storing one line
  54.                 tokens.clear();
  55.             }
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).
Feb 7 '08 #1
1 1404
EDIT: Just looking at my code a bit more and I came up with this:

Expand|Select|Wrap|Line Numbers
  1. //--Declare a vector to hold junk email
  2. vector<string> emailAddresses;
  3.  
  4. //--Declare a flag to determine what input we are using (0 = file, 1 = standard in)
  5. inputflag = 0;
  6.  
  7. if(inputflag = 0)
  8. {
  9.     //--For file
  10.     writeFiles(file, emailAddresses, outputFileJ, outputFileG);
  11. }
  12. else
  13. {
  14.     //--For standard in
  15.     writeFiles(cin, emailAddresses, outputFileJ, outputFileG);
  16. }
  17.  
  18. void writeFiles(istream& stream, vector<string> junkAddresses, ofstream& junkOutput, ofstream& goodOutput)
  19. {
  20.     //--Create a vector to hold tokens
  21.     vector<string> tokens;
  22.  
  23.     //--Create a string to hold the current line
  24.     std::string line = "";
  25.  
  26.     //--Create a buffer to hold the current token
  27.     std::string buffer = "";
  28.  
  29.     //--Create a flag for junk addresses
  30.     int junkAddress = 0;
  31.  
  32.     //--Keep reading until no more lines are found to read
  33.     while(getline(stream,line))
  34.     {
  35.         //--If the user is done typing in the terminal, need a way to
  36.         //  exit, or the loop will go on forever
  37.         if(line == "exit")
  38.         {
  39.             break;
  40.         }
  41.  
  42.         //--Place the line in a stringstream
  43.         stringstream ss(line);
  44.  
  45.         //--Tokenize it
  46.         while(ss >> buffer)
  47.         {
  48.           tokens.push_back(buffer);
  49.         }
  50.  
  51.         //--Check it to see if it is a From line
  52.         if(tokens[0] == "From")
  53.         {
  54.             for(int i = 0; i < junkAddresses.size(); i++)
  55.             {
  56.                 if(junkAddresses[i] == tokens[1])
  57.                 {
  58.                     //--Print the current line to the .junk file
  59.                     junkOutput << line << "\n";
  60.  
  61.                     //--Set the flag
  62.                     junkAddress = 1;
  63.                 }
  64.                 else
  65.                 {
  66.                     //--Print the current line to the .good file
  67.                     goodOutput << line << "\n";
  68.  
  69.                     //--Clear the flag
  70.                     junkAddress = 0;
  71.                 }
  72.             }
  73.         }
  74.         else
  75.         {
  76.             if(junkAddress == 1)
  77.             {
  78.                 //--We know this line isn't a From line
  79.                 junkOutput << line << "\n";
  80.             }
  81.             else
  82.             {
  83.                 //--We know this line isn't a From line
  84.                 goodOutput << line << "\n";
  85.             }
  86.         }
  87.  
  88.         //--Clear the Vector, so we're only storing one line
  89.         tokens.clear();
  90.     }
  91. }
  92.  
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?
Feb 7 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
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...
0
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...
38
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...
18
by: Sharon | last post by:
is microsoft going to develop .Net for Unix? or at lest CLR for Unix? 10x
0
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...
191
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...
22
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...
2
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...
3
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...
18
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...
0
isladogs
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...
1
isladogs
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...
0
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...
0
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...
1
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)...
1
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...
1
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....
0
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
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.