473,666 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(part 27) Han from China answers your C questions

fread breaks file descriptors opened in "w" mode.
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.
...
- 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 ?
Yes, once you break out of ANSI/ISO C, all things are possible.

You have posted an on-topic question to comp.lang.c. See version 2.0 of my topicality
FAQ if anyone should tell you otherwise.
Search: "Han from China's comp.lang.c topicality FAQ (version 2.0)"

I have written some code for you to study and have given you a
demonstration below it. You may want to include full error-checking.

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

void
PreParsing(FILE *my_file)
{
int fd, fl;
struct stat st;

// if (!my_file) { ... }
fd = fileno(my_file) ;
fl = fcntl(fd, F_GETFL, 0);
switch(fl & O_ACCMODE) {
case O_RDONLY: puts("read-only");
break;
case O_WRONLY: puts("write-only");
break;
case O_RDWR: puts("read-write");
break;
}

if(fl & O_APPEND)
puts("append");

fstat(fd, &st);
#ifdef S_ISREG
if(S_ISREG(st.s t_mode)) puts("regular") ;
#else
if((st.st_mode & S_IFMT) == S_IFREG) puts("regular") ;
#endif
else
#ifdef S_ISFIFO
if(S_ISFIFO(st. st_mode)) puts("fifo");
#else
if((st.st_mode & S_IFMT) == S_IFIFO) puts("fifo");
#endif

}

int
main(int argc, char **argv)
{
FILE *file;

if(argc == 3) {
file = fopen(argv[1], argv[2]);
if(file)
PreParsing(file );
}

return 0;
}

$ echo test test
$ ./clc test r
read-only
regular
$ ./clc test w
write-only
regular
$ ./clc test a
write-only
append
regular
$ ./clc test a+
read-write
append
regular
$ mkfifo pipe
$ ./clc pipe r+
read-write
fifo
$ ./clc pipe r & #blocks until writer
$ ./clc pipe w
read-only
write-only
fifo
fifo

Yours,
Han from China

Il mittente di questo messaggio|The sender address of this
non corrisponde ad un utente |message is not related to a real
reale ma all'indirizzo fittizio|person but to a fake address of an
di un sistema anonimizzatore |anonymous system
Per maggiori informazioni |For more info
https://www.mixmaster.it

Nov 18 '08 #1
0 1366

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

Similar topics

1
1953
by: Nomen Nescio | last post by:
A new learner's question Flash Gordon wrote: To the OP, please be aware that Flash Gordon has a history of either deliberately misleading and "miss-informing" posters to this newsgroup or providing facile answers with which to amuse himself. In this case, both. Facile because Flash Gordon did not tell you substantially anything more than your compiler had told you. Misleading
0
1473
by: Nomen Nescio | last post by:
what is the type for this call? Neo said: Neo, buddy, I've decided to give you a little summary for reference so that you don't have to sift through excess verbiage, cryptic meanderings, and irrelevant matters of naming to make sense of this thing. Please forgive any typos on my part.
0
1290
by: Nomen Nescio | last post by:
No format string passed to variable argument list function Adam said: Then Adam laters says: Then Adam later says:
2
1498
by: Chris Dollin | last post by:
Joachim Schmitz wrote: I think that remark is an ill-considered and unhelpful generalisation. -- "We dance, and the worlds melt away." - Curved Air, /Metamorphosis/ Hewlett-Packard Limited registered no: registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
2
1531
by: Borked Pseudo Mailed | last post by:
float and integers c.lang.myself@gmail.com said I will be making assumptions here about your system that will infuriate the pedants, but I don't care. Given your X-HTTP-UserAgent string, it is highly probable that these assumptions are valid. Whether you have included stdio.h or not, the float argument to printf() is promoted to a double, since even with a prototype
1
1479
by: George Orwell | last post by:
its urgent.pls help me. mona said: mona said: Hey again, mona. Please check the thread "(part 21) Han from China answers your C questions", in which I gave you a program with
16
1800
by: Borked Pseudo Mailed | last post by:
Error handling in C Nick Keighley said: ... ... To the OP, please be aware that Nick Keighley is a known troll and is deliberately providing you with unsound advice. You asked for error handling, not error generation (or much worse).
2
2197
by: Nomen Nescio | last post by:
What is this? Eric said: That looks like either someone's poor coding or someone's poor pasting for an implementation of offsetof() from stddef.h. The (char *) leads me to think that there's plenty of missing context, so post the surrounding code, and we'll be happy to jerk ourselves off analyzing the code for you and telling you why it won't work on the control system for your Tomahawk missile.
1
1593
by: George Orwell | last post by:
File Seeking / Overwriting bytes Martien Verbruggen said: And this is, of course, also not right. fseek() does support SEEK_END on text streams. If you want to know why, please start a thread on comp.lang.c. Also, you could read "(part 18) Han from China answers your C questions".
0
8869
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...
1
8551
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
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
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
6198
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
5664
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();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
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.