Removing \n from a file

C

Chad

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.
 
T

tom_usenet

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
 
C

Chris Theis

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
 
R

Rolf Magnus

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.

#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);
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top