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

opening a file in text mode

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/...extbinary.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
Jul 22 '05 #1
3 1974
Alex Gerdemann wrote:
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/...extbinary.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);

Jul 22 '05 #2
Alex Gerdemann wrote:
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.

--
Salu2
Jul 22 '05 #3
On Sun, 10 Oct 2004 11:34:43 GMT, "Alex Gerdemann"
<nu*******@hotmail.com> wrote:
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/...extbinary.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

Thanks,

Alex Gerdemann
University of Illinois Urbana-Champaign


--

The ultimate truth is that there is no Ultimate Truth
Jul 22 '05 #4

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

Similar topics

2
by: Dave Reid | last post by:
Hi everyone... I'm trying to open a text file in exclusive mode (meaning I want to lock out any other processes from accessing the file while I'm working with it). Here's my code: ifstream...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
2
by: Suma | last post by:
Hi To open a notepad file, right now I am using Process.start. But this does not ensure that the file is opened in readonly mode How can I make a notepad file open in Readonly mode I do not...
11
by: Lee Crabtree | last post by:
I've got a solution that has a C++ project that compiles to a DLL, and a C# project that is the main piece of the program. The DLL is managed C++ on top of regular C++. In one of the native...
5
by: mlg1906 | last post by:
I'm developing an intranet site in ASP.NET 2.0 but I can't seem to connect to the DB from within my code. I've created a .vb class that houses a private Connection() that other functions within the...
2
by: neib | last post by:
Hi I am writing a tool to extract form, report, properties to a XML format for further analysis (cross referencing etc) using Python. Reports and Forms can be accessed using AllForms,...
8
by: Claudio Grondi | last post by:
Here an example of what I mean (Python 2.4.2, IDLE 1.1.2, Windows XP SP2, NTFS file system, 80 GByte large file): Traceback (most recent call last): File "<pyshell#1>", line 1, in -toplevel-...
1
by: raghu | last post by:
i want to know the difference between 'r' mode and 'r+' mode 1.i = open('c:\python25\integer.txt','w')-------->for writiing i.write('hai')--------->written some content in text file i =...
1
by: manu | last post by:
Hi, Can anyone please tell me the difference of opening file in Binary mode or in Text mode?
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
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...
0
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,...

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.