Removing \n from a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chad

    Removing \n from a file

    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.
  • tom_usenet

    #2
    Re: Removing \n from a file

    On 14 Nov 2003 05:37:56 -0800, chad@mizmoz.com (Chad) wrote:
    [color=blue]
    >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.[/color]

    #include <fstream>
    int main()
    {
    std::ifstream infile("InFile. html");
    std::ofstream outfile("OutFil e.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

    Comment

    • Chris Theis

      #3
      Re: Removing \n from a file


      "Chad" <chad@mizmoz.co m> wrote in message
      news:bb0f9985.0 311140537.7d8bb 1@posting.googl e.com...[color=blue]
      > 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.[/color]

      A quick solution would be to use istream_iterato rs to read in each string of
      your file which is delimited by a space. Just assign the values of these
      istream_iterato rs to a collection (e.g. vector, list) and copy this
      collection to an output file using ostream_iterato rs. 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_iterato r<string>( InFile ),
      istream_iterato r<string>() );
      // otherwise you'll have to create a list and fill it via the first copy
      statement.
      list<string> DataList;
      copy( istream_iterato r<string>( InFile ), istream_iterato r<string>(),
      back_inserter( DataList ) );
      copy( DataList.begin( ), DataList.end(), ostream_iterato r<string>( OutFile,
      " ") );
      return 0;

      }

      HTH
      Chris


      Comment

      • Rolf Magnus

        #4
        Re: Removing \n from a file

        Chad wrote:
        [color=blue]
        > 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.[/color]

        #include <iostream>
        #include <fstream>

        void remove_newlines (std::istream& is, std::ostream& os)
        {
        std::string line;
        while (std::getline(i s, line) && os << line);
        }

        int main()
        {
        std::ifstream infile("input.h tml");
        std::ofstream outfile("output .html");

        remove_newlines (infile, outfile);
        }



        Comment

        • Chad

          #5
          Re: Removing \n from a file

          Excellent, thankyou people! :)

          Comment

          Working...