C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. farrealloc函数
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\n", fptr);
fptr = farrealloc(fptr,20);
printf("New address : %Fp\n", fptr);
farfree(fptr);
return 0;
}
2. fcloseall函数
fcloseall函数的功能是关闭打开流,其用法为int fcloseall(void);程序实例代码如下:
#include <stdio.h>
int main(void)
{
int streams_closed;
fopen("DUMMY.ONE", "w");
fopen("DUMMY.TWO", "w");
streams_closed = fcloseall();
if (streams_closed == EOF)
perror("Error");
else
printf("%d streams were closed.\n", streams_closed);
return 0;
}
3. fflush函数
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";
stream = fopen("DUMMY.FIL", "w");
fwrite(msg, strlen(msg), 1, stream);
clrscr();
printf("Press any key to flush\
DUMMY.FIL:");
getch();
closing it */
flush(stream);
printf("\nFile was flushed, Press any key\
to quit:");
getch();
return 0;
}
void flush(FILE *stream)
{
int duphandle;
fflush(stream);
duphandle = dup(fileno(stream));
the DOS buffer */
close(duphandle);
}
4. fgetc函数
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;
stream = fopen("DUMMY.FIL", "w+");
fwrite(string, strlen(string), 1, stream);
fseek(stream, 0, SEEK_SET);
do
{
ch = fgetc(stream);
putch(ch);
} while (ch != EOF);
fclose(stream);
return 0;
}
转载请注明出处: 程序员之家 http://www.sunxin.org