C語言中有哪些高階I/O函式?
I/O指的是C語言中的輸入輸出函式。
高階I/O
- 這些很容易被人理解。
- 優點是可移植性。
低階I/O
- 這些很容易被計算機理解。
- 優點是執行時間較短。
- 缺點是不可移植。
高階I/O函式
下面解釋高階輸入輸出(I/O)函式:
函式 | 描述 |
---|---|
fprintf ( ) | 將資料寫入檔案 |
fscanf ( ) | 從檔案讀取資料 |
putc ( )/ fputc() | 將字元寫入檔案 |
getc ( ) /fgetc() | 從檔案讀取字元 |
putw ( ) | 將數字寫入檔案 |
getw ( ) | 從檔案讀取數字 |
fputs ( ) | 將字串寫入檔案 |
fgets ( ) | 從檔案讀取字串 |
fread() | 從檔案讀取整個記錄 |
fwrite() | 將整個記錄寫入檔案 |
fprintf ( ) & fscanf ( ) 函式
- fprintf ( )
語法如下:
fprintf (file pointer, " control string”, variable list)
例如:
FILE *fp; fprintf (fp, "%d%c”, a,b);
- fscanf ( )
語法如下:
fscanf(file pointer, "control string”, & variable list);
例如:
FILE *fp; fscanf (fp, "%d%c”, &a,&b);
putc( ) 和 getc( ) 函式
- putc ( )
用於將字元寫入檔案。
語法如下:
putc (char ch, FILE *fp);
例如:
FILE *fp; char ch; putc(ch, fp);
- getc ( )
用於從檔案讀取字元。
語法如下:
char getc (FILE *fp);
例如:
FILE *fp; char ch; ch = getc(fp);
putw ( ) 和 getw ( ) 函式
- putw( )
用於將數字寫入檔案。
語法如下:
putw (int num, FILE *fp);
例如:
FILE *fp; int num; putw(num, fp);
- getw ( )
用於從檔案讀取數字。
語法如下:
int getw (FILE *fp);
例如:
FILE *fp; int num; num = getw(fp);
fputc ( ) 和 fgetc ( ) 函式
- fputc( )
用於將字元寫入檔案。
語法如下:
fputc (char ch, FILE *fp);
例如:
FILE *fp; char ch; fputc (ch.fp);
- fgetc( )
用於從檔案讀取字元。
語法如下:
fputc (char ch, FILE *fp);
例如:
FILE *fp; char ch; ch = fgetc(fp);
fgets ( ) 和 fputs ( ) 函式
- fgets ( )
用於從檔案讀取字串。
語法如下:
fgets (string variable, No. of characters, File pointer);
例如:
FILE *fp; char str [30]; fgets (str,30,fp);
- fputs ( )
用於將字串寫入檔案。
語法如下:
fputs (string variable, file pointer);
例如:
FILE *fp; char str[30]; fputs (str,fp);
fread ( ) 和 fwrite ( ) 函式
- fread ( )
用於一次讀取整個記錄。
語法如下:
fread( & structure variable, size of (structure variable), no of records, file pointer);
例如:
struct emp{ int eno; char ename [30]; float sal; } e; FILE *fp; fread (&e, sizeof (e), 1, fp);
- fwrite ( )
用於一次寫入整個記錄。
語法如下:
fwrite( & structure variable , size of structure variable, no of records, file pointer);
例如:
struct emp{ int eno: char ename [30]; float sal; } e; FILE *fp; fwrite (&e, sizeof(e), 1, fp);
示例程式
以下是將數字1到10儲存到檔案中並列印它們的C程式:
//Program for storing no’s from 1 to 10 and print the same #include<stdio.h> int main( ){ FILE *fp; int i; fp = fopen ("num.txt", "w"); for (i =1; i<= 10; i++){ putw (i, fp); } fclose (fp); fp =fopen ("num.txt", "r"); printf ("file content is"); for (i =1; i<= 10; i++){ i= getw(fp); printf ("%d",i); } fclose (fp); return 0; }
輸出
執行上述程式後,將產生以下結果:
file content is12345678910
下面是另一個C程式,它使用fread()和fwrite()將5個學生的詳細資訊儲存到檔案中並打印出來:
示例
#include<stdio.h> struct student{ int sno; char sname [30]; float marks; char temp; }; main ( ){ struct student s[60]; int i; FILE *fp; fp = fopen ("student1.txt", "w"); for (i=0; i<2; i++){ printf ("enter details of student %d
", i+1); printf("student number:"); scanf("%d",&s[i].sno); scanf("%c",&s[i].temp); printf("student name:"); gets(s[i].sname); printf("student marks:"); scanf("%f",&s[i].marks); fwrite(&s[i], sizeof(s[i]),1,fp); } fclose (fp); fp = fopen ("student1.txt", "r"); for (i=0; i<2; i++){ printf ("details of student %d are
", i+1); fread (&s[i], sizeof (s[i]) ,1,fp); printf("student number = %d
", s[i]. sno); printf("student name = %s
", s[i]. sname); printf("marks = %f
", s[i]. marks); } fclose(fp); getch( ); }
輸出
執行上述程式後,將產生以下結果:
enter details of student 1 student number:1 student name:bhanu student marks:50 enter details of student 2 student number:2 student name:priya student marks:69 details of student 1 are student number = 1 student name = bhanu marks = 50.000000 details of student 2 are student number = 2 student name = priya marks = 69.000000
廣告