1830882 Members
2349 Online
110017 Solutions
New Discussion

C programming question

 
SOLVED
Go to solution
Jeff Picton
Regular Advisor

C programming question

Hi

Does anyone know the C code to print the integer representation on the EOF character to standard out ?
11 REPLIES 11
Graham Cameron_1
Honored Contributor

Re: C programming question

# include
main () {
printf ("%d\n", VEOF) ;
}

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Mark Grant
Honored Contributor

Re: C programming question

#include
main(){
printf("%d\n",EOF);
}


However, you'll find this defined in the system "stdio.h" include file.
Never preceed any demonstration with anything more predictive than "watch this"
Elmar P. Kolkman
Honored Contributor

Re: C programming question

I don't think those will work... The EOF character (by default D, ASCII 4) is something different from the EOF value defined in stdio.h, which is used for things like comparison with the result code of the read call.

What you need to do is do a gtty, get the EOF value from there and then print it like the previous replies, using a %d for printf.

So, first start reading the manual page stty(2), stty(7), termio(7). I don't have time now to create the program, perhaps later.
Every problem has at least one solution. Only some solutions are harder to find.
Jeff Picton
Regular Advisor

Re: C programming question

Hi

Many thanks for the answers. I didn't know how simple the solution was - the key being the C library.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: C programming question

Actually Elmar is the only one on the right track althogh Graham gets the right answer (in the default case) quite by accident.

You have to ask the process itself (actually the controlling terminal's port). The EOF value can actually change "on the fly". The 'trick' is to do an ioctl on stdin requesting the termio structure be loaded. You then examine an array of "control characters" within this structure. VEOF refers refers to the array index that hold the EOF control character. VEOF is defined to be 4 and because by default the EOF is a Control-D (ASCII 4), Graham's solution works (most of the time) completely by accident.

------------------------------------------
Here's the proper method:

#include
#include

#define assign_errno(x) ((errno != 0) ? errno : (x))

extern int errno;

#define STDIN_FDES 0

int main()
{
int cc = 0;
struct termio t;

cc = ioctl(STDIN_FDES,TCGETA,&t);
if (cc == 0)
{
printf("%d\n",(int) t.c_cc[VEOF]);
}
else
{
cc = assign_errno(-1);
(void) fprintf(stderr,"Ioctl failed (%d)\n",cc);
}
return(cc);
}
-----------------------------------------
Incidently, you could add to this code by assigning a new value to t.c_cc[VEOF] and then doing an ioctl(STDIN_FDES,TCSETA,&t) and that would assign another character to the EOF function. This is what is done when you issue a stty command.
If it ain't broke, I can fix that.
Jeff Picton
Regular Advisor

Re: C programming question

Hi

Thanks for that information,Clay. That makes very interesting reading.
Graham Cameron_1
Honored Contributor

Re: C programming question

Wow
Just call me Uriah Heep ...

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
A. Clay Stephenson
Acclaimed Contributor

Re: C programming question

Oops, before I get dinged by the C Programming Police the line:

printf("%d\n",(int) t.c_cc[VEOF]);
should be:
(void) printf("%d\n",(int) t.c_cc[VEOF]);

because we are explicitly ignoring the result (the number of bytes transmitted) returned by the printf() function. Both versions work perfectly well; I'm just ultra nit-picky --- in all things except English grammar.

If it ain't broke, I can fix that.
Mark Grant
Honored Contributor

Re: C programming question

A clay,

After reading Elmars post, I tried exactly the same thing you mention here with the ioctl. For the life of me I could not get the thing to compile. cc_c was not a struct member. It plainy is though.

Any thoughts?
Never preceed any demonstration with anything more predictive than "watch this"
A. Clay Stephenson
Acclaimed Contributor

Re: C programming question

First make sure that you
#include
That actually includes but your problem is simply dumb pilot error:

It should be t.c_cc[VEOF] NOT t.cc_c[VEOF]. In C, spelling counts much more than it does in English.
If it ain't broke, I can fix that.
Mark Grant
Honored Contributor

Re: C programming question

Damn, I wish I'd posted this as a new thread, I could have assigned you the 10 points. I DID use cc_c and not c_cc!!

Good spot there A.Clay :)
Never preceed any demonstration with anything more predictive than "watch this"