+ Post New Thread
Results 1 to 4 of 4
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 ...
  1. #1
    mcheung0's Avatar
    Join Date
    Sep 2008
    Location
    York
    Posts
    24
    Thank Post
    0
    Thanked 2 Times in 2 Posts
    Rep Power
    13

    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.

  2. #2


    tom_newton's Avatar
    Join Date
    Sep 2006
    Location
    Leeds
    Posts
    4,485
    Thank Post
    867
    Thanked 853 Times in 674 Posts
    Rep Power
    197
    Personally I never got on with iostream.. but a simple-ish solution is

    while(!cin.eof())
    {
    cin >> text;
    doChecks(text);
    }

  3. #3

    dhicks's Avatar
    Join Date
    Aug 2005
    Location
    Knightsbridge
    Posts
    5,683
    Thank Post
    1,268
    Thanked 788 Times in 685 Posts
    Rep Power
    237
    Quote Originally Posted by mcheung0 View Post
    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

  4. #4
    mcheung0's Avatar
    Join Date
    Sep 2008
    Location
    York
    Posts
    24
    Thank Post
    0
    Thanked 2 Times in 2 Posts
    Rep Power
    13
    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:
+ Post New Thread

Similar Threads

  1. HP returns
    By maximus44 in forum Bad Experiences
    Replies: 5
    Last Post: 11th June 2008, 05:29 PM
  2. ignoring cache files with veritas backup exe
    By Millsy79 in forum How do you do....it?
    Replies: 0
    Last Post: 6th March 2008, 10:12 PM
  3. Returns procedure
    By j17sparky in forum General Chat
    Replies: 8
    Last Post: 24th January 2008, 06:21 PM
  4. Query of existing query data in PHP
    By markwilliamson2001 in forum Web Development
    Replies: 5
    Last Post: 5th October 2007, 08:43 AM
  5. Replies: 16
    Last Post: 10th February 2006, 07: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
  •