opening a file in text mode

A

Alex Gerdemann

Hello,

I'm writing a program using the g++ compiler that runs under Cygwin in
Windows, and have written the following:

std::ifstream file;
char buffer[128];
std::string myString;
file.open(filename);
file.getline(buffer,128);
myString=buffer;

This ought to open filename in text mode so all instances of \r\n will be
replaced with \n, and no line ending characters end up in myString.
However, when I actually run this, I end up with a \r character on the end
of all my strings. Where did I go wrong? The Cygwin documentation
(http://www.cygwin.com/cygwin-ug-net/using-textbinary.html) assures me that
it defaults to opening files in text mode as it should.

Incidentally, coming from a Java background, having to define a fixed size
buffer seems very aukward. Can it be avoided?

Thanks,

Alex Gerdemann
University of Illinois Urbana-Champaign
 
R

Rolf Magnus

Alex said:
Hello,

I'm writing a program using the g++ compiler that runs under Cygwin in
Windows, and have written the following:

std::ifstream file;
char buffer[128];
std::string myString;
file.open(filename);
file.getline(buffer,128);
myString=buffer;

This ought to open filename in text mode so all instances of \r\n will be
replaced with \n, and no line ending characters end up in myString.
However, when I actually run this, I end up with a \r character on the end
of all my strings. Where did I go wrong? The Cygwin documentation
(http://www.cygwin.com/cygwin-ug-net/using-textbinary.html) assures me
that it defaults to opening files in text mode as it should.

Incidentally, coming from a Java background, having to define a fixed size
buffer seems very aukward. Can it be avoided?

Lol. While reading your text, I was wondering why you use a fixed size
buffer. No, you don't need to. Just write:

std::getline(file, myString);
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Alex said:
This ought to open filename in text mode so all instances of \r\n will be
replaced with \n, and no line ending characters end up in myString.
However, when I actually run this, I end up with a \r character on the end
of all my strings. Where did I go wrong? The Cygwin documentation

Cygwin is intended to work like a unix inside windows. To write programs
that works closely with the windows environment MinGW is a better choice.
 
R

rossum

Hello,

I'm writing a program using the g++ compiler that runs under Cygwin in
Windows, and have written the following:

std::ifstream file;
char buffer[128];
std::string myString;
file.open(filename);
file.getline(buffer,128);
myString=buffer;

This ought to open filename in text mode so all instances of \r\n will be
replaced with \n, and no line ending characters end up in myString.
However, when I actually run this, I end up with a \r character on the end
of all my strings. Where did I go wrong? The Cygwin documentation
(http://www.cygwin.com/cygwin-ug-net/using-textbinary.html) assures me that
it defaults to opening files in text mode as it should.
Can't help you with that I'm afraid, no '\r' appears on my system.
Incidentally, coming from a Java background, having to define a fixed size
buffer seems very aukward. Can it be avoided?
Yes, use std::string instead.

std::ifstream file;
std::string myString;
file.open(filename);
getline(file, myString);

Note that you need to use the non-menber version of getline() with a
std::string.

rossum
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top