double fabs(double x);
返回 x 的绝对值(double)
float fabsf(float x);
返回 x 的绝对值(float)
long double fabsl(long double x);
返回 x 的绝对值(long double)
1.2 演示示例
#include <stdio.h>
#include <math.h>
int main(void)
{
double result, x = -4.5;
result = fabs(x); // 取绝对值
float resultf, xf = -3.5;
resultf = fabsf(xf);
long double resultL, xL = -2.5;
resultL = fabsl(xL);
printf("The absolute value of %lf is %lf\n", x, result);
printf("The absolute value of %f is %f\n", xf, resultf);
printf("The absolute value of %Lf is %Lf\n", xL, resultL);
return 0;
}
1.3 运行结果
2. fclose
2.1 函数说明
函数声明
函数功能
int fclose(FILE *stream);
关闭一个文件流
2.2 演示示例
#include <string.h>
#include <stdio.h>
int main(void)
{
FILE *fp;
char buf[30] = "Hello, Huazie 123456789";
fp = fopen("temp.txt", "w");
printf("temp.txt is created and opened\n");
fwrite(&buf, strlen(buf), 1, fp);
printf("temp.txt is writed\n");
fclose(fp);
printf("temp.txt is closed");
return 0;
}
2.3 运行结果
3. fcloseall
3.1 函数说明
函数声明
函数功能
int fcloseall(void);
关闭除标准流(stdin、stdout、stderr、stdprn、stdaux)之外的所有打开的流,刷新所有的流缓冲区,并返回关闭的流数。
3.2 演示示例
#include <stdio.h>
#define fcloseall() _fcloseall();
int main()
{
int streams_closed;
fopen("temp.one", "w");
fopen("temp.two", "w");
// 关闭打开流
streams_closed = fcloseall();
if (streams_closed == EOF)
perror("Error");
else
printf("%d streams were closed.\n", streams_closed);
return 0;
}
3.3 运行结果
4. fcvt
4.1 函数说明
函数声明
函数功能
char * fcvt(double value, int ndigit, int *decpt, int *sign);
把一个双精度浮点数转换为字符串
double fdim (double x, double y);
计算 x 和 y 之间的正差值 (double)
float fdimf (float x, float y);
计算 x 和 y 之间的正差值 (float)
long double fdiml (long double x, long double y);
计算 x 和 y 之间的正差值 (long double)
int fflush(FILE *stream);
清除读写缓冲区,并将缓冲区内的数据写回参数stream指向的文件中。
9.2 演示示例
#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("STU.FIL", "w");
fwrite(msg, strlen(msg), 1, stream);
printf("Press any key to flush STU.FIL:");
getchar();
// 将数据刷新到 STU.FIL 中而不关闭它
flush(stream);
printf("\nFile was flushed, Press any key to quit:");
getchar();
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);
}
9.3 运行结果
10. fgetc
10.1 函数说明
函数声明
函数功能
int fgetc(FILE *stream);
从流中读取字符
10.2 演示示例
#include <string.h>
#include <stdio.h>
int main(void)
{
FILE *stream;
char string[] = "This is a test";
char ch;
stream = fopen("STU.FIL", "w+");
fwrite(string, strlen(string), 1, stream);
fseek(stream, 0, SEEK_SET);
do
{
ch = fgetc(stream);
putchar(ch);
} while (ch != EOF);
fclose(stream);
return 0;
}
10.3 运行结果
11. fgetchar
11.1 函数说明
函数声明
函数功能
int fgetchar(void);
从流中读取字符
11.2 演示示例
#include <stdio.h>
int main(void)
{
char ch;
printf("Enter a character followed by <Enter>: ");
// read the character from stdin
ch = fgetchar();
printf("The character read is: '%c'\n", ch);
return 0;
}
11.3 运行结果
12. fgetpos
12.1 函数说明
函数声明
函数功能
int fgetpos(FILE *stream);
依据当前文件的句柄,获取当前访问指针位置信息
12.2 演示示例
#include <string.h>
#include <stdio.h>
int main(void)
{
FILE *stream;
char string[] = "This is a test";
fpos_t filepos;
stream = fopen("STU.FIL", "w+");
fwrite(string, strlen(string), 1, stream);
// report the file pointer position
fgetpos(stream, &filepos);
printf("The file pointer is at byte %lld\n", filepos);
fclose(stream);
return 0;
}
12.3 运行结果
13. fgets
13.1 函数说明
函数声明
函数功能
char * fgets(char *str, int n, FILE *stream);
从指定的流中读取数据,每次读取一行
double fabs(double x);
返回 x 的绝对值(double)
float fabsf(float x);
返回 x 的绝对值(float)
long double fabsl(long double x);
返回 x 的绝对值(long double)
1.2 演示示例
#include <stdio.h>
#include <math.h>
int main(void)
{
double result, x = -4.5;
result = fabs(x); // 取绝对值
float resultf, xf = -3.5;
resultf = fabsf(xf);
long double resultL, xL = -2.5;
resultL = fabsl(xL);
printf("The absolute value of %lf is %lf\n", x, result);
printf("The absolute value of %f is %f\n", xf, resultf);
printf("The absolute value of %Lf is %Lf\n", xL, resultL);
return 0;
}
1.3 运行结果
2. fclose
2.1 函数说明
函数声明
函数功能
int fclose(FILE *stream);
关闭一个文件流
2.2 演示示例
#include <string.h>
#include <stdio.h>
int main(void)
{
FILE *fp;
char buf[30] = "Hello, Huazie 123456789";
fp = fopen("temp.txt", "w");
printf("temp.txt is created and opened\n");
fwrite(&buf, strlen(buf), 1, fp);
printf("temp.txt is writed\n");
fclose(fp);
printf("temp.txt is closed");
return 0;
}
2.3 运行结果
3. fcloseall
3.1 函数说明
函数声明
函数功能
int fcloseall(void);
关闭除标准流(stdin、stdout、stderr、stdprn、stdaux)之外的所有打开的流,刷新所有的流缓冲区,并返回关闭的流数。
3.2 演示示例
#include <stdio.h>
#define fcloseall() _fcloseall();
int main()
{
int streams_closed;
fopen("temp.one", "w");
fopen("temp.two", "w");
// 关闭打开流
streams_closed = fcloseall();
if (streams_closed == EOF)
perror("Error");
else
printf("%d streams were closed.\n", streams_closed);
return 0;
}
3.3 运行结果
4. fcvt
4.1 函数说明
函数声明
函数功能
char * fcvt(double value, int ndigit, int *decpt, int *sign);
把一个双精度浮点数转换为字符串
double fdim (double x, double y);
计算 x 和 y 之间的正差值 (double)
float fdimf (float x, float y);
计算 x 和 y 之间的正差值 (float)
long double fdiml (long double x, long double y);
计算 x 和 y 之间的正差值 (long double)
int fflush(FILE *stream);
清除读写缓冲区,并将缓冲区内的数据写回参数stream指向的文件中。
9.2 演示示例
#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("STU.FIL", "w");
fwrite(msg, strlen(msg), 1, stream);
printf("Press any key to flush STU.FIL:");
getchar();
// 将数据刷新到 STU.FIL 中而不关闭它
flush(stream);
printf("\nFile was flushed, Press any key to quit:");
getchar();
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);
}
9.3 运行结果
10. fgetc
10.1 函数说明
函数声明
函数功能
int fgetc(FILE *stream);
从流中读取字符
10.2 演示示例
#include <string.h>
#include <stdio.h>
int main(void)
{
FILE *stream;
char string[] = "This is a test";
char ch;
stream = fopen("STU.FIL", "w+");
fwrite(string, strlen(string), 1, stream);
fseek(stream, 0, SEEK_SET);
do
{
ch = fgetc(stream);
putchar(ch);
} while (ch != EOF);
fclose(stream);
return 0;
}
10.3 运行结果
11. fgetchar
11.1 函数说明
函数声明
函数功能
int fgetchar(void);
从流中读取字符
11.2 演示示例
#include <stdio.h>
int main(void)
{
char ch;
printf("Enter a character followed by <Enter>: ");
// read the character from stdin
ch = fgetchar();
printf("The character read is: '%c'\n", ch);
return 0;
}
11.3 运行结果
12. fgetpos
12.1 函数说明
函数声明
函数功能
int fgetpos(FILE *stream);
依据当前文件的句柄,获取当前访问指针位置信息
12.2 演示示例
#include <string.h>
#include <stdio.h>
int main(void)
{
FILE *stream;
char string[] = "This is a test";
fpos_t filepos;
stream = fopen("STU.FIL", "w+");
fwrite(string, strlen(string), 1, stream);
// report the file pointer position
fgetpos(stream, &filepos);
printf("The file pointer is at byte %lld\n", filepos);
fclose(stream);
return 0;
}
12.3 运行结果
13. fgets
13.1 函数说明
函数声明
函数功能
char * fgets(char *str, int n, FILE *stream);
从指定的流中读取数据,每次读取一行