473,795 Members | 3,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

newlines in text files

Hi, I have this problem: when reading a M$ Windows text file under Windows in
text mode (fopen("blah", "r")) the Windows newline sequence, \r\n is returned as
is, i.e. it's not replaced by a single '\n'. Is this the correct behaviour? But
doesn't it make writing portable programs a bit harder? It would be nice if
whatever-newline-sequence-the-platform-has were replaced by a single \n, i.e.
the "C platform" line terminator. Like ints are always 64 bit in Java
irregardless of what platform lies under the Java platform.

In case it is indeed like that, how is my program supposed to know which line
terminator is being used?
Nov 13 '05 #1
7 2312
rihad wrote:
Hi, I have this problem: when reading a M$ Windows text file under Windows
in text mode (fopen("blah", "r")) the Windows newline sequence, \r\n is
returned as is, i.e. it's not replaced by a single '\n'. Is this the
correct behaviour?
No. Check your text file using a hex-capable file browser (e.g. LIST.COM if
you have it). If you don't have a hex-capable file browser, here's one:

FILE *fp = fopen(filename, "rb");
if(fp != NULL)
{
int ch = 0;
int i = 0;
while((ch = getc(fp)) != EOF)
{
printf(" %02X", ch);
i++;
if(16 == i)
{
putchar('\n');
i = 0;
}
}
fclose(fp);
}
putchar('\n');

But doesn't it make writing portable programs a bit
harder? It would be nice if whatever-newline-sequence-the-platform-has
were replaced by a single \n, i.e. the "C platform" line terminator.


It would be nice, and indeed it /is/ nice. Check your input file, and check
your fopen call. It's far more likely that your data or code is broken than
that your compiler is broken.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #2
rihad wrote:

Hi, I have this problem: when reading a M$ Windows text file under
Windows in text mode (fopen("blah", "r")) the Windows newline
sequence, \r\n is returned as is, i.e. it's not replaced by a
single '\n'. Is this the correct behaviour? But doesn't it make
writing portable programs a bit harder? It would be nice if
whatever-newline-sequence-the-platform-has were replaced by a
single \n, i.e. the "C platform" line terminator. Like ints are
always 64 bit in Java irregardless of what platform lies under
the Java platform.

In case it is indeed like that, how is my program supposed to
know which line terminator is being used?


There is something wrong with your installation and/or library.
The only way you should see both the /r and /n is if you open the
file in binary mode. Similarly writing a /n should create the /r
/n sequence.

However, you may be operating under Cygwin or something similar,
which attempts to create a complete Li/U-nix environment under
Windoze. I don't know just what provisions they make.

At any rate this is not a C language problem, and should be dealt
with in a newsgroup dedicated to your compiler/system.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #3
On Sat, 04 Oct 2003 17:31:14 GMT, CBFalconer <cb********@yah oo.com> wrote:

However, you may be operating under Cygwin or something similar,
which attempts to create a complete Li/U-nix environment under
Windoze. I don't know just what provisions they make.


You're right! I recompiled the program with mingw's gcc and the problem went
away. I'll have to ask why Cygwin worked that way in a different newsgroup.

Nov 13 '05 #4
rihad <ri***@mail.r u> writes:
On Sat, 04 Oct 2003 17:31:14 GMT, CBFalconer <cb********@yah oo.com> wrote:

However, you may be operating under Cygwin or something similar,
which attempts to create a complete Li/U-nix environment under
Windoze. I don't know just what provisions they make.


You're right! I recompiled the program with mingw's gcc and the problem went
away. I'll have to ask why Cygwin worked that way in a different newsgroup.


Because you probably told it to (IIRC, you are asked to specify
whether you want Cygwin to use Windows or Unix line-endings).

Cygwin is a different implementation from the implementation that
created the text file in question, so all bets are off. Cygwin
attempts to emulate a UNIX-like operating system, whereas if you
used Notepad or somesuch, you wrote the text file from within a
Windows operating system.

-Micah
Nov 13 '05 #5
On 04 Oct 2003 17:10:15 -0700, Micah Cowan <mi***@cowan.na me> wrote:
Cygwin is a different implementation from the implementation that
created the text file in question, so all bets are off. Cygwin
attempts to emulate a UNIX-like operating system, whereas if you
used Notepad or somesuch, you wrote the text file from within a
Windows operating system.

Then because of C's Unix heritage, can it be said that \n text files are the
most portable?
Nov 13 '05 #6
rihad wrote:
Micah Cowan <mi***@cowan.na me> wrote:
Cygwin is a different implementation from the implementation that
created the text file in question, so all bets are off. Cygwin
attempts to emulate a UNIX-like operating system, whereas if you
used Notepad or somesuch, you wrote the text file from within a
Windows operating system.

Then because of C's Unix heritage, can it be said that \n text
files are the most portable?


No. There are three common flavors of text files with line
termination sequences:

<crlf> \r\n CP/M, MsDos, Windoze, others
<lf> \n Linux, Unix, etc.
<cr> \r Macintosh, possibly other Apples.

and filters are available on most systems to convert between the
standards. Bear in mind that many other systems do not even have
a line termination sequence - they use other means, such as a
count of chars in a fixed length record, or whatever.

On MsDos/Windoze you can easily see the differences with such hex
capable systems as Buerg's LIST. TEXTPAD is capable of generating
(and converting) between at least Linux and Dos conventions, but
can't switch between hex and char displays.

As far as your C program is concerned, you end lines with a \n.
Nothing else enters into it. The i/o libraries will handle the
rest in a manner suitable for your system.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #7
rihad <ri***@mail.r u> writes:
On 04 Oct 2003 17:10:15 -0700, Micah Cowan <mi***@cowan.na me> wrote:
Cygwin is a different implementation from the implementation that
created the text file in question, so all bets are off. Cygwin
attempts to emulate a UNIX-like operating system, whereas if you
used Notepad or somesuch, you wrote the text file from within a
Windows operating system.

Then because of C's Unix heritage, can it be said that \n text files are the
most portable?


No, it cannot. What can be said is that all text files must be
represented as having "\n" line-endings to any C program that
opens them as a text file. However, what constitutes a "text
file" is defined by the particular implementation. In this case,
the text files made in Notepad do not fit Cygwin's idea of a
"text file".

-Micah
Nov 13 '05 #8

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

Similar topics

4
1758
by: Alessandro Crugnola *sephiroth* | last post by:
Hi, I'm trying to detect the newlines method of a file opened. I'm using the 'U' mode as parameter when opening a file, but for every file opened the result is always "\r\n", even if the file has been saved with the "\n" newline method. Is there i'm missing? I'm on Windows with python 2.3.3
5
4917
by: Thomas Philips | last post by:
I have a data file that I read with readline(), and would like to control the formats of the lines when they are printed. I have tried inserting escape sequences into the data file, but am having trouble getting them to work as I think they should. For example, if my data file has only one line which reads: 1\n234\n567 I would like to read it with a command of the form x=datafile.readline()
4
2486
by: Grant Edwards | last post by:
I'm using xml.sax to parse the "datebook" xml file generated by QTopiaDesktop. When I look at the xml file, some of the attribute strings have newlines in them (as they are supposed to). However, when xml.sax passes the attributes to my startElement() method the newlines seem to have been deleted. How do I get the un-munged element attribute values?
1
1807
by: dbee | last post by:
Hi, So I'm having a problem with disappearing newlines. I import the newlines from a file into my shell script fine. But then I process the text and the url_encode comes out the other end with linefeeds intact but no newlines. Also any single quotes in my file give me a T_STRING unexpected error in my output ... even though I'm using rawurlencode(). I've tried escaping them in my text but no luck there.
0
1384
by: skip | last post by:
*argh!* I hate XML! There, now that that's off my chest... I am trying to save Python code as attributes of an XML tag with xml.dom.minidom machinery. The code, predicatbly enough, contains newlines. If I do nothing to my program text, upon output I get XML which looks like this: <SomeTag text="def _f(): return 3 "/>
2
1995
by: Edward K. Ream | last post by:
Hello all, I recently ran across a situation in which sax.saxutils.quoteattr did not work as I expected. I am writing Leo outlines as opml files http://en.wikipedia.org/wiki/OPML which forces me to write python code in xml attributes rather than xml elements (as is done in .leo files). The problem is that the sax parser I am using ignores newlines in attributes. After reading the thread at:
6
2541
by: chrispwd | last post by:
Hello, I have a situation where I have a file that contains text similar to: myValue1 = contents of value1 myValue2 = contents of value2 but with a new line here myValue3 = contents of value3 My first approach was to open the file, use readlines to split the
5
5167
by: Neil Crighton | last post by:
I'm using the zipfile library to read a zip file in Windows, and it seems to be adding too many newlines to extracted files. I've found that for extracted text-encoded files, removing all instances of '\r' in the extracted file seems to fix the problem, but I can't find an easy solution for binary files. The code I'm using is something like: from zipfile import Zipfile z = Zipfile(open('zippedfile.zip'))
3
4530
by: joelkeepup | last post by:
Hi, im trying to create a text email message using xslt template , the transforms work great, but the newlines and whitespace in the xslt doc are removed. Is there a setting somewhere I have missed: My template is: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result- prefixes="msxsl">
0
9672
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
9519
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
10435
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
10163
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
10000
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
9037
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...
0
6779
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.