PDA

View Full Version : Repeating file ofstream and file creation


TheFarSide
11-06-2002, 10:09 AM
Since I've been taking classes for a whole 5 weeks now with C++, I've been designated to create an application here for work (ugh).

Basically, as an email hits our mail server, Declude software checks it for a virus, and also assigns a weight to the email, depending on how much it thinks it's a spam email. Anything above a weight of 20 gets set to the side to be hand pushed or deleted by us, and repeat offenders get put on the mail server blacklist.

This gets tedious.

So this program is basically invoked on a command line (in an NT environment), with variables being passed to it as command line arguments.

(EDIT: an example of how this will be called -
c:\program.exe 50 chansen@continentaloffice.com )


Hopefully this paste doesn't get mangled too much. This is what I have so far, which does most of what I need it to, with the exception of two things.

1. If blacklist.txt is not there, it needs to be able to create it.
2. This program gets run hundreds of times a day, and each time needs to seek the end of blacklist.txt and start the output there.

Neither of these things are addressed in my text, nor have I found anything in my limited net searching.


/*
*****************************************************
* Author- : Cameron Hansen
*
* Program- : Taking two variables from a command line
* : argument for an email blacklist. The
* : first variable is the weight sent from
* : the scanning software. If the weight is
* : higher than 20, add the email (second
* : variable) to the text file. The command
* : line syntax is:
* : "c:\program.exe weight email_address"
*
*
* Created : 11/05/02
*
* Modified : 11/06/02 - Altered the array conversion
* : for argv[1] to accept up to 3 digits
* : instead of two.
*
****************************************************
*/

#include <iostream>
#include <string> // Eventually for working with argv[2]
#include <fstream> // For file I/O

using std::cin;
using std::cout;
using std::endl;

// The total number of arguments (counting the program name)
// is stored into argc, while each argument is stored in the
// array, argv.

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

int i;
int j;
int k;
int m;

// Check to see if the correct number of variables are
// being sent. If not, end the program.

if(argc != 3)
{
return 0;
}
else
{
// Check to see if it's a 3 digit number
if (argv[1][2]==0)
{
// If not, check to see if it's a 2 digit number
if (argv[1][1]==0)
{
// If not, convert to an int from acsii
i = argv[1][0] - 48;
j = 0;
k = i;
}
// If it's a 2 digit number, convert it to an int
else
{
j = argv[1][0] - 48;
m = argv[1][1] - 48;
k = (j * 10) + m;
}
}
// If it's a 3 digit number, conver it to an int
else
{
i = argv[1][0] - 48;
j = argv[1][1] - 48;
m = argv[1][2] - 48;
k = (i * 100) + (j * 10) + m;
}
// Open blacklist.txt
ofstream BlackList;
BlackList.open("blacklist.txt");

// If the email weight is higher than 20, put them on the blacklist
if (k > 20)
{
BlackList << k << argv[2] << endl;
}

// Close blacklist.txt
BlackList.close();
}
}




EDIT: Looks like only the comments at the start got mushed, hooray.

sans-hubris
11-06-2002, 10:24 AM
If you're given a string that's nothing more than a number, than all you need to do is use atoi(char*), which will do all the conversion for you. Beyond that, it looks fine.

TheFarSide
11-06-2002, 10:47 AM
Originally posted by sans-hubris
If you're given a string that's nothing more than a number, than all you need to do is use atoi(char*), which will do all the conversion for you. Beyond that, it looks fine.

First, I've never worked with arrays and pointers in my life. What I've done here, is looked at an example and manipulated it enough to get it converted just by looking at the output. So, while atoi(char*) may shorten the code, I've no idea how to implement it.

Second, it doesn't look fine, because I'm still not sure on how to do the two things I listed above the code. I've never seen anything about creating a file if it can't find one in my school text, nor am I sure on how to make it find the end of the document each time the program is run, so that it doesn't overwrite the last thing that was put there.

EDIT: Wow, upon rereading that, it sounds like I'm being standoffish. I assure you, I'm not :)

stuka
11-06-2002, 10:57 AM
You will probably want to change your open call to:BlackList.open("blacklist.txt", ios::app);This will open the file in append mode like you want.

TheFarSide
11-06-2002, 11:22 AM
Originally posted by Stuka
You will probably want to change your open call to:BlackList.open("blacklist.txt", ios::app);This will open the file in append mode like you want.


YES! Perfect. I was dreading that it was going to be some long while loop where it was searching line by line until it found two \n in a row with no characters or something weird.

sans-hubris
11-06-2002, 11:49 AM
The following code:


// Check to see if it's a 3 digit number
if (argv[1][2]==0)
{
// If not, check to see if it's a 2 digit number
if (argv[1][1]==0)
{
// If not, convert to an int from acsii
i = argv[1][0] - 48;
j = 0;
k = i;
}
// If it's a 2 digit number, convert it to an int
else
{
j = argv[1][0] - 48;
m = argv[1][1] - 48;
k = (j * 10) + m;
}
}
// If it's a 3 digit number, conver it to an int
else
{
i = argv[1][0] - 48;
j = argv[1][1] - 48;
m = argv[1][2] - 48;
k = (i * 100) + (j * 10) + m;

can be replaced by the following line:

k=atoi(argv[1]);

TheFarSide
11-06-2002, 11:59 AM
Originally posted by sans-hubris
can be replaced by the following line:

k=atoi(argv[1]);



I... I... I think I love you.

kmj
11-06-2002, 12:06 PM
one thing to note about atoi .. it returns 0 on invalid input.

Strike
11-06-2002, 12:32 PM
Er, is there any real reason not to just do this instead?


echo 50 chansen@continentaloffice.com >> blacklist.txt


(yes this works on NT, well, 2k Pro at least)


C:\DOCUME~1\DDIPAOLO\DESKTOP>echo foo >> foo.txt

C:\DOCUME~1\DDIPAOLO\DESKTOP>type foo.txt
foo

C:\DOCUME~1\DDIPAOLO\DESKTOP>echo foo >> foo.txt

C:\DOCUME~1\DDIPAOLO\DESKTOP>type foo.txt
foo
foo

stuka
11-06-2002, 12:41 PM
Strike: he needs to decide whether to echo based on the number, so your solution doesn't quite work.

Strike
11-06-2002, 01:04 PM
Originally posted by Stuka
Strike: he needs to decide whether to echo based on the number, so your solution doesn't quite work.
Okay, still seems to be overkill when a script would do. But if the scripting knowledge isn't there then I suppose this is a better solution.

stuka
11-06-2002, 01:12 PM
Well, he did say it was a Windows environment, and windows "shell" scripting sucks...so it may not be overkill!

nex
11-06-2002, 02:02 PM
blacklist.bat:

if %1 LEQ 20 goto end
echo %2 >> blacklist.txt
:end

not sure what all it'd work on, i think that "LEQ" thing is some new-fangled dealie.