stream functions

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

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
 
J

John Gordon

In said:
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?
 
B

Bill Cunningham

John said:
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
 
J

Johann Klammer

Bill said:
[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"
 
K

Keith Thompson

Johann Klammer said:
Bill said:
[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.
 
B

Bill Cunningham

Keith said:
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
 
B

Barry Schwarz

Bill said:
[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.
 
J

John Gordon

In said:
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.
 
N

Nobody

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/library/75yw9bf3(v=vs.71).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).
 
B

Bill Cunningham

John said:
In <[email protected]> "Bill Cunningham"


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.

Ok so with a page of text, like a man page for example; scanning that
text would best be done with what? scanf or fscanf?

Bill
 
J

John Gordon

In said:
Ok so with a page of text, like a man page for example; scanning that
text would best be done with what? scanf or fscanf?

I'd use fgets to read the lines of text and store them in an array.

After that, it depends how the information is formatted. If the text
is arranged in columns, sscanf might be a good candidate for picking
out individual pieces of information. Or if it's plain free-form text,
strstr might be a better choice.
 
B

Bill Cunningham

John said:
I'd use fgets to read the lines of text and store them in an array.

After that, it depends how the information is formatted. If the text
is arranged in columns, sscanf might be a good candidate for picking
out individual pieces of information. Or if it's plain free-form
text,
strstr might be a better choice.

Ok so to scan the entire page and get results, fgets in a loop would be
best then? The text file I want to write will have columns of data. I hope
I'm clear. Loops is something I've never really caught onto in C. Especially
loops within loops.

Bill
 
J

John Gordon

In said:
Ok so to scan the entire page and get results, fgets in a loop would be
best then?

To scan the page, you really need to do two things:

1. Read the information and store it in some organized way.
2. Inspect the stored information.

fgets is good for #1. For #2, you'll need something like sscanf or strstr.
Loops is something I've never really caught onto in C.

Loops are a basic concept in almost any programming language; perhaps
you should brush up on them in C.
 
H

Heinrich Wolf

....
The text file I want to write will have columns of data.
If the columns are e.g. padded with spaces, then you might do this:
if (fgets(Buffer, sizeof(Buffer), fp))
{
*Value = 0;
strncat(Value, Buffer + ValueColumn, sizeof(Value) - 1);
}

If the columns are e.g. delimited with tab, then you might do that:
cp = strtok(Buffer, "\t");
Column = 0;
while (cp && Column < NUMCOLUMNS)
{
*Value[Column] = 0;
strncat(Value[Column], cp, sizeof(Value[Column]) - 1);
cp = strtok(NULL, "\t");
Column ++;
}
....
 
B

Bill Cunningham

Heinrich said:
If the columns are e.g. padded with spaces, then you might do this:
if (fgets(Buffer, sizeof(Buffer), fp))
{
*Value = 0;
strncat(Value, Buffer + ValueColumn, sizeof(Value) - 1);
}

If the columns are e.g. delimited with tab, then you might do that:
cp = strtok(Buffer, "\t");
Column = 0;
while (cp && Column < NUMCOLUMNS)
{
*Value[Column] = 0;
strncat(Value[Column], cp, sizeof(Value[Column]) - 1);
cp = strtok(NULL, "\t");
Column ++;
}
...

Thanks for the code example. I will keep it and study it. I recognize
*Value to be a derefence but I'm still not exactly sure when to use those
still at this time. My code is much much simpler.

Bill
 
J

Jorgen Grahn

On Mon, 2012-02-13, Gordon Burditt wrote:
[attribution lost]
It is very common for a program to fseek() to the end of a log file
(or sometimes a data file) to add messages to it. Using fopen(name,
"a") is not always useful as you might sometimes write to somewhere
other than the end.

Explain, please. My manual says about "a":

Open for appending (writing at end of file). The file is created
if it does not exist. The stream is positioned at the end of the
file.

Someone else might have appended to the file after you fopen()ed it,
but the same is true for fseek().

/Jorgen
 
N

Nick Keighley

Loops are a basic concept in almost any programming language;

I think some functional languages manage without them. Certainly Lisp,
C++, Python programs can be written that don't use loops.
perhaps you should brush up on them in C.

to put it mildly! Loops are absolutly fundamental to a language like C.
 
N

Nick Keighley

    Ok so with a page of text, like a man page for example; scanning that
text would best be done with what? scanf or fscanf?

scanf() reads from stdin fscanf() reads froma file. What would you
think?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top