473,765 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fread breaks file descriptors opened in "w" mode.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello all,

For some reasons, somewhere in a program, I'd like, if possible, to quickly
parse a whole file before rewinding it and letting the full analysis start.
My problem is that the FILE* I want do parse has been fopen'ed far away
from where I am and I don't know in which MODE my FILE* has been opened.
And additionally, my FILE* may not be a regular file, but a continuous
stream (pipe), in which case it is not rewindable.

So my program basically behaves like this:

void PreParsing( FILE *my_file )
{
/*
* Test if my FILE * is seekable
*/
if( fseek( my_file, 0, SEEK_SET ) == 0 ) {

/*
* If so, go on for the pre-parsing.
*/
//...
fread( buf, BUF_SIZE, 1, my_file );
//...

/*
* Rewind the file.
*/
if( fseek( my_file, 0, SEEK_SET ) != 0 ) {
// unexpected error since my FILE * was expected to be seekable.
}
}
}

* On both Linux 2.6.xx and Solaris 5.10,

when the my_file is a regular file that has been fopen'ed with "w" as mode,
- the first fseek returns 0 meaning success: my_file is seekable
- the fread returns 0 meaning that there was nothing to read: expected
since a file opened in "w" is truncated, hence is empty, and anyhow, cannot
be read!
- the second fseek returns 0 meaning success: my_file could be rewound.

when the my_file is a pipe that has been fopen'ed with "w" as mode,
- the first fseek returns -1 and errno is set to 29 (Illegal seek).
That's exactly what I expected since a pipe cannot be sought.

Ok. So, on Linux 2.6.xx and Solaris 5.10, my code behaves like I expected
for both regular files and pipes.

* On AIX 5.2,

when the my_file is a pipe that has been fopen'ed with "w" as mode,
- the first fseek returns -1 and errno is set to 29 (Illegal seek).
I'm still OK with that.

but...
when the my_file is a regular file that has been fopen'ed with "w" as mode,
- the first fseek returns 0 meaning success: my_file is seekable
- the fread returns 0 meaning that there was nothing to read: expected
since a file opened in "w" is truncated, hence is empty, and anyhow, cannot
be read!
- the second fseek returns -1 and sets errno to 9 (Bad file number)
I'm a little bit surprised by this error.

In fact, after having had a deeper look on that, it appears to me that a
fread attempt on a FILE * opened in "w" mode breaks it since any subsequent
operation (fseek, fwrite and even fclose !) fails with the error 9 (Bad
file number).
So, on AIX , my function fails to restore the FILE * state at its end.
So, I have a few questions:

- a fread on a FILE * opened in "w" will for sure return 0 item, but is it
really expected that it makes the given FILE * totally unusable even for
fclose !?! This behavior has been observed only on AIX. Linux and Solaris
works.
- given a FILE *, is there a better way to guess in which mode it has been
opened than attempting to read or write it and look at errors ?
- given a FILE *, is there a better way to guess if it can be sought than
attempting a fseek on it ?
PS: I Xposted my problem on both comp.lang.c and comp.unix.aix because I
have no idea whether this is an AIX specific problem or if the C norm
specifies explicitly that fread have unpredictable effects on a write-only
file descriptor. Anyhow, my goal is to find a portable solution that uses
as less platform specific stuff as possible.

Thanks for your advices.
Lénaïc ...still puzzled by AIX behavior...
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)

iEYEARECAAYFAkk jPpUACgkQjYEjJA TS6Bi55ACgj+Rbz OhyjDj63G+ciKo0 Iy1B
FuoAn3WsnRc69XX Di2KV0Wt4aOFo0B pf
=In7u
-----END PGP SIGNATURE-----
Nov 18 '08
15 3184
Lénaïc Huard wrote:
Richard Tobin a écrit :
>The other aspect - seekability - is entirely reasonable, but I
don't think you can handle it in standard C. If you are (as you
appear to be) using Posix systems, you can stat the underlying
file descriptor and seek only if it is a regular file.

Sounds to be exactly what I was looking for. I'm still
wondering why I didn't think about stat before. Thanks!
>I seem to recall using at least one system where fseek()
succeeded even on pipes provided the seek was within the
existing stdio buffer, so just because one fseek() succeeds,
it doesn't mean others will.

Sounds to be an excellent reason for not testing the seekability
with fseek!
And it is entirely off-topic on c.l.c, so all such answers are
necessarily suspect. If you ask on comp.unix.progr ammer, however,
you will probably be on-target and get valid answers and criticisms
of those answers.

As an aside, if a file is opened in "w" mode, you can't do many
things to it. Read the C standard.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Nov 19 '08 #11
vi******@gmail. com wrote:
On Nov 19, 12:43 am, Mark McIntyre <markmcint...@T ROUSERSspamcop. net>
wrote:
<snip>
AFAIK reading from a write-only stream is undefined behaviour.

I've recently asked this (or remember asking it), but I believe I
received no answers.
I've also searched the standard for the answer, with no luck. Anyone?
It's undefined behavior by virture of the fact that all I/O takes place
as if by calls to fgetc() and fputc(), and their behavior is only
defined for input and output streams, respectively.
--
Larry Jones

Talk about someone easy to exploit! -- Calvin
Nov 19 '08 #12
On Nov 19, 8:50 pm, lawrence.jo...@ siemens.com wrote:
vipps...@gmail. com wrote:
On Nov 19, 12:43 am, Mark McIntyre <markmcint...@T ROUSERSspamcop. net>
wrote:
<snip>
AFAIK reading from a write-only stream is undefined behaviour.
I've recently asked this (or remember asking it), but I believe I
received no answers.
I've also searched the standard for the answer, with no luck. Anyone?

It's undefined behavior by virture of the fact that all I/O takes place
as if by calls to fgetc() and fputc(), and their behavior is only
defined for input and output streams, respectively.
Ah, I suspected something like that. Thanks!
Nov 20 '08 #13
Lénaïc Huard wrote:
So, I have a few questions:

- a fread on a FILE * opened in "w" will for sure return 0 item, but is it
really expected that it makes the given FILE * totally unusable even for
fclose !?! This behavior has been observed only on AIX. Linux and Solaris
works.
- given a FILE *, is there a better way to guess in which mode it has been
opened than attempting to read or write it and look at errors ?
Once you've eliminated the pipe possibility, dup() the descriptor and
fdopen() the duplicate in r-w mode. Close the old one.

You shouldn't be attempting to read from a stream opened for writing.
That makes no sense and getting undefined, inconsistent results seems
reasonable.

Nov 20 '08 #14
"Gary R. Hook" <gh***@no.spamm ers.netwrites:
Lénaïc Huard wrote:
>So, I have a few questions:
- a fread on a FILE * opened in "w" will for sure return 0 item,
but is it
really expected that it makes the given FILE * totally unusable even for
fclose !?! This behavior has been observed only on AIX. Linux and Solaris
works.
- given a FILE *, is there a better way to guess in which mode it has been
opened than attempting to read or write it and look at errors ?
There's no portable way to do it.
Once you've eliminated the pipe possibility, dup() the descriptor and
fdopen() the duplicate in r-w mode. Close the old one.
dup() and fdopen() are defined by POSIX. Using them is ok as long as
you don't mind limiting your code's portability to POSIX
implementations .
You shouldn't be attempting to read from a stream opened for
writing. That makes no sense and getting undefined, inconsistent
results seems reasonable.
Right, reading from a file opened in "w" mode invokes undefined
behavior.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 20 '08 #15
In article <%V************ *****@flpi149.f fdc.sbc.com>,
Gary R. Hook <gh***@no.spamm ers.netwrote:
>Once you've eliminated the pipe possibility, dup() the descriptor and
fdopen() the duplicate in r-w mode. Close the old one.
What is this supposed to do? If the underlying file descriptor is
opened for writing only, fdopen()ing it read-write won't do you
any good.

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Nov 20 '08 #16

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

Similar topics

0
1349
by: Masiar Farahani | last post by:
Hi, Is there a option in PythonWin to write files in unix file mode? Regards Masiar
7
3801
by: Scott Chapman | last post by:
Hi! I'd like a "file" on the Linux box to actually be the input and output of a Python script. Anything written to the file would be sent to a database and anything read from the file would come from the database. I know how to do the database end of this but I'm not sure if a script can be hooked to a "file" transparently. The script would probably have to be run as a daemon. I doubt there's enough magic in the Linux world to make...
6
1898
by: Uwe Mayer | last post by:
Hi, when extending a build in class, what does the constructor __init__(...) have to return? and how does the constructor call its base-class construtor? (or is this done automatically?) I want to derive from "file" to create a class that reads record from a binary file:
11
2170
by: David Morgenthaler | last post by:
How does one overide the iterator implied by the construct "for line in file:"? For example, suppose I have a file containing row,col pairs on each line, and I wish to write a subclass of file that will transform these to x,y coordinates using an affine transform. I'd like it to look something like this (but this clearly doesn't work): class myFile(file): def __init__(self,name,affine):
1
10634
by: Matt | last post by:
When we click Browse button, <input type="FILE">, can we set the default of the directory to open?? For example, C:\MYFILES are open when the file open dialog is opened. please advise. thanks!!
1
2429
by: jingkun Hu via .NET 247 | last post by:
(Type your message here) I'm upgrading a VC++ 6 project to VC.NET 2003. The compiling isOK but not linking. The error message is "mfc42.lib cannot beopened". I looked through every setting of compiling and linkingand didn't find mfc42.lib in there. I understand that VC.NET2003 uses mfc71.lib. But how does the environment requiresmfc42.lib? Please help! -------------------------------- From: jingkun Hu ----------------------- Posted...
4
5829
by: Joe Lester | last post by:
I'm trying to figure out what the optimal Postgres configuration would be for my server (with 200 connecting clients, even though I'd really like to get it up to 500). I've got a 700 MHz eMac running Mac OS 10.3.2 (Panther) with 512 MB of RAM. I've messed around with some settings but I'm still getting an occasional "out of file descriptor" error, especially when performing a VACUUM. Like so... 2004-04-13 23:30:05 LOG: out of file...
1
13050
by: weird0 | last post by:
System.Data.SqlClient.SqlException: An attempt to attach an auto- named database for file "Location" failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. I am getting the above the exception while connecting to the db and i have written a web-service... i.e. a webmethod. What do I do? Why am i getting this error ? How can i remove this
9
3169
by: hstagni | last post by:
I tried to use fseek in a file opened for writing: ------ BEGIN ------- int main() { char c; FILE *fp; fp=fopen("texto", "wb"); putc('a', fp); putc('b', fp);
2
7953
by: jjlagtap | last post by:
Hey everyone When I try to open a file i get the Exception listed below. The weird thing is it works when I run the web app locally and when i use a remote server and open a file on my computer. When someone else uses the web app and tries to open a file using the remote server the error below happens. any help? **Code** C# - filepath is a valid file name which is passed via the form. System.IO.BinaryReader br = new...
0
9568
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
9399
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
10163
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...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9957
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
8832
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...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
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();...
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.