Chad said:
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