All pastes #370828 Raw Edit

Miscellany

public c v1 · immutable
#370828 ·published 2007-02-24 22:23 UTC
rendered paste body
#include <unistd.h>#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>int main(int argc, char **argv) {	char* ivalue = "index"; // Set default linked list filename	char* nvalue = "filenames"; // Set default file array filename	//char* currentFile = NULL; // Current file we're processing	int index; // Index of last element in cmd line options	int numFiles = 0; // Number of files to index	int cmdo; // Command line option	FILE* fp; // File pointer, used by all the files we open	while ((cmdo = getopt (argc, argv, "hi:n:")) != -1) {		switch(cmdo)		{		case 'i': // Specify what linked list file we're using			ivalue = optarg;			break;		case 'n': // Specify what file array filename to use			nvalue = optarg;			break;		case 'h': // Help, usage			printf("indexfile [-i FILE] [-n FILE] FILE ...\nOPTIONS\n\t-i FILE\n\t\tWrite the linked list to FILE. This overrides the default file name \"index\".\n\t-n FILE\n\t\tWrite the array of file names to FILE. This overrides the default file name \"filenames\".\n");			return(0);		case '?':			fprintf(stderr, "Unrecognised option: -%c\n", optopt);			return(1);			break;		default:			break;		}	}	numFiles = argc-optind;	char* files[argc-optind];	*files = *(argv+optind); // Create an array to store all the filename values	for (index = 0; index < numFiles; index++) {		fp = fopen(files[index],"r");		if (fp) {			printf("Successfully opened input file %s\n",files[index-optind]);			fclose(fp);		} else {			fprintf(stderr,"Can't open input file %s\n",files[index-optind]);		}	}	return(0);}