473,320 Members | 1,961 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,320 software developers and data experts.

istream altering text

a kinda newbie here.
I've done a simple little program that reads a text file and counts the number of lines and words.
I had a heck of a time getting it to count properly when I finally discovered the problem was not my coding, but the istream altering the incoming text.
What I was doing was checking each incoming character (seekg) and comparing it to a 'h0a' . What I found was that text files end their lines with a '0d' (CR) and a '0a' (line feed). However, it was reading them off the istream as both being '0a'. It had changed the CR.
My questions are, Is there any other little istream quirks like this I should be aware of? And is there some way to set the stream to not alter what is read?
TIA
-doh
Mar 1 '08 #1
2 1933
dohboy wrote:
a kinda newbie here.
I've done a simple little program that reads a text file and counts the number of lines and words.
I had a heck of a time getting it to count properly when I finally discovered the problem was not my coding, but the istream altering the incoming text.
What I was doing was checking each incoming character (seekg) and comparing it to a 'h0a' . What I found was that text files end their lines with a '0d' (CR) and a '0a' (line feed). However, it was reading them off the istream as both being '0a'. It had changed the CR.
My questions are, Is there any other little istream quirks like this I should be aware of? And is there some way to set the stream to not alter what is read?
It'd more of a normalisation than a quirk. It saves the programmer from
tedious platform specific conversion code.

I guess another is the use of the eof flag to hide platform specific
file endings.

--
Ian Collins.
Mar 2 '08 #2
Hi,

Didn't test the following code. But you might want to use something like
this

#include <fstream>
#include <string>
#include <algorithm>

using namespace std;

ifstream Input( Filename.c_str(), ios_base::binary );

// test if open!
//So files are read the same on whatever system then you might want to get
rid of any carriage returns with something like
string Line;
while( getline( Input, Line ) )
{
Line.erase( remove_if( Line.begin(), Line.end(), bind2nd(
equal_to<char>(), (char)13) ), Line.end() );

// From here on Line is the same on unix and ms-windows
}
Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

"dohboy" <do****@bollocks.comwrote in message
news:ic********************************@4ax.com...
>a kinda newbie here.
I've done a simple little program that reads a text file and counts the
number of lines and words.
I had a heck of a time getting it to count properly when I finally
discovered the problem was not my coding, but the istream altering the
incoming text.
What I was doing was checking each incoming character (seekg) and
comparing it to a 'h0a' . What I found was that text files end their lines
with a '0d' (CR) and a '0a' (line feed). However, it was reading them off
the istream as both being '0a'. It had changed the CR.
My questions are, Is there any other little istream quirks like this I
should be aware of? And is there some way to set the stream to not alter
what is read?
TIA
-doh

Mar 2 '08 #3

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

Similar topics

3
by: Charles Law | last post by:
Could anyone please tell me what is wrong with the code below: STDMETHODIMP CSimpleObj::WriteUnmanagedData(BSTR pString, IStream** ppData) { HRESULT hr; hr = CreateStreamOnHGlobal(0, TRUE,...
9
by: JimmyKoolPantz | last post by:
IDE: Visual Studio 2005 Language: VB.NET Fox Pro Driver Version: 9.0.0.3504 Problem: I currently have a problem altering a DBF file. I do not get any syntax errors when running the program. ...
3
by: Kourosh | last post by:
Hi all, I'm trying to call a COM function from C# The function gets a paramter of type IStream in C++. I've found the type System.Runtime.InteropServices.ComTypes.IStream, however I'm not sure...
0
by: admb600 | last post by:
I wouldn't normally beg but my dissertation is due in, in a couple of weeks and I am in way over my head here.. Fixing this issue will make or break the project and my degree.. The goal is to...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.