CSC 272 - Software II: Principles of Programming Languages

Dr. R. M. Siegfried

A Program in C to Sort an array of integers

#include	<stdio.h>
#include	<stdlib.h>

#define		FILENAMELEN	30

/*
 * main() -	A program to display a file on the screen
 */
int	main(void)
{
	FILE	*infile;	/* A pointer to the file */
	char	filename[FILENAMELEN];/* A character string to hold
						the name */
	int	inchar;  /* Even though it's an integer,
				we use it to read a character*/

	/* Get the file name */
	printf("Enter file name ->\t");	/* Print the prompt */
	gets(filename);  /* Read a string until the next blank
					tab or newline */

	/*  Standard idiom for opening a file */
	if ((infile = fopen(filename, "r")) == NULL)	{
		printf("Cannot open %s\n", filename); /* %s is where filename
                					goes in the output */
		exit(1);	/* End execution with an error code */
	}

	/*  Standard idiom for reading the whole file */
	/* Read a character until end of file */
	while((inchar = getc(infile)) != EOF) 
		putchar(inchar); /* Display a character */

	fclose(infile); /* Close the file */
	return(0);	/* End without error */
}

[Back to the C Program Index]