1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include <stdio.h>
typedef struct
{
const char *fname;
const unsigned int authlevel;
}FENTRY;
#define NUM_FENTRY 2
const FENTRY flist[]={
{"main.c",3},
{"words.txt",126}
};
void printfile(const char* fname)
{
FILE *file=fopen(fname,"rt");
char c;
while( !feof(file) && (c=fgetc(file))!=EOF ) putchar(c);
fclose(file);
}
int main(void)
{
unsigned int authlevel=5;
char name[32];
int i;
int choice;
printf("Welcome to remote file viewing, guest access mode.\n");
printf("What is your name? ");
fgets(name,34,stdin);
printf("Your authorization level is %03d.\n",authlevel);
for(;;)
{
printf("0: exit\n");
for(i=0;i<NUM_FENTRY;++i)
printf("%d: Level %03d: view \"%s\"\n",i+1,
flist[i].authlevel,flist[i].fname);
if(scanf("%d",&choice)!=1)
{
scanf("%*[^\n]%1*[\n]");
continue;
}
if(choice==0) return 0;
if(choice>NUM_FENTRY)
{
printf("Invalid choice\n");
continue;
}
if(authlevel>=flist[choice-1].authlevel)
printfile(flist[choice-1].fname);
else printf("Error: Authorization level of %03d+ required. "
"Your level is %03d.\n",flist[choice-1].authlevel,
authlevel);
}
return 0;
}
|