Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > stream functions

Reply
Thread Tools

stream functions

 
 
Bill Cunningham
Guest
Posts: n/a
 
      02-07-2012
Now I want to make sure I get this straight. fread and fwrite is for
binary files and data. fgets, fgetc, ftell, fseek, and the f*pos functions
are for textual data. What should be put in that fpos_t* type with f*pos?

Bill


 
Reply With Quote
 
 
 
 
John Gordon
Guest
Posts: n/a
 
      02-07-2012
In <4f319cb1$0$1049$(E-Mail Removed)> "Bill Cunningham" <(E-Mail Removed)> writes:

> Now I want to make sure I get this straight. fread and fwrite is for
> binary files and data. fgets, fgetc, ftell, fseek, and the f*pos functions
> are for textual data.


No, that's not right.

fread and fwrite work on both binary and text data.

fgets is for reading one line of text from a stream.

fgetc is for reading one character from a stream.

ftell and fseek are for getting and setting the position within a stream.

The f*pos() functions handle streams which are too large for ftell/fseek,
and they also have special code to handle multibyte stream content.

Where do you come up with this stuff, Bill? Do you not have access to
documentation?

--
John Gordon A is for Amy, who fell down the stairs
http://www.velocityreviews.com/forums/(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      02-07-2012
John Gordon wrote:
>
> No, that's not right.
>
> fread and fwrite work on both binary and text data.
>
> fgets is for reading one line of text from a stream.
>
> fgetc is for reading one character from a stream.
>
> ftell and fseek are for getting and setting the position within a
> stream.
>
> The f*pos() functions handle streams which are too large for
> ftell/fseek,
> and they also have special code to handle multibyte stream content.
>
> Where do you come up with this stuff, Bill? Do you not have access to
> documentation?


Yes I read the man pages. That's what it looked like to me. What you
said about f*pos functions handling things too big, well...It wasn't in the
man pages so it's new to me. I guess it would depend on what the sizeof that
pointer to that fpos_t type was I guess. What are you fseeking in a text
file for? I know text right? What if I was seeking for a number in a column
of a text file written with nano? Say 10. How would I skip all the other
10's in the file?

Bill


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      02-07-2012
Bill Cunningham wrote:
> John Gordon wrote:
>>
>> No, that's not right.
>>
>> fread and fwrite work on both binary and text data.
>>
>> fgets is for reading one line of text from a stream.
>>
>> fgetc is for reading one character from a stream.
>>
>> ftell and fseek are for getting and setting the position within a
>> stream.

[snip]

So is ftell and fseek for binary files?

Bill


 
Reply With Quote
 
Johann Klammer
Guest
Posts: n/a
 
      02-08-2012
Bill Cunningham wrote:
> [snip]
>
> So is ftell and fseek for binary files?
>
> Bill
>
>

They are _all_ for binary files.
Text files are a subset of them

....

Except in windows, where newline-carriage-return conversion may be going
on if you do not fopen() with "rb" or "wb"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-08-2012
Johann Klammer <(E-Mail Removed)1.net> writes:
> Bill Cunningham wrote:
>> [snip]
>>
>> So is ftell and fseek for binary files?
>>
>> Bill
>>
>>

> They are _all_ for binary files.
> Text files are a subset of them
>
> ...
>
> Except in windows, where newline-carriage-return conversion may be going
> on if you do not fopen() with "rb" or "wb"


And except for systems based on neither Unix nor MS-DOS/Windows, where
the distinction between binary and text files might be even more
significant. C's stdio is very deliberately designed to work on such
systems.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      02-08-2012
Keith Thompson wrote:

> And except for systems based on neither Unix nor MS-DOS/Windows, where
> the distinction between binary and text files might be even more
> significant. C's stdio is very deliberately designed to work on such
> systems.


What do you mean for example Keith? I know for portability to use "rb"
and "wb" but is there even more distinction you say from binary and text
files? I think Unix treats everything like a text file.

Bill


 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      02-08-2012
On Wed, 08 Feb 2012 02:39:35 +0100, Johann Klammer
<(E-Mail Removed)1.net> wrote:

>Bill Cunningham wrote:
>> [snip]
>>
>> So is ftell and fseek for binary files?
>>
>> Bill
>>
>>

>They are _all_ for binary files.
>Text files are a subset of them
>
>...
>
>Except in windows, where newline-carriage-return conversion may be going
>on if you do not fopen() with "rb" or "wb"


Windows is not the only OS where the '\n' does not match the external
representation of the file. For IBM's MVS and its descendents, the
'\n' is usually not present in the file at all.

--
Remove del for email
 
Reply With Quote
 
John Gordon
Guest
Posts: n/a
 
      02-08-2012
In <4f31b446$0$1075$(E-Mail Removed)> "Bill Cunningham" <(E-Mail Removed)> writes:

> What if I was seeking for a number in a column of a text file


That's not what fseek does. fseek goes to a specific location in a file,
as defined by an offset from the beginning or end. It does not "seek" for
specific content in the file.

--
John Gordon A is for Amy, who fell down the stairs
(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      02-08-2012
On Tue, 07 Feb 2012 22:36:23 -0500, Bill Cunningham wrote:

>> And except for systems based on neither Unix nor MS-DOS/Windows, where
>> the distinction between binary and text files might be even more
>> significant. C's stdio is very deliberately designed to work on such
>> systems.

>
> What do you mean for example Keith? I know for portability to use "rb"
> and "wb" but is there even more distinction you say from binary and text
> files?


fseek() and ftell() have some weirdness with files opened in text mode on
platforms where text and binary modes differ. E.g. for Windows:

http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx

For streams opened in text mode, fseek has limited use, because
carriage return–linefeed translations can cause fseek to produce
unexpected results. The only fseek operations guaranteed to work on
streams opened in text mode are:

Seeking with an offset of 0 relative to any of the origin values.

Seeking from the beginning of the file with an offset value
returned from a call to ftell.

Also in text mode, CTRL+Z is interpreted as an end-of-file character
on input. In files opened for reading/writing, fopen and all related
routines check for a CTRL+Z at the end of the file and remove it if
possible. This is done because using fseek and ftell to move within a
file that ends with a CTRL+Z may cause fseek to behave improperly near
the end of the file.

My system with a copy of the C99 standard isn't to hand, but I'm fairly
sure that the standard says something similar, i.e. that fseek/ftell have
well-defined behaviour when used on binary streams but a large amount of
undefined behaviour when used on text streams.

> I think Unix treats everything like a text file.


Unix uses a bare LF ('\n') as its EOL indicator and doesn't have an EOF
indicator, so there's no distinction between text and binary files on Unix
(i.e. there's no difference between "r" and "rb", etc).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the different between byte stream and character stream? dolphin Java 6 03-18-2007 01:58 PM
get stream mode flags from an opened stream Alexander Korsunsky C++ 1 02-17-2007 10:38 AM
How to GET multi-word input from a *file* stream as opposed to a *console* stream? sherifffruitfly@gmail.com C++ 9 04-27-2006 04:14 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Doing readline in a thread from a popen4('rsync ...') stream blocks when the stream ends. Rasmusson, Lars Python 1 04-30-2004 08:10 AM



Advertisments