Thread View: comp.sys.mac.programmer.codewarrior
3 messages
3 total messages
Started by Rick Ross
Sun, 03 Sep 1995 18:01
getline() dilemna - text or binary?
Author: Rick Ross
Date: Sun, 03 Sep 1995 18:01
Date: Sun, 03 Sep 1995 18:01
29 lines
2009 bytes
2009 bytes
I need to process text files one line at a lime. I'd like to use
istream::getline() to do it, but my input files come from many different
sources (Mac, DOS, and UNIX). Each of these uses a different convention
for representing the end of a line:
Mac - 0x0D (carriage return or '\r')
UNIX - 0x0A (linefeed or '\n')
DOS - 0x0D, 0x0A (carriage return/linefeed pair)
The istream::getline() member function allows the delimiter character to
be specified, but I can't specify more than one. Also, the "text" mode on
my compiler translates the DOS newline characters into a single '\n' for
input and expands it again on the way back out.
I'm wondering if anyone has come up with a convenient, efficient way to
read files a line at a time, regardless of the format of the text file? Is
there an easy way to make istream::getline() adaptive to the different
formats? Should I just open the file first, make a quick pass through a
few lines to determine which newline character is being used, and then use
that character as the delimiter?
========================================================================
Rick Ross - Iris Development Corporation - rick@iway.com
___ _ _ ___ makers of _____ _
| __|_ __ __ _(_) | | _ \_____ __ _____ _ _ |_ _|__ ___| |___
| _|| ' \/ _` | | | | _/ _ \ V V / -_) '_| | |/ _ \/ _ \ (_-<
|___|_|_|_\__,_|_|_| |_| \___/\_/\_/\___|_| |_|\___/\___/_/__/
Visit us on the World Wide Web at: "http://irisdev.com"
========================================================================
Re: getline() dilemna - text or binary?
Author: tran0119
Date: Mon, 04 Sep 1995 14:12
Date: Mon, 04 Sep 1995 14:12
12 lines
873 bytes
873 bytes
rick@iway.com (Rick Ross) writes:
>Mac - 0x0D (carriage return or '\r')
>UNIX - 0x0A (linefeed or '\n')
>DOS - 0x0D, 0x0A (carriage return/linefeed pair)
I thought you just had to do :
getline(buff, num_chars);
and that will take care of the line delimeter. Besides, if it's ANSI C,
the compiler should take care of those conversions (in text mode). Am I
missing something here?
Re: getline() dilemna - text or binary?
Author: Raja R Harinath
Date: Mon, 04 Sep 1995 19:43
Date: Mon, 04 Sep 1995 19:43
42 lines
2306 bytes
2306 bytes
In article <tran0119.810223973@itlabs.umn.edu> tran0119@itlabs.umn.edu () writes: > rick@iway.com (Rick Ross) writes: > > > >Mac - 0x0D (carriage return or '\r') > >UNIX - 0x0A (linefeed or '\n') > >DOS - 0x0D, 0x0A (carriage return/linefeed pair) > > I thought you just had to do : > getline(buff, num_chars); > and that will take care of the line delimeter. Besides, if it's ANSI C, > the compiler should take care of those conversions (in text mode). Am I > missing something here? The standard library has support for/requires: * the "raw" or binary format * a _native_ text format: What the _native_ format is depends on the library author, and is obviously not portable. It also has an _internal_ text format, which basically uses '\n' (usually 0x0a) characters to delimit lines. The library has to convert between _internal_ and _native_ formats on all I/O calls. Nothing prevents the _native_ format from being identical (as in the case of UNIX - in fact, many pre-std libraries did _not_ have a text format per se), or different (as in DOS and Mac) from the _internal_ format. The problem here is that the library does _not_ support _any_ text format, it does only the _native_ format; and thru the binary/raw backdoor, the _internal_ format. All this leads to this: you cannot depend on the standard libary to handle multiple file formats for you. You have to write wrapper classes which handle the format. - Hari -- Raja R Harinath ------------------------------ harinath@cs.umn.edu "When all else fails, read the instructions." -- Cahn's Axiom "Our policy is, when in doubt, do the right thing." -- Roy L Ash
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads