www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/09/24/07:32:52

From: "A. Sinan Unur" <asu1 AT cornell DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: what's the equivalent of <iostream.h> for gpp ?
Date: 24 Sep 2001 11:29:03 GMT
Organization: Cornell University
Lines: 46
Sender: asu1 AT cornell DOT invalid (on slip-32-102-40-104.ny.us.prserv.net)
Message-ID: <Xns91264C23B8583ASINANUNUR@132.236.56.8>
References: <01c14445$8cc5f3e0$bc8684d5 AT feta> <01c14449$4c5c3fe0$bc8684d5 AT feta> <3BAED94A DOT E957ABFD AT falconsoft DOT be>
NNTP-Posting-Host: slip-32-102-40-104.ny.us.prserv.net
X-Trace: news01.cit.cornell.edu 1001330943 26022 32.102.40.104 (24 Sep 2001 11:29:03 GMT)
X-Complaints-To: usenet AT news01 DOT cit DOT cornell DOT edu
NNTP-Posting-Date: 24 Sep 2001 11:29:03 GMT
User-Agent: Xnews/4.06.22
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Tim Van Holder <tim DOT vanholder AT falconsoft DOT be> wrote in
news:3BAED94A DOT E957ABFD AT falconsoft DOT be: 

> The main problem is that for a file opened in text mode (the default),
> CR/LF pairs are read as a signle LF.  So if it's a text file you're
> reading, there's a good chance you'll never see a CR.  Try using 
> fopen(file, "rb") instead of fopen(file, "r").

I don't think that is correct advice. The purpose of opening text mode is 
to transparently treat different line endings as '\n'. It does not change 
the meaning of a stand-alone CR.

The problem with the posted snippet was that it went into an infinite loop 
if the first character read from the stream was not CR, it went into an 
infinite loop. Assmuing the OP wanted to get rid of line-endings, not the 
byte 0x0d, he should open the file in text mode, and use code similar to:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
	int c;
	FILE *fin;

	fin = fopen(argv[1], "rt");
	if( fin == NULL )
	{
		perror(argv[1]);
		exit(EXIT_FAILURE);
	}

	while( (c = getc(fin)) != EOF)
		if( c != '\n' ) putc(c, stdout);

	return EXIT_SUCCESS;
}

Try this code on files with Unix and DOS line endings, and you'll see the 
same behavior.

Sinan.
-- 
--------------------------------
A. Sinan Unur
http://www.unur.com/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019