How read a line of a file from C-shell?

Kim Christian Madsen kimcm at diku.dk
Thu Nov 1 10:25:25 AEST 1990


nwosuck at aix.aix.kingston.ibm.com (Kingsley Nwosu) writes:

->In article <8900 at ncar.ucar.edu>, tparker at bierstadt.scd.ucar.edu (Tom Parker) writes:
->> 
->> 
->> I tried something like      foreach line(`cat file`)
->> 
->> The only method I've gotten to work is this inelegant structure: 
->> 
->>       set line = `head -$n | tail -1`   # Read n-th line
->> 
->> Does anyone have better ways to do file I/O in a C-shell script?
->> 

->On my IBM AIX/370 OS I am able to do:

->cat <filename> | awk '{FS=CR; print $1}'

->This prints each line. You can then pipe the output to the desired function,
->if that is what you want.

If your system is running system V use the command line(1)

otherwise you might want to implement the following code:

----------line.c-----Public Domain Version---------------
#include <stdio.h>

main()
{
	char	line[BUFSIZ];

	fgets(line,BUFSIZ,stdin);
	fputs(line,stdout);
}
----------------------------------------------------------
For more flexibility you can use the following program as well:

--------gets.c------Public Domain Version-----------------
/*
 * gets.c:
 *	Read a line from input and return the read data, if empty return
 *	the default value given as arguments. No interpretation of the
 *	arguments is performed, however it is allowable not to specify
 *	any arguments at all, in which case nothing will be returned.
 *
 * (c) Copyright 1988, Kim Chr. Madsen
 *     All Rights Reserved
 */

#include <stdio.h>

static char *sccsid = "@(#)gets.c	1.1 90/11/01 00:22:43";

main(argc,argv)
int	argc;
char	*argv[];
{
	char	buf[BUFSIZ];
	int	def;

	gets(buf);
	if (strlen(buf)) printf("%s\n",buf);
	else {
		for (def=1; def<argc; def++)
			printf("%s ",argv[def]);
		putchar('\n');
	}
}
--------------------------------------------------------------------

Hope this helps.....

Kim Chr. Madsen



More information about the Comp.unix.questions mailing list