By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 913 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Removing \n from a file

P: n/a
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.
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 14 Nov 2003 05:37:56 -0800, ch**@mizmoz.com (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::ofstream 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
Jul 19 '05 #2

P: n/a

"Chad" <ch**@mizmoz.com> wrote in message
news:bb************************@posting.google.com ...
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
Jul 19 '05 #3

P: n/a
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::ostream& os)
{
std::string line;
while (std::getline(is, line) && os << line);
}

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

remove_newlines(infile, outfile);
}

Jul 19 '05 #4

P: n/a
Excellent, thankyou people! :)
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.