Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[C] Redirecting stdin <--> file

6 views
Skip to first unread message

Alex Vinokur

unread,
Mar 26, 2003, 6:30:55 AM3/26/03
to
######### C code : BEGIN #########

/*
// ============================================
// Redirecting stdin <--> file
// Sample of usage
// --------------------------------------------
// Alex Vinokur
// mailto:ale...@connect.to
// http://www.simtel.net/pub/oth/19088.html
// --------------------------------------------
// 2003-03-26
// ============================================

// =============
// Windows 2000
// DJGPP 2.03
// GNU gcc 3.2.1
// =============

// =============
// File std_in.c
// =============
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>

#define PRINT_ERROR(msg) \
printf ("FATAL ERROR : %s ---> %s; See %s, #%d\n", msg, strerror (errno), __FILE__, __LINE__); \
exit (1)

#define PRINT2_ERROR(msg1, msg2) \
printf ("FATAL ERROR : %s %s ---> %s; See %s, #%d\n", msg1, msg2, strerror (errno), __FILE__, __LINE__); \
exit (1)


#define PRINT_TEST(msg) \
printf ("[%s, #%3d] Test#%d --> %s\n", \
__FILE__, __LINE__, ++stepNo, msg)


int main ()
{
FILE *fp;
const int fd_stdin = 0;
int fd_infile;
int fd_new_infile;
int fd_save_stdin;
char* infile_name = "file1.in";
int stepNo = 0;
char in_data [512];

printf ("\n");
printf ("\t===========================\n");
printf ("\tRedirecting stdin <--> file\n");
printf ("\tSample of usage\n");
printf ("\t===========================\n");
printf ("\n");


if ((fp = fopen (infile_name, "r")) == NULL)
{
PRINT2_ERROR ("Cannot open file", infile_name);
}

/* =============== */
/* get from screen */
/* =============== */

fd_infile = fileno (fp);
strcpy (in_data, "");
printf ("\tEnter1 : ");
fgets (in_data, sizeof(in_data), stdin);

assert (strlen (in_data) < sizeof (in_data));
assert (in_data [strlen (in_data) - 1] == '\n');
in_data [strlen (in_data) - 1] = 0;

PRINT_TEST (in_data);


/* ====================== */
/* redirect stdin to file */
/* get from file */
/* ====================== */

if ((fd_save_stdin = dup (fd_stdin)) == -1)
{
PRINT_ERROR ("Cannot execute dup");
}
/* Now fd_save_stdin and 0 are descriptors of the screen */
strcpy (in_data, "");
printf ("\tEnter2 : ");
fgets (in_data, sizeof(in_data), stdin);

assert (strlen (in_data) < sizeof (in_data));
assert (in_data [strlen (in_data) - 1] == '\n');
in_data [strlen (in_data) - 1] = 0;

PRINT_TEST (in_data);

/* ---------------------- */
/* Close screen */

if (close (fd_stdin) == -1)
{
PRINT_ERROR ("Cannot close descriptor");
}
/* Now descriptor 0 (fd_stdin) is vacant */
strcpy (in_data, "");
fgets (in_data, sizeof(in_data), stdin);

assert (strlen (in_data) < sizeof (in_data));
assert (strcmp (in_data, "") == 0);

PRINT_TEST (in_data);

/* ---------------------- */
if ((fd_new_infile = dup (fd_infile)) == -1)
{
PRINT_ERROR ("Cannot execute dup");
}
/* Now fd_infile and 0 are descriptors of the infile */
strcpy (in_data, "");
fgets (in_data, sizeof(in_data), stdin);

assert (strlen (in_data) < sizeof (in_data));
assert (in_data [strlen (in_data) - 1] == '\n');
in_data [strlen (in_data) - 1] = 0;

PRINT_TEST (in_data);

/* ---------------------- */
if (close (fd_infile) == -1)
{
PRINT_ERROR ("Cannot close descriptor");
}
/* Now only descriptor 0 (fd_stdin) is descriptor of the infile */
strcpy (in_data, "");
fgets (in_data, sizeof(in_data), stdin);

assert (strlen (in_data) < sizeof (in_data));
assert (in_data [strlen (in_data) - 1] == '\n');
in_data [strlen (in_data) - 1] = 0;

PRINT_TEST (in_data);


/* ================================== */
/* redirect (restore) stdin to screen */
/* ================================== */

/* ---------------------- */
/* Close screen */
if (close (fd_stdin) == -1)
{
PRINT_ERROR ("Cannot close descriptor");
}
/* Now descriptor 0 (fd_stdin) is vacant */
strcpy (in_data, "");
fgets (in_data, sizeof(in_data), stdin);

assert (strlen (in_data) < sizeof (in_data));
assert (strcmp (in_data, "") == 0);

PRINT_TEST (in_data);


/* ---------------------- */
if (dup (fd_save_stdin) == -1)
{
PRINT_ERROR ("Cannot execute dup");
}
/* Now fd_save_stdin and 0 are descriptors of the screen */
strcpy (in_data, "");
printf ("\tEnter3 : ");
fgets (in_data, sizeof(in_data), stdin);

assert (strlen (in_data) < sizeof (in_data));
assert (in_data [strlen (in_data) - 1] == '\n');
in_data [strlen (in_data) - 1] = 0;

PRINT_TEST (in_data);


/* ---------------------- */
if (close (fd_save_stdin) == -1)
{
PRINT_ERROR ("Cannot close descriptor");
}
/* Now only descriptor 0 (fd_stdin) is descriptor of the screen */
strcpy (in_data, "");
printf ("\tEnter4 : ");
fgets (in_data, sizeof(in_data), stdin);

assert (strlen (in_data) < sizeof (in_data));
assert (in_data [strlen (in_data) - 1] == '\n');
in_data [strlen (in_data) - 1] = 0;

PRINT_TEST (in_data);


/* =================== */
printf ("\nGood bye\n");
return 0;
}

######### C code : END ###########


######### Datafile -> file1.in : BEGIN #########
bbb1
bbb2
######### Datafile -> file1.in : END ###########

######### Compiling & Running : BEGIN #########

% gcc -W -Wall std_in.c


$ a.exe

===========================
Redirecting stdin <--> file
Sample of usage
===========================

Enter1 : aaa1
[std_in.c, # 82] Test#1 --> aaa1
Enter2 : aaa2
[std_in.c, #103] Test#2 --> aaa2
[std_in.c, #119] Test#3 -->
[std_in.c, #134] Test#4 --> bbb1
[std_in.c, #149] Test#5 --> bbb2
[std_in.c, #169] Test#6 -->
Enter3 : aaa3
[std_in.c, #186] Test#7 --> aaa3
Enter4 : aaa4
[std_in.c, #203] Test#8 --> aaa4

Good bye

######### Compiling & Running : END ###########

--
=================================
Alex Vinokur
mailto:ale...@connect.to
http://www.simtel.net/pub/oth/19088.html
=================================


0 new messages