473,382 Members | 1,657 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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.
Jul 19 '05 #1
4 3227
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

"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
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
Excellent, thankyou people! :)
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: bart_nessux | last post by:
Hello, I have some Macbinary files on a PC. I want to recursively read these files and remove the first 128 bytes of the files if they contain the macbinary header info. I know how to read...
4
by: Anthony | last post by:
Hi, Removing files seems to be rather clumsy in C (not C++). I am trying next piece of code to delete a file (under W32); int errno; errno = remove("FSelCancelled"); After exiting the...
0
by: sameer mowade via .NET 247 | last post by:
Hello All, I have problem while dynamically removing row from the Datagrid which i have added dynamically as shown in the following code snippet. The problem is that while removing dynamically...
9
by: David Pratt | last post by:
Hi. I'm trying to clean files for packaging on mac using os.path.walk and want to clean the .DS_Store files that are hidden from view but could end up in code that I produce. # Clean mac...
5
by: nuffnough | last post by:
This is python 2.4.3 on WinXP under PythonWin. I have a config file with many blank lines and many other lines that I don't need. read the file in, splitlines to make a list, then run a loop...
3
by: Kilicaslan Fatih | last post by:
When I push a button to trigger the code: def runCCCC(self, event): cmd_out = self.A_com() if App.runF != "": os.mkdir('C:\copiedFiles') for item in App.runF: App.beCopied = str(item)...
17
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not...
0
by: Mike | last post by:
Hi, I have a collection object bound to a data grid, after I remove an item from the collection, the minute I click on the datagrid I get an error saying the specified argument was out of the...
2
by: beatTheDevil | last post by:
Hey guys, As the title says I'm trying to make a regular expression (regex/regexp) for use in removing the comments from code. In this case, this particular regex is meant to match /* ... */...
0
by: Adam Salisbury | last post by:
**To members of microsoft.public.dotnet.framework, apologies for the crosspost. I originally posted this message into that group however have since realised this may have been a better...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.