473,804 Members | 3,742 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Q: std::getline and portability

Hi!

I've just been bitten by the famous Windows newline issue, i.e.
tried to read a textfile written by a windows application on
an unix platform. When reading a line with

std::getline(in put,buf);

GCC 3.2 leaves a trailing '\r' in buf.
Is this standard conformant? My impression was that getline
should remove '\r\n' as well as '\n'.

Thanks in advance & regards
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frank DOT schmitt AT 4sc DOT com
Jul 19 '05 #1
5 10956
On 01 Sep 2003 12:29:59 +0200, Frank Schmitt <fr***********@ 4sc.com> wrote:
Hi!

I've just been bitten by the famous Windows newline issue, i.e.
tried to read a textfile written by a windows application on
an unix platform. When reading a line with

std::getline(in put,buf);

GCC 3.2 leaves a trailing '\r' in buf.
Is this standard conformant? My impression was that getline
should remove '\r\n' as well as '\n'.


Assuming the program is running on a system that uses \n as the
line terminator (such as every unix I've heard of) then yes.

When you transfer text files between systems you need to convert them
to the destination systems format. The line terminator is the most
obvious conversion, but occassionaly there are character set issues too.

--
Sam Holden

Jul 19 '05 #2
Frank Schmitt wrote:
Hi!

I've just been bitten by the famous Windows newline issue, i.e.
tried to read a textfile written by a windows application on
an unix platform. When reading a line with

std::getline(in put,buf);

GCC 3.2 leaves a trailing '\r' in buf.
Is this standard conformant? My impression was that getline
should remove '\r\n' as well as '\n'.


It is conformant AFAIK. A platform only needs to be prepared for its own
file format.

--
Attila aka WW
Jul 19 '05 #3
Frank Schmitt <fr***********@ 4sc.com> writes:
Hi!

I've just been bitten by the famous Windows newline issue, i.e.
tried to read a textfile written by a windows application on
an unix platform. When reading a line with

std::getline(in put,buf);

GCC 3.2 leaves a trailing '\r' in buf.
Is this standard conformant? My impression was that getline
should remove '\r\n' as well as '\n'.


Only if (a) you opened the file in text mode and *not* in binary mode,
and (b) your platform defines CR LF as a newline (like windows,
but not like unix or mac).
Jul 19 '05 #4
Frank Schmitt <fr***********@ 4sc.com> writes:
Hi!

I've just been bitten by the famous Windows newline issue, i.e.
tried to read a textfile written by a windows application on
an unix platform. When reading a line with

std::getline(in put,buf);

GCC 3.2 leaves a trailing '\r' in buf.
Is this standard conformant?
Yes.
My impression was that getline
should remove '\r\n' as well as '\n'.


Not on unix. On unix \n is just LF, so that is all that is
removed. The CR windows put there is not part of a unix newline,
and is left alone. If you want it removed you must do so
yourself.
Jul 19 '05 #5
llewelly <ll*********@xm ission.dot.com> writes:
Frank Schmitt <fr***********@ 4sc.com> writes:
Hi!

I've just been bitten by the famous Windows newline issue, i.e.
tried to read a textfile written by a windows application on
an unix platform. When reading a line with

std::getline(in put,buf);

GCC 3.2 leaves a trailing '\r' in buf.
Is this standard conformant?


Yes.
My impression was that getline
should remove '\r\n' as well as '\n'.


Not on unix. On unix \n is just LF, so that is all that is
removed. The CR windows put there is not part of a unix newline,
and is left alone. If you want it removed you must do so
yourself.


Ah, I feared so.

Thanks to all for the quick & informative answers
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frank DOT schmitt AT 4sc DOT com
Jul 19 '05 #6

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

Similar topics

5
7752
by: vknid | last post by:
Hello, I have a question. Its probably a very newbish question so please be nice hehe. =D I have been reading through C++ Programming Fundamentals, and have come a crossed an example program that shows how to use the 'getline' function. It said: "The getline function allows you to specify how many bytes you will get from the users input. Each character the user's types takes up one byte. So if, in the getline function, you specify...
10
5619
by: Skywise | last post by:
I keep getting the following error upon compiling: c:\c++ files\programs\stellardebug\unitcode.h(677) : error C2664: 'class istream &__thiscall istream::getline(char *,int,char)' : cannot convert parameter 1 from 'const char *' to 'char *' Conversion loses qualifiers I have a data file called Standard.udf The Data File (not the problem): The data in the file is in text format, and was created using a function in this program with...
4
5744
by: sam | last post by:
Hi, How can I use getline ignore the commandline arguments? Thanks Sam
7
2463
by: Vladimir Lushnikov | last post by:
Hi, I have just started learning C++ and have been trying to do simple string manipulation. However, when calling this function: int helloString() { string name; cout << "Please enter your name:\n";
18
8259
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include <iostream> #include <fstream> #include <cstdlib> #include <string>
2
1978
by: aGAric | last post by:
i use std::getling to read a line in unicoude file to buffer,like: WCHAR buf wifstream in; std::getling(buf,in,100); but i can't get the right result,it seems can only read by 1 byte;e.g first word of unicode file is "0xfffe" ,if use getline ,it may 0x00ff 0x00fe in my buffer do you have any idea,except use c lib like fgetws
2
6015
by: Bit Byter | last post by:
I have a method in a class implemented like this: int foo (const char* filename, bool flg) { FILE *fp; char *buffer, *tokstr, *tmp; size_t size, toksize; unsigned int i; if ((fp = fopen(filename, "r")) == NULL)
1
4049
by: Kevin Eller | last post by:
I haven't used std::getline for a long time, if someone can help me
6
6779
by: JML | last post by:
Hi, I have some code which parses a text file and creates objects based on what is in the text file. The code works just fine on Windows, but when I compile it using XCode on OS X the parsing goes all wrong. Is there some known differences with file handling on OS X? My code is quite long, but one of the defect parts looks like this (sorry about the indentation - I'm new to posting code on a newsgroup):
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9571
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10561
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.