home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (C) 1990 MetaWare Incorporated; All Rights Reserved */
-
- /**** name=fabs ****/
- #define main TEST_fabs
-
- #include <stdio.h>
- #include <math.h>
-
- void main() {
- double absolut, real = -32.234763;
-
- absolut = fabs(real);
- printf("The absolute value of %f is: %f\n",
- real, absolut);
- }
- /* End fabs */
- #undef main
- /**** name=fclose ****/
- #define main TEST_fclose
-
- /*
- The program fragment below attempts to open test.dat for reading. It
- later attempts to close test.dat prior to opening fclose.tmp for writing
- (using the same FILE variable). For this example to work, you must have
- a file named test.dat in your current directory.
- */
-
- #include <stdio.h>
- #define FAILURE (-1)
-
- int main() {
- FILE *FP;
-
- if ((FP = fopen("test.dat", "r")) == NULL) {
- perror("fopen of test.dat");
- return FAILURE;
- }
- if (fclose(FP) != 0)
- perror("fclose of test.dat");
- if ((FP = fopen("fclose.tmp", "w")) == NULL) {
- perror("fopen of fclose.tmp");
- return FAILURE;
- }
- puts("fopen and fclose were successful.");
- }
- /* End fclose */
- #undef main
- #undef FAILURE
- /**** name=_fdopen ****/
- #define main TEST__fdopen
-
- #include <fcntl.h>
- #include <io.h>
- #include <share.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- void main(void) {
- FILE *fp;
- int pan;
- int c;
-
- pan = _sopen("test.dat",
- O_RDONLY | O_TEXT,
- _SH_DENYWR
- );
- if (pan != -1)
- printf("File test.dat exists, handle=%d\n", pan);
- else
- printf("File test.dat cannot be sopened,"
- " error code=%d\n",pan);
- if((fp = _fdopen(pan, "r")) == NULL) {
- perror("Error opening test.dat");
- return;
- }
- /* Get and print characters until the end of file. */
- while((c = fgetc(fp)) != EOF)
- printf("%c", c);
-
- /* Close the file. Now any other process can read and write to the file
- test.dat until some other process puts a lock on it. */
- fclose(fp);
- }
- /* End _fdopen */
- #undef main
- /**** name=fgetc ****/
- #define main TEST_fgetc
-
- /*
- This program prints out test.dat character by character using fgetc.
- */
- #include <stdio.h>
-
- void main() {
- FILE *FP;
- signed char c;
-
- if ((FP = fopen("test.dat", "r")) == NULL)
- perror("fopen of test.dat");
- while (!feof(FP)) {
- c = fgetc(FP);
- fputc(c, stdout);
- }
- }
- /* End fgetc */
- #undef main
- /**** name=_fgetchar ****/
- #define main TEST__fgetchar
-
- /*
- This program copies standard input character by character to
- fgetchar.tmp and to standard output. Similar examples are given for
- fgetc and getc, pointing out the differences among the functions.
- */
- #include <stdio.h>
-
- void main() {
- FILE *FP1;
-
- if ((FP1 = fopen("fgetchar.tmp", "w")) == NULL)
- perror("Cannot open fgetchar.tmp.");
- printf("Please enter text, ending"
- " with the ENTER key.\n\n");
- while ('\n' != fputc(fputchar(fgetchar()), FP1));
- }
- /* End _fgetchar */
- #undef main
- /**** name=fgetpos ****/
- #define main TEST_fgetpos
-
-
- #include <stdio.h>
-
- void print_it(FILE *FP) {
- int c;
- fpos_t pos1,pos2;
- for (;;) {
- fgetpos(FP, &pos1);
- do { /* skip first line */
- c = fgetc(FP);
- if (c == EOF) return;
- } while (c != '\n');
- do { /* print second line */
- c = fgetc(FP);
- if (c == EOF) return;
- printf("%c", c);
- } while (c != '\n');
-
- fgetpos(FP, &pos2);
- fsetpos(FP, &pos1); /* reposition to first line */
-
- do { /* print first line */
- c = fgetc(FP);
- if (c == EOF) return;
- printf("%c", c);
- } while (c != '\n');
- fsetpos(FP, &pos2);
- }
- }
-
- void main() {
- FILE *FP;
-
- if (((FP = fopen("test.dat","r")) == NULL))
- return;
- print_it(FP);
- fclose(FP);
- }
- /* End fgetpos */
- #undef main
- /**** name=fgets ****/
- #define main TEST_fgets
-
- /*
- The program below prints test.dat line by line. This example
- illustrates the use of fgets, and fopen.
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #define LINESIZE (80)
- void main() {
- FILE *FP;
- char line[LINESIZE];
- if ((FP = fopen("test.dat", "r")) == NULL) {
- perror("cannot open test.dat");
- return;
- }
- while (fgets(line, LINESIZE, FP)) {
- /* fgets returns NULL (FALSE) when it */
- /* cannot get any more characters. */
- fputs(line, stdout);
- }
- }
- /* End fgets */
- #undef main
- #undef LINESIZE
- /**** name=__FILE__ ****/
- #define main TEST___FILE__
- /* End __FILE__ */
- #undef main
- /**** name=_filelength ****/
- #define main TEST__filelength
-
- /*
- This program determines the length of a file.
- */
-
- #include <stdio.h>
- #include <io.h>
-
- FILE *stream;
- long length;
-
- void main() {
- /* Must open file first. */
- stream = fopen("test.dat", "r");
- length = _filelength (fileno(stream));
- if (length == -1L)
- puts("Could not find file test.dat.");
- else
- printf("test.dat is %ld bytes long.\n", length);
- }
-
- /* End _filelength */
- #undef main
- /**** name=_fileno ****/
- #define main TEST__fileno
-
- /*
- Program to obtain the predefined file handles.
- */
- #include <stdio.h>
-
- void TEST__fileno() {
-
- puts("Predefined file handles:");
- puts("\nDevice handle");
- printf("Standard input %d\n", _fileno(stdin));
- printf("Standard output %d\n", _fileno(stdout));
- printf("Standard error %d\n", _fileno(stderr));
- }
- /* End _fileno */
- #undef main
- /**** name=_fmalloc ****/
- #define main TEST__fmalloc
-
-
- #include <stdio.h>
- #include <malloc.h>
- #include <dos.h>
-
- void main() {
- _Far char *new_array;
-
- new_array = _fmalloc(15000 * sizeof(long));
-
- if (new_array == NULL) {
- printf("_fmalloc: Not enough memory"
- " for 15,000 longs\n");
- new_array = _fmalloc(5000 * sizeof(long));
- if (new_array == NULL)
- printf("_fmalloc: Not enough memory"
- " for 5,000 longs\n");
- else
- printf("_fmalloc: Allocated 5,000 longs"
- " at %x:%lx\n",
- _FP_SEG(new_array),
- _FP_OFF(new_array));
- }
- else
- printf("_fmalloc: Allocated 15,000 longs"
- " at %x:%lx\n",
-
- _FP_SEG(new_array),
- _FP_OFF(new_array));
- }
- /* End _fmalloc */
- #undef main
- /**** name=fopen ****/
- #define main TEST_fopen
- /* End fopen */
- #undef main
- /**** name=_FP_X ****/
- #define main TEST__FP_X
-
- /*
- This program displays the segment and offset of a far pointer.
- */
- #include <dos.h>
- #include <stdio.h>
-
- void main() {
- struct ovly {
- int offs;
- short segs;
- };
- union {
- _Far char *a;
- struct ovly q;
- } x;
- unsigned int seg, off;
-
- x.q.offs = 320; /* Line two, column one. */
- x.q.segs = 0x1C; /* Phar Lap screen segment. */
- puts("\nchar *a is stored at: ");
-
- seg = _FP_SEG(x.a);
- off = _FP_OFF(x.a);
- printf("Segment: 0x%.4x\n"
- "Offset : 0x%.4x\n", seg, off);
- }
- /* End _FP_* */
- #undef main
- /**** name=fprintf ****/
- #define main TEST_fprintf
-
- /*
- Results are written to file fprintf.tmp.
- */
- #include <stdio.h>
- #define FAILURE (-1)
-
- int main() {
- FILE *FP;
- char s[14] = "like this one", c = '*';
- int i = 10, x = 255, o = 45;
- double d = 3.1415927, nf = -3.449123;
-
- if ((FP = fopen("fprintf.tmp", "w")) == NULL) {
- perror("Cannot open fprintf.tmp.");
- return FAILURE;
- }
- puts("Results are in fprintf.tmp.");
- fprintf(FP,"fprintf prints strings (%s),\n", s);
- fprintf(FP,"decimal numbers(%d),", i);
- fprintf(FP," hex numbers(%04X),\n", x);
- fprintf(FP,"floating-point numbers (%+.4e)\n", d);
- fprintf(FP,"percent signs(%%),\n");
- fprintf(FP,"characters (%-5c),\n", c);
- fprintf(FP,"negative floating-points"
- " (% 010.2e)\n", nf);
- fprintf(FP,"and even octal numbers (%-+#5o).", o);
- fclose(FP);
- }
- /* End fprintf */
- #undef main
- #undef FAILURE
- /**** name=fputc ****/
- #define main TEST_fputc
- /* End fputc */
- #undef main
- /**** name=_fputchar ****/
- #define main TEST__fputchar
- /* End _fputchar */
- #undef main
- /**** name=fputs ****/
- #define main TEST_fputs
- /* End fputs */
- #undef main
- /**** name=fread ****/
- #define main TEST_fread
-
- /*
- The program below copies test.dat, a block at a time, to fread.tmp.
- This example illustrates the use of fread and fwrite.
- */
- #include <stdio.h>
- #define FAILURE (-1)
- #define BLOCKSIZE (128)
- #define MAXBUF (256)
-
- int main() {
- char buf[MAXBUF];
- FILE *FP1, *FP2;
- size_t i,j;
-
- puts("Testing fread...\n");
- if ((FP1 = fopen("test.dat", "r")) == NULL) {
- perror("Cannot open test.dat");
- return FAILURE;
- }
- if ((FP2 = fopen("fread.tmp", "w")) == NULL) {
- perror("Cannot open fread.tmp.");
- return FAILURE;
- }
- i = fread(buf, sizeof(char), BLOCKSIZE, FP1);
- j = fwrite(buf, sizeof(char), i, FP2);
-
- printf("i = %d, and j = %d.\n",i,j);
- if (fclose(FP1) == 0)
- puts("File test.dat is now closed.");
- else puts("Unable to close file test.dat.");
- if (fclose(FP2) == 0)
- puts("File fread.tmp is now closed.");
- else puts("Unable to close file fread.tmp.");
- }
- /* End fread */
- #undef main
- #undef FAILURE
- #undef BLOCKSIZE
- #undef MAXBUF
- /**** name=freopen ****/
- #define main TEST_freopen
-
- #include <stdio.h>
-
- void main() {
- printf("Testing freopen."
- " Output is in freopen.tmp\n");
- freopen("freopen.tmp", "w", stdout);
- printf("This will not appear on the screen.\n");
- printf("It will appear in the file freopen.tmp.\n");
- freopen("con", "w", stdout);
- printf("freopen test is finished.");
- }
- /* End freopen */
- #undef main
- /**** name=frexp ****/
- #define main TEST_frexp
-
- #include <stdio.h>
- #include <math.h>
-
- void main() {
- int exp;
- double x, num=73.0;
-
- puts("Testing frexp...\n");
- x = frexp(num, &exp);
- printf("For the number %e.\n",num);
- printf("Fractional part = %e,\n"
- "Exponent part = %d.\n", x, exp);
- }
-
- /* End frexp */
- #undef main
- /**** name=fscanf ****/
- #define main TEST_fscanf
-
- #include <stdio.h>
-
- void main() {
- FILE *FP;
- char s1[11], s2[19] = "but missed eleven.";
- char s3[19] = "but missed eleven.";
- int i, j;
-
- puts("Testing fscanf...\n");
- if ((FP = fopen("fscanf.dat", "r")) == NULL) {
- perror("Cannot open fscanf.dat.");
- return;
- }
- fscanf(FP,"%11c%d %%%d %[^z]z", s1, &i, &j, s2);
- printf("%11s%d, %s%s\n", s1, i,
- j == 11 ? "11, " : "",s2);
- fgets(s1,11,FP);
- fscanf(FP,"%11c%d %%%d %[^z]z", s1, &i, &j, s3);
- printf("%11s%d, %s%s\n", s1, i,
- j == 11 ? "11, " : "",s3);
- }
- /* End fscanf */
- #undef main
- /**** name=fseek ****/
- #define main TEST_fseek
-
- /*
- This program prints the last ten bytes of test.dat
- */
-
- #include <stdio.h>
- #define FAILURE (-1)
-
- int main() {
- FILE *FP;
- char c;
-
- if (((FP = fopen("test.dat","r")) == NULL))
- return FAILURE;
-
- fseek(FP, -18L, SEEK_END);
- printf("Now at position %ld in file test.dat.\n",
- ftell(FP));
- while (!feof(FP)) {
- c = fgetc(FP);
- printf("%c", c);
- }
- }
- /* End fseek */
- #undef main
- #undef FAILURE
- /**** name=fsetpos ****/
- #define main TEST_fsetpos
- /* End fsetpos */
- #undef main
- /**** name=_fsopen ****/
- #define main TEST__fsopen
-
- #include <share.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- void main(void) {
- FILE *fp;
- char c;
-
- /*
- The next line opens the file test.dat for reading. If another
- process tries to access test.dat after this program has opened it,
- the other process will be able to read from the file, but not write
- to it.
- */
- if((fp = _fsopen("test.dat", "r", _SH_DENYWR))
- == NULL) {
-
- perror("Error opening test.dat");
- return;
- }
-
- while(!feof(fp)) { /* While it's not the end */
- c = fgetc(fp); /* get another character */
- printf("%c", c); /* and print it out. */
- }
-
- /* Close the file. */
- fclose(fp);
- /*
- Now any other process can read and write to the file test.dat until
- some other process puts a lock on it.
- */
- }
- /* End _fsopen */
- #undef main
- /**** name=ftell ****/
- #define main TEST_ftell
- /* End ftell */
- #undef main
- /**** name=_ftime ****/
- #define main TEST__ftime
-
- #include <types.h>
- #include <timeb.h>
- #include <time.h>
- #undef timezone
- #include <stdio.h>
- #include <stdlib.h>
-
- void main() {
- struct _timeb x;
-
- puts("Testing _ftime...\n");
- _ftime(&x);
- printf("daylight is:%d\n"
- "timezone is:%d\n"
- "time is:%ld\n",
- x.dstflag,x.timezone,x.time);
- printf("time is:%ld\n",time(NULL));
- tzset();
- _ftime(&x);
- printf("daylight is:%d\n"
- "timezone is:%d\n"
- "time is:%ld\n",
- x.dstflag,x.timezone,x.time);
- printf("time is:%ld\n",time(NULL));
- }
- /* End _ftime */
- #undef main
- /**** name=fwrite ****/
- #define main TEST_fwrite
- /* End fwrite */
- #undef main
- /**** name=getc ****/
- #define main TEST_getc
-
- /*
- This program uses getc to print out test.dat.
- */
- #include <stdio.h>
-
- void main() {
- FILE *FP;
- int c;
- if ((FP = fopen("test.dat", "r")) == NULL)
- perror("fopen of test.dat");
- while (!feof(FP)) {
- c = getc(FP);
- printf("%c", c);
- }
- }
- /* End getc */
- #undef main
- /**** name=_getchX ****/
- #define main TEST__getchX
-
- #include <conio.h>
- #include <stdio.h>
-
- void main() {
- int c;
-
- puts("Every other character you key"
- " will be displayed.");
- do {
- c = _getche();
- if (c != '\r')
- c = _getch();
- } while (c != '\r');
- }
-
- /* End _getch* */
- #undef main
- /**** name=getchar ****/
- #define main TEST_getchar
-
-
- /*
- This program copies standard input character by character to getchar.tmp
- and to standard output. Similar examples are given for fgetc and getc,
- pointing out the differences among the functions.
- */
- #include <stdio.h>
- #undef getchar /* Undefine macros and access */
- #undef putchar /* the named functions. */
- void main() {
- FILE *FP1;
-
- printf("Enter characters; end with"
- " the ENTER key.\n");
- if ((FP1 = fopen("getchar.tmp", "w")) == NULL)
- perror("Cannot open getchar.tmp.");
- while ('\n' != fputc(putchar(getchar()), FP1));
- }
- /* End getchar */
- #undef main
- /**** name=_getcwd ****/
- #define main TEST__getcwd
-
- /*
- This program gets the current working directory.
- */
-
- #include <stdio.h>
- #include <direct.h>
-
- void main() {
- char *cwd = NULL;
- int maxlen = 66;
-
- if ((cwd = _getcwd(cwd, maxlen)) != NULL)
- printf("Current working directory: %s\n", cwd);
- else
- perror("Error in _getcwd.");
- }
-
- /* End _getcwd */
- #undef main
- /**** name=getenv ****/
- #define main TEST_getenv
-
- /*
- This program displays the value of the PATH environment variable.
- */
- #include <stdio.h>
- #include <stdlib.h>
- void main() {
- char *ev;
- if ((ev = getenv("PATH")) != NULL)
- printf("The value of PATH is:\n%s", ev);
- else puts("The PATH variable is not set.\n");
- }
-
- /* End getenv */
- #undef main
- /**** name=_getpid ****/
- #define main TEST__getpid
-
- /*
- This program displays the caller's process ID.
- */
-
- #include <process.h>
- #include <stdio.h>
-
- void main() {
- printf("The process ID is: %d\n", _getpid());
- }
-
- /* End _getpid */
- #undef main
- /**** name=_getpvect ****/
- #define main TEST__getpvect
- /* End _getpvect */
- #undef main
- /**** name=_getrvect ****/
- #define main TEST__getrvect
- /* End _getrvect */
- #undef main
- /**** name=gets ****/
- #define main TEST_gets
-
- /*
- The program below copies a line from standard input to standard output.
- It illustrates the use of gets and puts.
- */
-
- #include <stdio.h>
- #define LINESIZE (256)
-
- void main() {
- char line[LINESIZE];
-
- printf("\nWaiting for keyboard input: ");
- if (gets(line))
- puts(line);
- }
-
- /* End gets */
- #undef main
- #undef LINESIZE
- /**** name=gmtime ****/
- #define main TEST_gmtime
-
- /*
- This example illustrates the use of asctime() with time() and
- localtime().
- */
- #include <time.h>
- #include <stdio.h>
-
- void main() {
- time_t t;
- struct tm tim_hd;
- char *str;
-
- time(&t);
- tim_hd = *gmtime(&t);
- str = asctime(&tim_hd);
- printf("Greenwich Mean Time: %s", str);
- }
- /* End gmtime */
- #undef main
- /**** name=HUGE_VAL ****/
- #define main TEST_HUGE_VAL
-
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- void main() {
- double x = 1.0;
- int counter;
- for (counter = 1; counter < 10000; (counter += 20)) {
- if (exp(x) >= HUGE_VAL) {
- printf("\n%d\t.....Number too big."
- " Bye.", counter);
- break;
- }
- else printf("\n%d\t%g", counter, exp(x));
- x += counter;
- }
- }
- /* End HUGE_VAL */
- #undef main
- /**** name=_hypot ****/
- #define main TEST__hypot
-
- #include <math.h>
- #include <stdio.h>
-
- void main() {
- double base=3.0, height=4.0;
-
- puts("Testing _hypot...\n");
- printf("Base = %.2f, Height = %.2f,"
- " Hypotenuse = %.2f\n",
- base,height,_hypot(base,height));
- base = 1.0, height = 1.0;
- printf("Base = %.2f, Height = %.2f,"
- " Hypotenuse = %.2f\n",
- base,height,_hypot(base,height));
- base = 8.0, height = -6.0;
- printf("Base = %.2f, Height = %.2f,"
- " Hypotenuse = %.2f\n",
- base, height, _hypot(base, height));
- }
-
- /* End _hypot */
- #undef main
-
- /*****names*****/
-
- char * names[]={
- "fabs",
- "fclose",
- "_fdopen",
- "fgetc",
- "_fgetchar",
- "fgetpos",
- "fgets",
- "_filelength",
- "_fileno",
- "_fmalloc",
- "_FP_X",
- "fprintf",
- "fread",
- "freopen",
- "frexp",
- "fscanf",
- "fseek",
- "_fsopen",
- "_ftime",
- "getc",
- "_getchX",
- "getchar",
- "_getcwd",
- "getenv",
- "_getpid",
- "gets",
- "gmtime",
- "HUGE_VAL",
- "_hypot",
- "",""};
- int nextfunum;
- void main() {
- char ans[90];
- for (;;) {
- for (int j=0;j< 29;j++)
- if (j%3==2) printf("%4d %-21s\n",j+1,names[j]);
- else printf("%4d %-21s",j+1,names[j]);
- printf("\n\nPlease enter a number from the above list (enter=%d, exit=0): ",++nextfunum);
- gets(ans);
- if (ans[0] != 0) nextfunum=atoi(ans);
- printf("\n\n\n");
- switch(nextfunum) {
- case 0:exit(0);
- case 1:TEST_fabs();break;
- case 2:TEST_fclose();break;
- case 3:TEST__fdopen();break;
- case 4:TEST_fgetc();break;
- case 5:TEST__fgetchar();break;
- case 6:TEST_fgetpos();break;
- case 7:TEST_fgets();break;
- case 8:TEST__filelength();break;
- case 9:TEST__fileno();break;
- case 10:TEST__fmalloc();break;
- case 11:TEST__FP_X();break;
- case 12:TEST_fprintf();break;
- case 13:TEST_fread();break;
- case 14:TEST_freopen();break;
- case 15:TEST_frexp();break;
- case 16:TEST_fscanf();break;
- case 17:TEST_fseek();break;
- case 18:TEST__fsopen();break;
- case 19:TEST__ftime();break;
- case 20:TEST_getc();break;
- case 21:TEST__getchX();break;
- case 22:TEST_getchar();break;
- case 23:TEST__getcwd();break;
- case 24:TEST_getenv();break;
- case 25:TEST__getpid();break;
- case 26:TEST_gets();break;
- case 27:TEST_gmtime();break;
- case 28:TEST_HUGE_VAL();break;
- case 29:TEST__hypot();break;
- default:printf("I don't recognize that answer\n");nextfunum=-1;break;
- }
- printf("\n\npress enter to select another function\n");
- gets(ans);
- }
- }
-