How's it going:
How do you use fopen() to establish a stream to console I/O (keyboard)? What I needed to do is read in data from the console I/O and then send it elsewhere using fputs. I just don't know what the first argument for fopen("here", r) would be. Oh, and this is an event driven program (chatting); and the fopen will be part of a read event handler. I am supposed to read data from the console using fgets and then send it out to the other recipient using fputs. Thanks.

Dani AI

Generated

As described, there’s no need to call fopen for keyboard input — the standard streams are already open when the program starts. As points out, use stdin (file descriptor 0) for reads and stdout/stderr for writes. In an event-driven chat program the usual pattern is to watch the descriptor returned by fileno(stdin) with select/poll (or the platform’s event API) and only call fgets when the descriptor is readable; that avoids blocking the event loop.

A compact POSIX-style example (replace sockfd with the connected socket fd):

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/socket.h>

char buf[512];
for (;;) {
    fd_set rfds;
    FD_ZERO(&rfds);
    FD_SET(fileno(stdin), &rfds);
    FD_SET(sockfd, &rfds);
    int maxfd = (sockfd > fileno(stdin) ? sockfd : fileno(stdin)) + 1;

    if (select(maxfd, &rfds, NULL, NULL, NULL) <= 0) break;
    if (FD_ISSET(fileno(stdin), &rfds)) {
        if (fgets(buf, sizeof buf, stdin) == NULL) break;
        send(sockfd, buf, strlen(buf), 0); /* or use write() */
    }
    if (FD_ISSET(sockfd, &rfds)) {
        /* handle incoming network data */
    }
}

Notes and pitfalls: wrapping a socket fd with fdopen to use fputs is possible (FILE *f = fdopen(sockfd,"w")) but watch buffering — call fflush or set the stream to unbuffered/line-buffered to avoid delayed sends. Avoid mixing stdio calls and low-level send/write on the same descriptor without care. If the program needs the actual controlling terminal even when stdin is redirected, open /dev/tty (POSIX) or CONIN$/CONOUT$ on Windows. Finally, console I/O on Windows doesn’t behave like a socket fd for select — use a separate reader thread or Windows console APIs for a responsive event loop.

Recommended Answers

All 2 Replies

How's it going:
How do you use fopen() to establish a stream to console I/O (keyboard)? What I needed to do is read in data from the console I/O and then send it elsewhere using fputs. I just don't know what the first argument for fopen("here", r) would be. Oh, and this is an event driven program (chatting); and the fopen will be part of a read event handler. I am supposed to read data from the console using fgets and then send it out to the other recipient using fputs. Thanks.

Its not necessary to use fopen to open the console keyboard -- its already open when the program starts. You can use stdout, stdin or stderr.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.