Removing \n from a file

Discussion in 'C++' started by Chad, Nov 14, 2003.

  1. Chad

    Chad Guest

    Hello, Im new to c++ and want to know how i can remove \n from a file.
    Im used to php where it is very easy to do. Im trying to remove the \n
    from a html file if that makes any difference. Thanks for any help.
     
    Chad, Nov 14, 2003
    #1
    1. Advertising

  2. Chad

    tom_usenet Guest

    On 14 Nov 2003 05:37:56 -0800, (Chad) wrote:

    >Hello, Im new to c++ and want to know how i can remove \n from a file.
    >Im used to php where it is very easy to do. Im trying to remove the \n
    >from a html file if that makes any difference. Thanks for any help.


    #include <fstream>
    int main()
    {
    std::ifstream infile("InFile.html");
    std::eek:fstream outfile("OutFile.html");
    char c;
    while (infile.get(c))
    {
    if (c != '\n')
    outfile.put(c);
    }
    }

    There are more efficient ways to do this of course, but that should be
    sufficient for your needs.

    Tom
     
    tom_usenet, Nov 14, 2003
    #2
    1. Advertising

  3. Chad

    Chris Theis Guest

    "Chad" <> wrote in message
    news:...
    > Hello, Im new to c++ and want to know how i can remove \n from a file.
    > Im used to php where it is very easy to do. Im trying to remove the \n
    > from a html file if that makes any difference. Thanks for any help.


    A quick solution would be to use istream_iterators to read in each string of
    your file which is delimited by a space. Just assign the values of these
    istream_iterators to a collection (e.g. vector, list) and copy this
    collection to an output file using ostream_iterators. For
    these you can specify the delimiter (in your case a blank) and you are done.
    A probably more comprehensible solution is to read every character and
    output only those that are different from \n. Anway, this is just to show
    you how efficient the use of the standard library can be with such problems.


    #include <list>
    #include <stream>
    #include <iostream>
    #include <fstream>
    #include <iterator>

    using namespace std;

    int main()
    {
    ifstream InFile( "c:\\winzip.log" );
    ofstream OutFile( "c:\\winzip.out" );
    if( !InFile || !OutFile ) {
    cerr << "Error opening the input or output file" << endl;
    return false;
    }

    // If your list class supports initialization using iterators as its ctor
    arguments
    // you can do this
    //list<string> DataList( istream_iterator<string>( InFile ),
    istream_iterator<string>() );
    // otherwise you'll have to create a list and fill it via the first copy
    statement.
    list<string> DataList;
    copy( istream_iterator<string>( InFile ), istream_iterator<string>(),
    back_inserter( DataList ) );
    copy( DataList.begin(), DataList.end(), ostream_iterator<string>( OutFile,
    " ") );
    return 0;

    }

    HTH
    Chris
     
    Chris Theis, Nov 14, 2003
    #3
  4. Chad

    Rolf Magnus Guest

    Chad wrote:

    > Hello, Im new to c++ and want to know how i can remove \n from a file.
    > Im used to php where it is very easy to do. Im trying to remove the \n
    > from a html file if that makes any difference. Thanks for any help.


    #include <iostream>
    #include <fstream>

    void remove_newlines(std::istream& is, std::eek:stream& os)
    {
    std::string line;
    while (std::getline(is, line) && os << line);
    }

    int main()
    {
    std::ifstream infile("input.html");
    std::eek:fstream outfile("output.html");

    remove_newlines(infile, outfile);
    }
     
    Rolf Magnus, Nov 14, 2003
    #4
  5. Chad

    Chad Guest

    Excellent, thankyou people! :)
     
    Chad, Nov 15, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom
    Replies:
    5
    Views:
    438
  2. bart_nessux
    Replies:
    6
    Views:
    1,002
    Peter Hansen
    Apr 29, 2004
  3. Noud Aldenhoven

    removing comments form a file

    Noud Aldenhoven, Jan 3, 2005, in forum: Python
    Replies:
    2
    Views:
    371
  4. Replies:
    17
    Views:
    600
    Anthra Norell
    Sep 5, 2006
  5. Chris  Chiasson
    Replies:
    6
    Views:
    636
    Richard Tobin
    Nov 14, 2006
Loading...

Share This Page