C++常用庫函數

1、常用數學函數

    頭文件 #include <math> 或者 #include <math.h>

 

函數原型

功能

返回值

int abs(int x)

求整數x的絕對值

絕對值

double acos(double x)

計算arcos(x)的值

計算結果

double asin(double x)

計算arsin(x)的值

計算結果

double atan(double x)

計算arctan(x)的值

計算結果

double cos(double x)

計算cos(x)的值

計算結果

double cosh(double x)

計算x的雙曲餘弦cosh(x)的值

計算結果

double exp(double x)

求的值

計算結果

double fabs(double x)

求實數x的絕對值

絕對值

double fmod(double x)

求x/y的餘數

餘數的雙精度數

long labs(long x)

求長整型數的絕對值

絕對值

double log(double x)

計算In(x)的值

計算結果

double log10(double x)

計算的值

計算結果

double modf(double x, double *y)

取x的整數部分送到y所指向的單元格中

x的小樹部分

double pow(double x, double y)

                                     求的值

計算結果

double sin(double x)

計算sin(x)的值

計算結果

double sqrt(double x)

求的值

計算結果

double tan(double x)

計算tan(x)的值

計算結果

fcvt

將浮點型數轉化爲字符串

 

 

 

2、常用字符串處理函數

頭文件 #include <string> 或者 #include <string.h>

函數原型

功能

返回值

void *memcpy(void *p1, const void *p2 size_t n)

存儲器拷貝,將p2所指向的共n個字節拷貝到p1所指向的存儲區中

目的存儲區的起始地址

實現任意數據類型之間的拷貝

void *memset(void *p int v, size_t n)

將v的值作爲p所指向的區域的

值,n是p所指向區域的大小

該區域的起始地址

char *strcpy(char *p1, const char *p2)

將p2所指向的字符串拷貝到

p1所指向的存儲區中

目的存儲區的起始地址

char *strcat(char *p1, const

char *p2)

將p2所指向的字符串連接到

p1所指向的字符串後面

目的存儲區的起始地址

int strcmp(const char *p1, const char *p2)

比較p1,p2所指向的兩個

字符串的大小

兩個字符串相同,返回0;若p1所指向的字符串小於p2所指的字符串,返回負值;否則,返回正值

int strlen(const char *p)

求p所指向的字符串的長度

字符串所包含的字符個數

不包括字符串結束標誌’\n’

char *strncpy(char *p1, const char *p2, size_t n)

將p2所指向的字符串(至多n個字符)拷貝到p1所指向的存儲區中

目的存儲區的起始地址

(與strcpy()類似)

char *strncat(char *p1, const char *p2, size_t n)

將p2所指向的字符串(至多n個字符)連接到p1所指向的字符串的後面

目的存儲區的起始地址

(與strcpy()類似)

char *strncmp(const char *p1, const char *p2, size_t n)

比較p1,p2所指向的兩個字符串的大小,至多比較n個字符

兩個字符串相同,返回0;若p1所指向的字符串小於p2所指的字符串,返回負值;否則,返回正值

(與strcpy()類似)

char *strstr(const char *p1, const char *p2)

判斷p2所指向的字符串是否是p1所指向的字符串的子串

若是子串,返回開始位置的地址;否則返回0。

 

3、其他常用函數

頭文件#include <stdlib> 或者 #include <stdlib.h>

函數原型

功能

返回值

說明

void abort(void)

終止程序執行

 

不能結束工作

void exit(int)

終止程序執行

 

做結束工作

double atof(const char *s)

將s所指向的字符串轉換成實數

實數值

 

int atoi(const char *s)

將s所指向的字符串轉換成整數

整數值

 

long atol(const char *s)

將s所指的字符串轉換成長整數

長整數值

 

int rand(void)

產生一個隨機整數

隨機整數

 

void srand(unsigned int)

初始化隨機數產生器

   

int system(const char *s)

將s所指向的字符串作爲一個可執行文件,並加以執行

   

max(a, b)

求兩個數中的大數

大數

參數爲任意類型

min(a,b)

求兩個數中的小數

小數

參數爲任意類型

4、實現鍵盤和文件輸入/輸出的成員函數

頭文件#include <iostream> 或者 #include <iostream.h>

函數原型

功能

返回值

cin >> v

輸入值送給變量

 

cout << exp

輸出表達式exp的值

 

istream & istream::get(char &c)

輸入字符送給變量c

 

istream & istream::get(char *, int , char = ‘\n’)

輸入一行字符串

 

istream & istream::getline(char *, int , char = ‘\n’)

輸入一行字符串

 

void ifstream::open(const char*,int=ios::in,

int = filebuf::openprot )

打開輸入文件

 

void ofstream::open(const char*,int=ios::out,

int = filebuf::openprot)

打開輸出文件

 

void fsream::open(const char*,int ,

int = filebuf::openprot)

打開輸入/輸出文件

 

ifstream::ifstream(const char*,int = ios::in,

int = filebuf::openprot)

構造函數打開輸入文件

 

ofstream::ofstream(const char*,int=ios::out,

int = filebuf::openprot)

構造函數打開輸出函數

 

fstream::fstream(const char*, int,

int = filebuf::openprot)

構造函數打開輸入/輸出文件

 

void istream::close()

關閉輸入文件

 

void ofsream::close()

關閉輸出文件

 

void fsream::close()

關閉輸入/輸出文件

 

istream & istream::read(char*, int)

從文件中讀取數據

 

ostream & istream::write(const char*,int)

將數據寫入文件中

 

int ios::eof()

判斷是否到達打開文件的尾部

1爲到達2爲沒有

istream & istream::seekg(streampos)

移動輸入文件的指針

 

istream & istream::seekg(streamoff,ios::seek_dir)

移動輸入文件的指針

 

streampos istream::tellg()

取輸入文件的指針

 

ostream & ostream::seekp(streampos)

移動輸出文件的指針

 

ostream & ostream::seekp(streamoff,ios::seek_dir)

移動輸出文件的指針

 

streampos ostream::tellp()

取輸出文件的指針

 

 

C++的頭文件!

#include <ctype.h>//字符處理

#include <errno.h>//定義錯誤碼

#include <float.h>//浮點數處理

#include <fstream.h>//文件輸入/輸出

#include <iomanip.h>//參數化輸入/輸出

#include <iostream.h> //數據流輸入/輸出

#include <limits.h> //定義各種數據類型最值常量

#include <locale.h> //定義本地化函數

#include <math.h> //定義數學函數

#include <stdio.h> //定義輸入/輸出函數

#include <stdlib.h> //定義雜項函數及內存分配函數

#include <string.h> //字符串處理

#include <strstrea.h> //基於數組的輸入/輸出

#include <time.h> //定義關於時間的函數

#include <wchar.h> //寬字符處理及輸入/輸出

#include <wctype.h> //寬字符分類

標準 C++ (同上的不再註釋)

#include <algorithm> //STL 通用算法

#include <bitset> //STL 位集容器

#include <cctype>

#include <cerrno>

#include <clocale>

#include <cmath>

#include <complex> //複數類

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <ctime>

#include <deque> //STL 雙端隊列容器

#include <exception> //異常處理類

#include <fstream>
#include <functional> //STL 定義運算函數(代替運算符)

#include <limits>

#include <list> //STL 線性列表容器

#include <map> //STL 映射容器

#include <iomanip>

#include <ios> //基本輸入/輸出支持

#include <iosfwd> //輸入/輸出系統使用的前置聲明

#include <iostream>

#include <istream> //基本輸入流

#include <ostream> //基本輸出流

#include <queue> //STL 隊列容器

#include <set> //STL 集合容器

#include <sstream> //基於字符串的流

#include <stack> //STL 堆棧容器

#include <stdexcept> //標準異常類

#include <streambuf> //底層輸入/輸出支持

#include <string> //字符串類

#include <utility> //STL 通用模板類

#include <vector> //STL 動態數組容器

#include <cwchar>

#include <cwctype>

using namespace std;

C99 增加

#include //複數處理
#include //浮點環境
#include //整數格式轉換
#include //布爾環境
#include //整型環境
#include //通用類型數學宏數學函數,所在函數庫爲math.h、stdlib.h、string.h、float.h
int abs(int i) 返回整型參數i的絕對值
double cabs(struct complex znum) 返回複數znum的絕對值
double fabs(double x) 返回雙精度參數x的絕對值
long labs(long n) 返回長整型參數n的絕對值
double exp(double x) 返回指數函數ex的值
double frexp(double value,int *eptr) 返回value=x*2n中x的值,n存貯在eptr中
double ldexp(double value,int exp); 返回value*2exp的值
double log(double x) 返回logex的值
double log10(double x) 返回log10x的值
double pow(double x,double y) 返回xy的值
double pow10(int p) 返回10p的值
double sqrt(double x) 返回+√x的值
double acos(double x) 返回x的反餘弦cos-1(x)值,x爲弧度
double asin(double x) 返回x的反正弦sin-1(x)值,x爲弧度
double atan(double x) 返回x的反正切tan-1(x)值,x爲弧度
double atan2(double y,double x) 返回y/x的反正切tan-1(x)值,y的x爲弧度
double cos(double x) 返回x的餘弦cos(x)值,x爲弧度
double sin(double x) 返回x的正弦sin(x)值,x爲弧度
double tan(double x) 返回x的正切tan(x)值,x爲弧度
double cosh(double x) 返回x的雙曲餘弦cosh(x)值,x爲弧度
double sinh(double x) 返回x的雙曲正弦sinh(x)值,x爲弧度
double tanh(double x) 返回x的雙曲正切tanh(x)值,x爲弧度
double hypot(double x,double y) 返回直角三角形斜邊的長度(z),
x和y爲直角邊的長度,z2=x2+y2
double ceil(double x) 返回不小於x的最小整數
double floor(double x) 返回不大於x的最大整數
void srand(unsigned seed) 初始化隨機數發生器
int rand() 產生一個隨機數並返回這個數
double poly(double x,int n,double c[])從參數產生一個多項式
double modf(double value,double *iptr)將雙精度數value分解成尾數和階
double fmod(double x,double y) 返回x/y的餘數
double frexp(double value,int *eptr) 將雙精度數value分成尾數和階
double atof(char *nptr) 將字符串nptr轉換成浮點數並返回這個浮點數
double atoi(char *nptr) 將字符串nptr轉換成整數並返回這個整數
double atol(char *nptr) 將字符串nptr轉換成長整數並返回這個整數
char *ecvt(double value,int ndigit,int *decpt,int *sign)
將浮點數value轉換成字符串並返回該字符串
char *fcvt(double value,int ndigit,int *decpt,int *sign)
將浮點數value轉換成字符串並返回該字符串
char *gcvt(double value,int ndigit,char *buf)
將數value轉換成字符串並存於buf中,並返回buf的指針
char *ultoa(unsigned long value,char *string,int radix)
將無符號整型數value轉換成字符串並返回該字符串,radix爲轉換時所用基數
char *ltoa(long value,char *string,int radix)
將長整型數value轉換成字符串並返回該字符串,radix爲轉換時所用基數
char *itoa(int value,char *string,int radix)
將整數value轉換成字符串存入string,radix爲轉換時所用基數
double atof(char *nptr) 將字符串nptr轉換成雙精度數,並返回這個數,錯誤返回0
int atoi(char *nptr) 將字符串nptr轉換成整型數, 並返回這個數,錯誤返回0
long atol(char *nptr) 將字符串nptr轉換成長整型數,並返回這個數,錯誤返回0
double strtod(char *str,char **endptr)將字符串str轉換成雙精度數,並返回這個數,
long strtol(char *str,char **endptr,int base)將字符串str轉換成長整型數,
並返回這個數,
int matherr(struct exception *e)
用戶修改數學錯誤返回信息函數(沒有必要使用)
double _matherr(_mexcep why,char *fun,double *arg1p,
double *arg2p,double retval)
用戶修改數學錯誤返回信息函數(沒有必要使用)
unsigned int _clear87() 清除浮點狀態字並返回原來的浮點狀態
void _fpreset() 重新初使化浮點數學程序包
unsigned int _status87() 返回浮點狀態字目錄函數,所在函數庫爲dir.h、dos.h
int chdir(char *path) 使指定的目錄path(如:”C:\\WPS”)變成當前的工作目錄,成
功返回0
int findfirst(char *pathname,struct ffblk *ffblk,int

attrib)查找指定的文件,成功
返回0
pathname爲指定的目錄名和文件名,如”C:\\WPS\\TXT”
ffblk爲指定的保存文件信息的一個結構,定義如下:
┏━━━━━━━━━━━━━━━━━━┓
┃struct ffblk┃
┃{ ┃
┃ char ff_reserved[21]; /*DOS保留字*/┃
┃ char ff_attrib; /*文件屬性*/ ┃
┃ int ff_ftime; /*文件時間*/ ┃
┃ int ff_fdate; /*文件日期*/ ┃
┃ long ff_fsize; /*文件長度*/ ┃
┃ char ff_name[13]; /*文件名*/ ┃
┃} ┃
┗━━━━━━━━━━━━━━━━━━┛
attrib爲文件屬性,由以下字符代表
┏━━━━━━━━━┳━━━━━━━━┓
┃FA_RDONLY 只讀文件┃FA_LABEL 卷標號┃
┃FA_HIDDEN 隱藏文件┃FA_DIREC 目錄┃
┃FA_SYSTEM 系統文件┃FA_ARCH 檔案┃
┗━━━━━━━━━┻━━━━━━━━┛
例:
struct ffblk ff;
findfirst(“*.wps”,&ff,FA_RDONLY);
int findnext(struct ffblk *ffblk) 取匹配finddirst的文件,成功返回0
void fumerge(char *path,char *drive,char *dir,char *name,char *ext)
此函數通過盤符drive(C:、A:等),路徑dir(\TC、\BC\LIB等),
文件名name(TC、WPS等),擴展名ext(.EXE、.COM等)組成一個文件名
存與path中.
int fnsplit(char *path,char *drive,char *dir,char *name,char *ext)
此函數將文件名path分解成盤符drive(C:、A:等),路徑dir(\TC、\BC\LIB等),
文件名name(TC、WPS等),擴展名ext(.EXE、.COM等),並分別存入相應的變量中.
int getcurdir(int drive,char *direc) 此函數返回指定驅動器的當前工作目錄名稱
drive 指定的驅動器(0=當前,1=A,2=B,3=C等)
direc 保存指定驅動器當前工作路徑的變量成功返回0
char *getcwd(char *buf,iint n) 此函數取當前工作目錄並存入buf中,直到n個字
節長爲爲止.錯誤返回NULL
int getdisk() 取當前正在使用的驅動器,返回一個整數(0=A,1=B,2=C等)
int setdisk(int drive) 設置要使用的驅動器drive(0=A,1=B,2=C等),
返回可使用驅動器總數
int mkdir(char *pathname) 建立一個新的目錄pathname,成功返回0
int rmdir(char *pathname) 刪除一個目錄pathname,成功返回0
char *mktemp(char *template) 構造一個當前目錄上沒有的文件名並存於template中
char *searchpath(char *pathname) 利用MSDOS找出文件filename所在路徑,
,此函數使用DOS的PATH變量,未找到文件返回NULL 進程函數,所在函數庫爲stdlib.h、process.h
void abort() 此函數通過調用具有出口代碼3的_exit寫一個終止信息於stderr,
並異常終止程序。無返回值
int exec…裝入和運行其它程序
int execl( char *pathname,char *arg0,char *arg1,…,char *argn,NULL)
int execle( char *pathname,char *arg0,char *arg1,…,
char *argn,NULL,char *envp[])
int execlp( char *pathname,char *arg0,char *arg1,…,NULL)
int execlpe(char *pathname,char *arg0,char *arg1,…,NULL,char *envp[])
int execv( char *pathname,char *argv[])
int execve( char *pathname,char *argv[],char *envp[])
int execvp( char *pathname,char *argv[])
int execvpe(char *pathname,char *argv[],char *envp[])
exec函數族裝入並運行程序pathname,並將參數
arg0(arg1,arg2,argv[],envp[])傳遞給子程序,出錯返回-1
在exec函數族中,後綴l、v、p、e添加到exec後,
所指定的函數將具有某種操作能力
有後綴 p時,函數可以利用DOS的PATH變量查找子程序文件。
l時,函數中被傳遞的參數個數固定。
v時,函數中被傳遞的參數個數不固定。
e時,函數傳遞指定參數envp,允許改變子進程的環境,
無後綴e時,子進程使用當前程序的環境。
void _exit(int status)終止當前程序,但不清理現場
void exit(int status) 終止當前程序,關閉所有文件,寫緩衝區的輸出(等待輸出),
並調用任何寄存器的”出口函數”,無返回值
int spawn…運行子程序
int spawnl( int mode,char *pathname,char *arg0,char *arg1,…,
char *argn,NULL)
int spawnle( int mode,char *pathname,char *arg0,char *arg1,…,
char *argn,NULL,char *envp[])
int spawnlp( int mode,char *pathname,char *arg0,char *arg1,…,
char *argn,NULL)
int spawnlpe(int mode,char *pathname,char *arg0,char *arg1,…,
char *argn,NULL,char *envp[])
int spawnv( int mode,char *pathname,char *argv[])
int spawnve( int mode,char *pathname,char *argv[],char *envp[])
int spawnvp( int mode,char *pathname,char *argv[])
int spawnvpe(int mode,char *pathname,char *argv[],char *envp[])
spawn函數族在mode模式下運行子程序pathname,並將參數
arg0(arg1,arg2,argv[],envp[])傳遞給子程序.出錯返回-1
mode爲運行模式
mode爲 P_WAIT 表示在子程序運行完後返回本程序
P_NOWAIT 表示在子程序運行時同時運行本程序(不可用)
P_OVERLAY表示在本程序退出後運行子程序
在spawn函數族中,後綴l、v、p、e添加到spawn後,
所指定的函數將具有某種操作能力
有後綴 p時, 函數利用DOS的PATH查找子程序文件
l時, 函數傳遞的參數個數固定.
v時, 函數傳遞的參數個數不固定.
e時, 指定參數envp可以傳遞給子程序,允許改變子程序運行環境.
當無後綴e時,子程序使用本程序的環境.
int system(char *command) 將MSDOS命令command傳遞給DOS執行轉換子程序,函數庫爲math.h、stdlib.h、ctype.h、float.h
char *ecvt(double value,int ndigit,int *decpt,int *sign)
將浮點數value轉換成字符串並返回該字符串
char *fcvt(double value,int ndigit,int *decpt,int *sign)
將浮點數value轉換成字符串並返回該字符串
char *gcvt(double value,int ndigit,char *buf)
將數value轉換成字符串並存於buf中,並返回buf的指針
char *ultoa(unsigned long value,char *string,int radix)
將無符號整型數value轉換成字符串並返回該字符串,radix爲轉換時所用基數
char *ltoa(long value,char *string,int radix)
將長整型數value轉換成字符串並返回該字符串,radix爲轉換時所用基數
char *itoa(int value,char *string,int radix)
將整數value轉換成字符串存入string,radix爲轉換時所用基數
double atof(char *nptr) 將字符串nptr轉換成雙精度數,並返回這個數,錯誤返回0
int atoi(char *nptr) 將字符串nptr轉換成整型數, 並返回這個數,錯誤返回0
long atol(char *nptr) 將字符串nptr轉換成長整型數,並返回這個數,錯誤返回0
double strtod(char *str,char **endptr)將字符串str轉換成雙精度數,並返回這個數,
long strtol(char *str,char **endptr,int base)將字符串str轉換成長整型數,
並返回這個數,
int toascii(int c) 返回c相應的ASCII
int tolower(int ch) 若ch是大寫字母(‘A’-‘Z’)返回相應的小寫字母(‘a’-‘z’)
int _tolower(int ch) 返回ch相應的小寫字母(‘a’-‘z’)
int toupper(int ch) 若ch是小寫字母(‘a’-‘z’)返回相應的大寫字母(‘A’-‘Z’)
int _toupper(int ch) 返回ch相應的大寫字母(‘A’-‘Z’) 診斷函數,所在函數庫爲assert.h、math.h
void assert(int test) 一個擴展成if語句那樣的宏,如果test測試失敗,
就顯示一個信息並異常終止程序,無返回值
void perror(char *string) 本函數將顯示最近一次的錯誤信息,格式如下:
字符串string:錯誤信息
char *strerror(char *str) 本函數返回最近一次的錯誤信息,格式如下:
字符串str:錯誤信息
int matherr(struct exception *e)
用戶修改數學錯誤返回信息函數(沒有必要使用)
double _matherr(_mexcep why,char *fun,double *arg1p,
double *arg2p,double retval)
用戶修改數學錯誤返回信息函數(沒有必要使用) 輸入輸出子程序,函數庫爲io.h、conio.h、stat.h、dos.h、stdio.h、signal.h
int kbhit() 本函數返回最近所敲的按鍵
int fgetchar() 從控制檯(鍵盤)讀一個字符,顯示在屏幕上
int getch() 從控制檯(鍵盤)讀一個字符,不顯示在屏幕上
int putch() 向控制檯(鍵盤)寫一個字符
int getchar() 從控制檯(鍵盤)讀一個字符,顯示在屏幕上
int putchar() 向控制檯(鍵盤)寫一個字符
int getche() 從控制檯(鍵盤)讀一個字符,顯示在屏幕上
int ungetch(int c) 把字符c退回給控制檯(鍵盤)
char *cgets(char *string) 從控制檯(鍵盤)讀入字符串存於string中
int scanf(char *format[,argument…])從控制檯讀入一個字符串,分別對各個參數進行
賦值,使用BIOS進行輸出
int vscanf(char *format,Valist param)從控制檯讀入一個字符串,分別對各個參數進行
賦值,使用BIOS進行輸出,參數從Valist param中取得
int cscanf(char *format[,argument…])從控制檯讀入一個字符串,分別對各個參數進行
賦值,直接對控制檯作操作,比如顯示器在顯示時字符時即爲直接寫頻方式顯示
int sscanf(char *string,char *format[,argument,…])通過字符串string,分別對各個
參數進行賦值
int vsscanf(char *string,char *format,Vlist param)通過字符串string,分別對各個
參數進行賦值,參數從Vlist param中取得
int puts(char *string) 發關一個字符串string給控制檯(顯示器),
使用BIOS進行輸出
void cputs(char *string) 發送一個字符串string給控制檯(顯示器),
直接對控制檯作操作,比如顯示器即爲直接寫頻方式顯示
int printf(char *format[,argument,…]) 發送格式化字符串輸出給控制檯(顯示器)
使用BIOS進行輸出
int vprintf(char *format,Valist param) 發送格式化字符串輸出給控制檯(顯示器)
使用BIOS進行輸出,參數從Valist param中取得
int cprintf(char *format[,argument,…]) 發送格式化字符串輸出給控制檯(顯示器),
直接對控制檯作操作,比如顯示器即爲直接寫頻方式顯示
int vcprintf(char *format,Valist param)發送格式化字符串輸出給控制檯(顯示器),
直接對控制檯作操作,比如顯示器即爲直接寫頻方式顯示,
參數從Valist param中取得
int sprintf(char *string,char *format[,argument,…])
將字符串string的內容重新寫爲格式化後的字符串
int vsprintf(char *string,char *format,Valist param)
將字符串string的內容重新寫爲格式化後的字符串,參數從Valist param中取得
int rename(char *oldname,char *newname)將文件oldname的名稱改爲newname
int ioctl(int handle,int cmd[,int *argdx,int argcx])
本函數是用來控制輸入/輸出設備的,請見下表:
┌———┬————————————————————————————┐
│cmd值 │功能 │
├———┼————————————————————————————┤
│ 0 │取出設備信息 │
│ 1 │設置設備信息 │
│ 2 │把argcx字節讀入由argdx所指的地址 │
│ 3 │在argdx所指的地址寫argcx字節 │
│ 4 │除把handle當作設備號(0=當前,1=A,等)之外,均和cmd=2時一樣 │
│ 5 │除把handle當作設備號(0=當前,1=A,等)之外,均和cmd=3時一樣 │
│ 6 │取輸入狀態 │
│ 7 │取輸出狀態 │
│ 8 │測試可換性;只對於DOS 3.x │
│ 11 │置分享衝突的重算計數;只對DOS 3.x │
└———┴————————————————————————————┘
int (*ssignal(int sig,int(*action)())()執行軟件信號(沒必要使用)
int gsignal(int sig) 執行軟件信號(沒必要使用)
int _open(char *pathname,int access)爲讀或寫打開一個文件,
按後按access來確定是讀文件還是寫文件,access值見下表
┌——————┬————————————————————┐
│access值 │意義 │
├——————┼————————————————————┤
│O_RDONLY │讀文件 │
│O_WRONLY │寫文件 │
│O_RDWR │即讀也寫 │
│O_NOINHERIT │若文件沒有傳遞給子程序,則被包含 │
│O_DENYALL │只允許當前處理必須存取的文件 │
│O_DENYWRITE │只允許從任何其它打開的文件讀 │
│O_DENYREAD │只允許從任何其它打開的文件寫 │
│O_DENYNONE │允許其它共享打開的文件 │
└——————┴————————————————————┘
int open(char *pathname,int access[,int permiss])爲讀或寫打開一個文件,
按後按access來確定是讀文件還是寫文件,access值見下表
┌————┬————————————————————┐
│access值│意義 │
├————┼————————————————————┤
│O_RDONLY│讀文件 │
│O_WRONLY│寫文件 │
│O_RDWR │即讀也寫 │
│O_NDELAY│沒有使用;對UNIX系統兼容 │
│O_APPEND│即讀也寫,但每次寫總是在文件尾添加 │
│O_CREAT │若文件存在,此標誌無用;若不存在,建新文件 │
│O_TRUNC │若文件存在,則長度被截爲0,屬性不變 │
│O_EXCL │未用;對UNIX系統兼容 │
│O_BINARY│此標誌可顯示地給出以二進制方式打開文件 │
│O_TEXT │此標誌可用於顯示地給出以文本方式打開文件│
└————┴————————————————————┘
permiss爲文件屬性,可爲以下值:
S_IWRITE允許寫 S_IREAD允許讀 S_IREAD|S_IWRITE允許讀、寫
int creat(char *filename,int permiss) 建立一個新文件filename,並設定
讀寫性。permiss爲文件讀寫性,可以爲以下值
S_IWRITE允許寫 S_IREAD允許讀 S_IREAD|S_IWRITE允許讀、寫
int _creat(char *filename,int attrib) 建立一個新文件filename,並設定文件
屬性。attrib爲文件屬性,可以爲以下值
FA_RDONLY只讀 FA_HIDDEN隱藏 FA_SYSTEM系統
int creatnew(char *filenamt,int attrib) 建立一個新文件filename,並設定文件
屬性。attrib爲文件屬性,可以爲以下值
FA_RDONLY只讀 FA_HIDDEN隱藏 FA_SYSTEM系統
int creattemp(char *filenamt,int attrib) 建立一個新文件filename,並設定文件
屬性。attrib爲文件屬性,可以爲以下值
FA_RDONLY只讀 FA_HIDDEN隱藏 FA_SYSTEM系統
int read(int handle,void *buf,int nbyte)從文件號爲handle的文件中讀nbyte個字符
存入buf中
int _read(int handle,void *buf,int nbyte)從文件號爲handle的文件中讀nbyte個字符
存入buf中,直接調用MSDOS進行操作.
int write(int handle,void *buf,int nbyte)將buf中的nbyte個字符寫入文件號
爲handle的文件中
int _write(int handle,void *buf,int nbyte)將buf中的nbyte個字符寫入文件號
爲handle的文件中
int dup(int handle) 複製一個文件處理指針handle,返回這個指針
int dup2(int handle,int newhandle)

複製一個文件處理指針handle到newhandle
int eof(int *handle)檢查文件是否結束,結束返回1,否則返回0
long filelength(int handle) 返回文件長度,handle爲文件號
int setmode(int handle,unsigned mode)本函數用來設定文件號爲handle的文件的打
開方式
int getftime(int handle,struct ftime *ftime) 讀取文件號爲handle的文件的時間,
並將文件時間存於ftime結構中,成功返回0,ftime結構如下:
┌—————————————————┐
│struct ftime │
│{ │
│ unsigned ft_tsec:5; /*秒*/ │
│ unsigned ft_min:6; /*分*/ │
│ unsigned ft_hour:5; /*時*/ │
│ unsigned ft_day:5; /*日*/ │
│ unsigned ft_month:4;/*月*/ │
│ unsigned ft_year:1; /*年-1980*/ │
│} │
└—————————————————┘
int setftime(int handle,struct ftime *ftime) 重寫文件號爲handle的文件時間,
新時間在結構ftime中.成功返回0.結構ftime如下:
┌—————————————————┐
│struct ftime │
│{ │
│ unsigned ft_tsec:5; /*秒*/ │
│ unsigned ft_min:6; /*分*/ │
│ unsigned ft_hour:5; /*時*/ │
│ unsigned ft_day:5; /*日*/ │
│ unsigned ft_month:4;/*月*/ │
│ unsigned ft_year:1; /*年-1980*/ │
│} │
└—————————————————┘
long lseek(int handle,long offset,int fromwhere) 本函數將文件號爲handle的文件
的指針移到fromwhere後的第offset個字節處.
SEEK_SET 文件開關 SEEK_CUR 當前位置 SEEK_END 文件尾
long tell(int handle) 本函數返回文件號爲handle的文件指針,以字節表示
int isatty(int handle)本函數用來取設備handle的類型
int lock(int handle,long offset,long length) 對文件共享作封鎖
int unlock(int handle,long offset,long length) 打開對文件共享的封鎖
int close(int handle) 關閉handle所表示的文件處理,handle是從_creat、creat、
creatnew、creattemp、dup、dup2、_open、open中的一個處調用獲得的文件處理
成功返回0否則返回-1,可用於UNIX系統
int _close(int handle) 關閉handle所表示的文件處理,handle是從_creat、creat、
creatnew、creattemp、dup、dup2、_open、open中的一個處調用獲得的文件處理
成功返回0否則返回-1,只能用於MSDOS系統
FILE *fopen(char *filename,char *type) 打開一個文件filename,打開方式爲type,
並返回這個文件指針,type可爲以下字符串加上後綴
┌——┬————┬———————┬————————┐
│type│讀寫性 │文本/2進制文件│建新/打開舊文件 │
├——┼————┼———————┼————————┤
│r │讀 │文本 │打開舊的文件 │
│w │寫 │文本 │建新文件 │
│a │添加 │文本 │有就打開無則建新│
│r+ │讀/寫 │不限制 │打開 │
│w+ │讀/寫 │不限制 │建新文件 │
│a+ │讀/添加 │不限制 │有就打開無則建新│
└——┴————┴———————┴————————┘
可加的後綴爲t、b。加b表示文件以二進制形式進行操作,t沒必要使用
例: ┌——————————————————┐
│#include │
│main() │
│{ │
│ FILE *fp; │
│ fp=fopen(“C:\\WPS\\WPS.EXE”,”r+b”);│
└——————————————————┘
FILE *fdopen(int ahndle,char *type)
FILE *freopen(char *filename,char *type,FILE *stream)
int getc(FILE *stream) 從流stream中讀一個字符,並返回這個字符
int putc(int ch,FILE *stream)向流stream寫入一個字符ch
int getw(FILE *stream) 從流stream讀入一個整數,錯誤返回EOF
int putw(int w,FILE *stream)向流stream寫入一個整數
int ungetc(char c,FILE *stream) 把字符c退回給流stream,下一次讀進的字符將是c
int fgetc(FILE *stream) 從流stream處讀一個字符,並返回這個字符
int fputc(int ch,FILE *stream) 將字符ch寫入流stream中
char *fgets(char *string,int n,FILE *stream) 從流stream中讀n個字符存入string中
int fputs(char *string,FILE *stream) 將字符串string寫入流stream中
int fread(void *ptr,int size,int nitems,FILE *stream) 從流stream中讀入nitems
個長度爲size的字符串存入ptr中
int fwrite(void *ptr,int size,int nitems,FILE *stream) 向流stream中寫入nitems
個長度爲size的字符串,字符串在ptr中
int fscanf(FILE *stream,char *format[,argument,…]) 以格式化形式從流stream中
讀入一個字符串
int vfscanf(FILE *stream,char *format,Valist param) 以格式化形式從流stream中
讀入一個字符串,參數從Valist param中取得
int fprintf(FILE *stream,char *format[,argument,…]) 以格式化形式將一個字符
串寫給指定的流stream
int vfprintf(FILE *stream,char *format,Valist param) 以格式化形式將一個字符
串寫給指定的流stream,參數從Valist param中取得
int fseek(FILE *stream,long offset,int fromwhere) 函數把文件指針移到fromwhere
所指位置的向後offset個字節處,fromwhere可以爲以下值:
SEEK_SET 文件開關 SEEK_CUR 當前位置 SEEK_END 文件尾
long ftell(FILE *stream) 函數返回定位在stream中的當前文件指針位置,以字節表示
int rewind(FILE *stream) 將當前文件指針stream移到文件開頭
int feof(FILE *stream) 檢測流stream上的文件指針是否在結束位置
int fileno(FILE *stream) 取流stream上的文件處理,並返回文件處理
int ferror(FILE *stream) 檢測流stream上是否有讀寫錯誤,如有錯誤就返回1
void clearerr(FILE *stream) 清除流stream上的讀寫錯誤
void setbuf(FILE *stream,char *buf) 給流stream指定一個緩衝區buf
void setvbuf(FILE *stream,char *buf,int type,unsigned size)
給流stream指定一個緩衝區buf,大小爲size,類型爲type

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章