C言語関数セット(五)


   : fabs    :              : double fabs(double x);    : #include <stdio.h> #include <math.h> int main(void) { float number = -1234.0; printf("number: %f absolute value: %f
"
, number, fabs(number)); return 0; } : farcalloc : : void far *farcalloc(unsigned long units, unsigned ling unitsz); : #include <stdio.h> #include <alloc.h> #include <string.h> #include <dos.h> int main(void) { char far *fptr; char *str = "Hello"; /* allocate memory for the far pointer */ fptr = farcalloc(10, sizeof(char)); /* copy "Hello" into allocated memory */ /* Note: movedata is used because you might be in a small data model, in which case a normal string copy routine can not be used since it assumes the pointer size is near. */ movedata(FP_SEG(str), FP_OFF(str), FP_SEG(fptr), FP_OFF(fptr), strlen(str)); /* display string (note the F modifier) */ printf("Far string is: %Fs
"
, fptr); /* free the memory */ farfree(fptr); return 0; } : farcoreleft : : long farcoreleft(void); : #include <stdio.h> #include <alloc.h> int main(void) { printf("The difference between the\ highest allocated block in the\ far
"
); printf("heap and the top of the far heap\ is: %lu bytes
"
, farcoreleft()); return 0; } : farfree : : void farfree(void); : #include <stdio.h> #include <alloc.h> #include <string.h> #include <dos.h> int main(void) { char far *fptr; char *str = "Hello"; /* allocate memory for the far pointer */ fptr = farcalloc(10, sizeof(char)); /* copy "Hello" into allocated memory */ /* Note: movedata is used because you might be in a small data model, in which case a normal string copy routine can't be used since it assumes the pointer size is near. */ movedata(FP_SEG(str), FP_OFF(str), FP_SEG(fptr), FP_OFF(fptr), strlen(str)); /* display string (note the F modifier) */ printf("Far string is: %Fs
"
, fptr); /* free the memory */ farfree(fptr); return 0; } : farmalloc : : void far *farmalloc(unsigned long size); : #include <stdio.h> #include <alloc.h> #include <string.h> #include <dos.h> int main(void) { char far *fptr; char *str = "Hello"; /* allocate memory for the far pointer */ fptr = farmalloc(10); /* copy "Hello" into allocated memory */ /* Note: movedata is used because we might be in a small data model, in which case a normal string copy routine can not be used since it assumes the pointer size is near. */ movedata(FP_SEG(str), FP_OFF(str), FP_SEG(fptr), FP_OFF(fptr), strlen(str)); /* display string (note the F modifier) */ printf("Far string is: %Fs
"
, fptr); /* free the memory */ farfree(fptr); return 0; } : farrealloc : : void far *farrealloc(void far *block, unsigned long newsize); : #include <stdio.h> #include <alloc.h> int main(void) { char far *fptr; fptr = farmalloc(10); printf("First address: %Fp
"
, fptr); fptr = farrealloc(fptr,20); printf("New address : %Fp
"
, fptr); farfree(fptr); return 0; } : fclose : : int fclose(FILE *stream); : #include <string.h> #include <stdio.h> int main(void) { FILE *fp; char buf[11] = "0123456789"; /* create a file containing 10 bytes */ fp = fopen("DUMMY.FIL", "w"); fwrite(&buf, strlen(buf), 1, fp); /* close the file */ fclose(fp); return 0; } : fcloseall : : int fcloseall(void); : #include <stdio.h> int main(void) { int streams_closed; /* open two streams */ fopen("DUMMY.ONE", "w"); fopen("DUMMY.TWO", "w"); /* close the open streams */ streams_closed = fcloseall(); if (streams_closed == EOF) /* issue an error message */ perror("Error"); else /* print result of fcloseall() function */ printf("%d streams were closed.
"
, streams_closed); return 0; } : fcvt : : char *fcvt(double value, int ndigit, int *decpt, int *sign); : #include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void) { char *string; double value; int dec, sign; int ndig = 10; clrscr(); value = 9.876; string = ecvt(value, ndig, &dec, &sign); printf("string = %s dec = %d \ sign = %d
"
, string, dec, sign); value = -123.45; ndig= 15; string = ecvt(value,ndig,&dec,&sign); printf("string = %s dec = %d sign = %d
"
, string, dec, sign); value = 0.6789e5; /* scientific notation */ ndig = 5; string = ecvt(value,ndig,&dec,&sign); printf("string = %s dec = %d\ sign = %d
"
, string, dec, sign); return 0; } : fdopen : : FILE *fdopen(int handle, char *type); : #include <sys\stat.h> #include <stdio.h> #include <fcntl.h> #include <io.h> int main(void) { int handle; FILE *stream; /* open a file */ handle = open("DUMMY.FIL", O_CREAT, S_IREAD | S_IWRITE); /* now turn the handle into a stream */ stream = fdopen(handle, "w"); if (stream == NULL) printf("fdopen failed
"
); else { fprintf(stream, "Hello world
"
); fclose(stream); } return 0; } : feof : : int feof(FILE *stream); : #include <stdio.h> int main(void) { FILE *stream; /* open a file for reading */ stream = fopen("DUMMY.FIL", "r"); /* read a character from the file */ fgetc(stream); /* check for EOF */ if (feof(stream)) printf("We have reached end-of-file
"
); /* close the file */ fclose(stream); return 0; } : ferror : : int ferror(FILE *stream); : #include <stdio.h> int main(void) { FILE *stream; /* open a file for writing */ stream = fopen("DUMMY.FIL", "w"); /* force an error condition by attempting to read */ (void) getc(stream); if (ferror(stream)) /* test for an error on the stream */ { /* display an error message */ printf("Error reading from DUMMY.FIL
"
); /* reset the error and EOF indicators */ clearerr(stream); } fclose(stream); return 0; } : fflush : : int fflush(FILE *stream); : #include <string.h> #include <stdio.h> #include <conio.h> #include <io.h> void flush(FILE *stream); int main(void) { FILE *stream; char msg[] = "This is a test"; /* create a file */ stream = fopen("DUMMY.FIL", "w"); /* write some data to the file */ fwrite(msg, strlen(msg), 1, stream); clrscr(); printf("Press any key to flush\ DUMMY.FIL:"); getch(); /* flush the data to DUMMY.FIL without\ closing it */ flush(stream); printf("
File was flushed, Press any key\ to quit:"
); getch(); return 0; } void flush(FILE *stream) { int duphandle; /* flush the stream's internal buffer */ fflush(stream); /* make a duplicate file handle */ duphandle = dup(fileno(stream)); /* close the duplicate handle to flush\ the DOS buffer */ close(duphandle); } : fgetc : : int fgetc(FILE *stream); : #include <string.h> #include <stdio.h> #include <conio.h> int main(void) { FILE *stream; char string[] = "This is a test"; char ch; /* open a file for update */ stream = fopen("DUMMY.FIL", "w+"); /* write a string into the file */ fwrite(string, strlen(string), 1, stream); /* seek to the beginning of the file */ fseek(stream, 0, SEEK_SET); do { /* read a char from the file */ ch = fgetc(stream); /* display the character */ putch(ch); } while (ch != EOF); fclose(stream); return 0; } : fgetchar : : int fgetchar(void); : #include <stdio.h> int main(void) { char ch; /* prompt the user for input */ printf("Enter a character followed by \ <Enter>: "); /* read the character from stdin */ ch = fgetchar(); /* display what was read */ printf("The character read is: '%c'
"
, ch); return 0; } : fgetpos : : int fgetpos(FILE *stream); : #include <string.h> #include <stdio.h> int main(void) { FILE *stream; char string[] = "This is a test"; fpos_t filepos; /* open a file for update */ stream = fopen("DUMMY.FIL", "w+"); /* write a string into the file */ fwrite(string, strlen(string), 1, stream); /* report the file pointer position */ fgetpos(stream, &filepos); printf("The file pointer is at byte\ %ld
"
, filepos); fclose(stream); return 0; } : fgets : : char *fgets(char *string, int n, FILE *stream); : #include <string.h> #include <stdio.h> int main(void) { FILE *stream; char string[] = "This is a test"; char msg[20]; /* open a file for update */ stream = fopen("DUMMY.FIL", "w+"); /* write a string into the file */ fwrite(string, strlen(string), 1, stream); /* seek to the start of the file */ fseek(stream, 0, SEEK_SET); /* read a string from the file */ fgets(msg, strlen(string)+1, stream); /* display the string */ printf("%s", msg); fclose(stream); return 0; } : filelength : : long filelength(int handle); : #include <string.h> #include <stdio.h> #include <fcntl.h> #include <io.h> int main(void) { int handle; char buf[11] = "0123456789"; /* create a file containing 10 bytes */ handle = open("DUMMY.FIL", O_CREAT); write(handle, buf, strlen(buf)); /* display the size of the file */ printf("file length in bytes: %ld
"
, filelength(handle)); /* close the file */ close(handle); return 0; } : fillellipse : : void far fillellipse(int x, int y, int xradius, int yradius); : #include <graphics.h> #include <conio.h> int main(void) { int gdriver = DETECT, gmode; int xcenter, ycenter, i; initgraph(&gdriver,&gmode,""); xcenter = getmaxx() / 2; ycenter = getmaxy() / 2; for (i=0; i<13; i++) { setfillstyle(i,WHITE); fillellipse(xcenter,ycenter,100,50); getch(); } closegraph(); return 0; } : fillpoly : : void far fillpoly(int numpoints, int far *polypoints); : #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int i, maxx, maxy; /* our polygon array */ int poly[8]; /* initialize graphics, local variables */ initgraph(&gdriver, &gmode, ""); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %s
"
, grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ } maxx = getmaxx(); maxy = getmaxy(); poly[0] = 20; /* 1st vertext */ poly[1] = maxy / 2; poly[2] = maxx - 20; /* 2nd */ poly[3] = 20; poly[4] = maxx - 50; /* 3rd */ poly[5] = maxy - 20; /* 4th vertex. fillpoly automatically closes the polygon. */ poly[6] = maxx / 2; poly[7] = maxy / 2; /* loop through the fill patterns */ for (i=EMPTY_FILL; i<USER_FILL; i++) { /* set fill pattern */ setfillstyle(i, getmaxcolor()); /* draw a filled polygon */ fillpoly(4, poly); getch(); } /* clean up */ closegraph(); return 0; } : findfirst, findnext : ; findfirst : int findfirst(char *pathname, struct ffblk *ffblk, int attrib); int findnext(struct ffblk *ffblk); : /* findnext example */ #include <stdio.h> #include <dir.h> int main(void) { struct ffblk ffblk; int done; printf("Directory listing of *.*
"
); done = findfirst("*.*",&ffblk,0); while (!done) { printf(" %s
"
, ffblk.ff_name); done = findnext(&ffblk); } return 0; } : floodfill : : void far floodfill(int x, int y, int border); : #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int maxx, maxy; /* initialize graphics, local variables */ initgraph(&gdriver, &gmode, ""); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %s
"
, grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ } maxx = getmaxx(); maxy = getmaxy(); /* select drawing color */ setcolor(getmaxcolor()); /* select fill color */ setfillstyle(SOLID_FILL, getmaxcolor()); /* draw a border around the screen */ rectangle(0, 0, maxx, maxy); /* draw some circles */ circle(maxx / 3, maxy /2, 50); circle(maxx / 2, 20, 100); circle(maxx-20, maxy-50, 75); circle(20, maxy-20, 25); /* wait for a key */ getch(); /* fill in bounded region */ floodfill(2, 2, getmaxcolor()); /* clean up */ getch(); closegraph(); return 0; } : floor : : double floor(double x); : #include <stdio.h> #include <math.h> int main(void) { double number = 123.54; double down, up; down = floor(number); up = ceil(number); printf("original number %10.2lf
"
, number); printf("number rounded down %10.2lf
"
, down); printf("number rounded up %10.2lf
"
, up); return 0; } : flushall : : int flushall(void); : #include <stdio.h> int main(void) { FILE *stream; /* create a file */ stream = fopen("DUMMY.FIL", "w"); /* flush all open streams */ printf("%d streams were flushed.
"
, flushall()); /* close the file */ fclose(stream); return 0; } : fmod : x y , x/y : double fmod(double x, double y); : #include <stdio.h> #include <math.h> int main(void) { double x = 5.0, y = 2.0; double result; result = fmod(x,y); printf("The remainder of (%lf / %lf) is \ %lf
"
, x, y, result); return 0; } : fnmerge : : void fnerge(char *path, char *drive, char *dir); : #include <string.h> #include <stdio.h> #include <dir.h> int main(void) { char s[MAXPATH]; char drive[MAXDRIVE]; char dir[MAXDIR]; char file[MAXFILE]; char ext[MAXEXT]; getcwd(s,MAXPATH); /* get the current working directory */ strcat(s,"\\"); /* append on a trailing \ character */ fnsplit(s,drive,dir,file,ext); /* split the string to separate elems */ strcpy(file,"DATA"); strcpy(ext,".TXT"); fnmerge(s,drive,dir,file,ext); /* merge everything into one string */ puts(s); /* display resulting string */ return 0; } : fopen : : FILE *fopen(char *filename, char *type); : #include <stdlib.h> #include <stdio.h> #include <dir.h> int main(void) { char *s; char drive[MAXDRIVE]; char dir[MAXDIR]; char file[MAXFILE]; char ext[MAXEXT]; int flags; s=getenv("COMSPEC"); /* get the comspec environment parameter */ flags=fnsplit(s,drive,dir,file,ext); printf("Command processor info:
"
); if(flags & DRIVE) printf("\tdrive: %s
"
,drive); if(flags & DIRECTORY) printf("\tdirectory: %s
"
,dir); if(flags & FILENAME) printf("\tfile: %s
"
,file); if(flags & EXTENSION) printf("\textension: %s
"
,ext); return 0; } : fprintf : : int fprintf(FILE *stream, char *format[, argument,...]); : /* Program to create backup of the AUTOEXEC.BAT file */ #include <stdio.h> int main(void) { FILE *in, *out; if ((in = fopen("\\AUTOEXEC.BAT", "rt")) == NULL) { fprintf(stderr, "Cannot open input \ file.
"
); return 1; } if ((out = fopen("\\AUTOEXEC.BAK", "wt")) == NULL) { fprintf(stderr, "Cannot open output \ file.
"
); return 1; } while (!feof(in)) fputc(fgetc(in), out); fclose(in); fclose(out); return 0; } : FP_OFF : : unsigned FP_OFF(void far *farptr); : /* FP_OFF */ #include <dos.h> #include <stdio.h> int main(void) { char *str = "fpoff.c"; printf("The offset of this file in memory\ is: %Fp
"
, FP_OFF(str)); return 0; } : FP_SEG : : unsigned FP_SEG(void far *farptr); : /* FP_SEG */ #include <dos.h> #include <stdio.h> int main(void) { char *filename = "fpseg.c"; printf("The offset of this file in memory\ is: %Fp
"
, FP_SEG(filename)); return(0); } : fputc : : int fputc(int ch, FILE *stream); : #include <stdio.h> int main(void) { char msg[] = "Hello world"; int i = 0; while (msg[i]) { fputc(msg[i], stdout); i++; } return 0; } : fputchar : (stdout) : int fputchar(char ch); : #include <stdio.h> int main(void) { char msg[] = "This is a test"; int i = 0; while (msg[i]) { fputchar(msg[i]); i++; } return 0; } : fputs : : int fputs(char *string, FILE *stream); : #include <stdio.h> int main(void) { /* write a string to standard output */ fputs("Hello world
"
, stdout); return 0; } : fread : : int fread(void *ptr, int size, int nitems, FILE *stream); : #include <string.h> #include <stdio.h> int main(void) { FILE *stream; char msg[] = "this is a test"; char buf[20]; if ((stream = fopen("DUMMY.FIL", "w+")) == NULL) { fprintf(stderr, "Cannot open output file.
"
); return 1; } /* write some data to the file */ fwrite(msg, strlen(msg)+1, 1, stream); /* seek to the beginning of the file */ fseek(stream, SEEK_SET, 0); /* read the data and display it */ fread(buf, strlen(msg)+1, 1, stream); printf("%s
"
, buf); fclose(stream); return 0; } : free : : void free(void *ptr); : #include <string.h> #include <stdio.h> #include <alloc.h> int main(void) { char *str; /* allocate memory for string */ str = malloc(10); /* copy "Hello" to string */ strcpy(str, "Hello"); /* display string */ printf("String is %s
"
, str); /* free memory */ free(str); return 0; } : freemem : DOS : int freemem(unsigned seg); : #include <dos.h> #include <alloc.h> #include <stdio.h> int main(void) { unsigned int size, segp; int stat; size = 64; /* (64 x 16) = 1024 bytes */ stat = allocmem(size, &segp); if (stat < 0) printf("Allocated memory at segment:\ %x
"
, segp); else printf("Failed: maximum number of\ paragraphs available is %u
"
, stat); freemem(segp); return 0; } : freopen : : FILE *freopen(char *filename, char *type, FILE *stream); : #include <stdio.h> int main(void) { /* redirect standard output to a file */ if (freopen("OUTPUT.FIL", "w", stdout) == NULL) fprintf(stderr, "error redirecting\ stdout
"
); /* this output will go to a file */ printf("This will go into a file."); /* close the standard output stream */ fclose(stdout); return 0; } : frexp : : double frexp(double value, int *eptr); : #include <math.h> #include <stdio.h> int main(void) { double mantissa, number; int exponent; number = 8.0; mantissa = frexp(number, &exponent); printf("The number %lf is ", number); printf("%lf times two to the ", mantissa); printf("power of %d
"
, exponent); return 0; } : fscanf : : int fscanf(FILE *stream, char *format[,argument...]); : #include <stdlib.h> #include <stdio.h> int main(void) { int i; printf("Input an integer: "); /* read an integer from the standard input stream */ if (fscanf(stdin, "%d", &i)) printf("The integer read was: %i
"
, i); else { fprintf(stderr, "Error reading an \ integer from stdin.
"
); exit(1); } return 0; } : fseek : : int fseek(FILE *stream, long offset, int fromwhere); : #include <stdio.h> long filesize(FILE *stream); int main(void) { FILE *stream; stream = fopen("MYFILE.TXT", "w+"); fprintf(stream, "This is a test"); printf("Filesize of MYFILE.TXT is %ld bytes
"
, filesize(stream)); fclose(stream); return 0; } long filesize(FILE *stream) { long curpos, length; curpos = ftell(stream); fseek(stream, 0L, SEEK_END); length = ftell(stream); fseek(stream, curpos, SEEK_SET); return length; } : fsetpos : : int fsetpos(FILE *stream, const fpos_t *pos); : #include <stdlib.h> #include <stdio.h> void showpos(FILE *stream); int main(void) { FILE *stream; fpos_t filepos; /* open a file for update */ stream = fopen("DUMMY.FIL", "w+"); /* save the file pointer position */ fgetpos(stream, &filepos); /* write some data to the file */ fprintf(stream, "This is a test"); /* show the current file position */ showpos(stream); /* set a new file position, display it */ if (fsetpos(stream, &filepos) == 0) showpos(stream); else { fprintf(stderr, "Error setting file \ pointer.
"
); exit(1); } /* close the file */ fclose(stream); return 0; } void showpos(FILE *stream) { fpos_t pos; /* display the current file pointer position of a stream */ fgetpos(stream, &pos); printf("File position: %ld
"
, pos); } : fstat : : int fstat(char *handle, struct stat *buff); : #include <sys\stat.h> #include <stdio.h> #include <time.h> int main(void) { struct stat statbuf; FILE *stream; /* open a file for update */ if ((stream = fopen("DUMMY.FIL", "w+")) == NULL) { fprintf(stderr, "Cannot open output \ file.
"
); return(1); } fprintf(stream, "This is a test"); fflush(stream); /* get information about the file */ fstat(fileno(stream), &statbuf); fclose(stream); /* display the information returned */ if (statbuf.st_mode & S_IFCHR) printf("Handle refers to a device.
"
); if (statbuf.st_mode & S_IFREG) printf("Handle refers to an ordinary \ file.
"
); if (statbuf.st_mode & S_IREAD) printf("User has read permission on \ file.
"
); if (statbuf.st_mode & S_IWRITE) printf("User has write permission on \ file.
"
); printf("Drive letter of file: %c
"
, 'A'+statbuf.st_dev); printf("Size of file in bytes: %ld
"
, statbuf.st_size); printf("Time file last opened: %s
"
, ctime(&statbuf.st_ctime)); return 0; } : ftell : : long ftell(FILE *stream); : #include <stdio.h> int main(void) { FILE *stream; stream = fopen("MYFILE.TXT", "w+"); fprintf(stream, "This is a test"); printf("The file pointer is at byte \ %ld
"
, ftell(stream)); fclose(stream); return 0; } : fwrite : : int fwrite(void *ptr, int size, int nitems, FILE *stream); : #include <stdio.h> struct mystruct { int i; char ch; }; int main(void) { FILE *stream; struct mystruct s; if ((stream = fopen("TEST.$$$", "wb")) == NULL) /* open file TEST.$$$ */ { fprintf(stderr, "Cannot open output file.
"
); return 1; } s.i = 0; s.ch = 'A'; fwrite(&s, sizeof(s), 1, stream); /* write struct s to file */ fclose(stream); /* close file */ return 0; }