Scripts Thread, C++ query: read and write to file ignoring newlines/carriage returns in Coding and Web Development; Hello, I'm writing a C++ program that scans emails for words on our school "banned word list".
I have got ...
-
16th September 2008, 03:41 PM #1 C++ query: read and write to file ignoring newlines/carriage returns
Hello, I'm writing a C++ program that scans emails for words on our school "banned word list".
I have got to the point where we can paste text from an email into command line which gets saved in a .txt file called 'emailscan' - this is then read by ifstream and scanned for the chosen word which if found is returned with its location
My pickle is that the user cannot paste more than a single paragraph of text.
For example if we copy an entire email, which more often than not contains several carriage returns, only the first block of text before the first carriage return is fed into the emailscan.txt file and subsequently only this part of the email is scanned.
Is there a way of ignoring/discarding carriage returns or converting text after the cin >> operator in order to merge all the text into one 'paragraph'?
Thanks
Michael
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string line, paragraph, text, textin;
int i, location;
char* word = "virus"; // <--for example
// USER TO PASTE IN EMAIL
cout << "Paste text here:" << endl;
cin >> text;
cout << "You entered: " << text << endl;
getline(cin, text);
cin.ignore('\n');
ofstream filewriter;
filewriter.open("emailscan.txt"); // read in text to file called
filewriter << text << endl; // emailscan
if(!filewriter) // check that emailscan can be opened {
cout << "Error opening file for output" << endl;
return -1;
}
filewriter.close();
cout << "The text has been saved in emailscan.txt" << endl; // checkpoint
///////////////////////////////////////////////////////////////////////////////////////
// OPEN AND READ EMAILSCAN.TXT
ifstream filereader("emailscan.txt"); // first, read the text file
if( !filereader) // check that emailscan can be opened {
cout << "Error opening file for output" << endl;
return -1;
}
for(i = 0; ! filereader.eof(); i++){ // run a loop to read lines
getline(filereader,line); cout << line << endl;
///////////////////////////////////////////////////////////////////////////////////////
// FIND A WORD FOR EXAMPLE 'VIRUS'
if ((location = line.find(word, 0)) != string::npos) {
cout << "found \"" << word << "\" at location " << location << endl;
} else filereader.close(); // look for the word and if found state where
}
cout << line << endl;
filereader.close();
cout << "Iterations:" << i << endl; // how many lines?
return 0;
}
Last edited by mcheung0; 17th September 2008 at 08:43 AM.
-
-
IDG Tech News
-
16th September 2008, 04:05 PM #2 Personally I never got on with iostream.. but a simple-ish solution is
while(!cin.eof())
{
cin >> text;
doChecks(text);
}
-
-
16th September 2008, 05:58 PM #3 
Originally Posted by
mcheung0
Hello, I'm writing a C++ program that scans emails for words on our school "banned word list".
Now open: StackOverflow, for all your programming questions:
Stack Overflow
As created by Joel Spolsky:
Stack Overflow Launches - Joel on Software
And Jeff Atwood:
Coding Horror: Stack Overflow: None of Us is as Dumb as All of Us
I have got to the point where the user pastes the email which gets saved in a .txt file called 'emailscan' - this is then read by ifstream and scanned for the chosen word which if found is returned with its location
Sorry, what's being pasted where, and on what? Are you writing a filter for an email system? Those generally tend to work by taking input on stdin and writing to stdout. Writing to an intermediate text file is going to slow you down somewhat.
--
David Hicks
-
-
16th September 2008, 10:32 PM #4 Hello thanks for both replies -
the user copies text from an email and pastes it to a text box (the command line for now)
it's not as complicated as streaming directly from email (yet)
michael
-
SHARE:
Similar Threads
-
By maximus44 in forum Bad Experiences
Replies: 5
Last Post: 11th June 2008, 05:29 PM
-
By Millsy79 in forum How do you do....it?
Replies: 0
Last Post: 6th March 2008, 11:12 PM
-
By j17sparky in forum General Chat
Replies: 8
Last Post: 24th January 2008, 07:21 PM
-
By markwilliamson2001 in forum Web Development
Replies: 5
Last Post: 5th October 2007, 08:43 AM
-
By tosca925 in forum Windows
Replies: 16
Last Post: 10th February 2006, 08:27 AM
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules