程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C指針編程之道 ---第六次筆記

C指針編程之道 ---第六次筆記

編輯:關於C語言

C指針編程之道 ---第六次筆記


//指向文件類型的指針
//在C語言的文件你的讀寫一般使用系統的庫函數來對數據進行讀寫
//文件的類型的指針
//文件的結構
#include
#include
using namespace std;
typedef struct
{
short level; //緩沖區滿或者是空的程度
unsigned flags; //文件狀態標志
char fd; //文件描述符
unsigned hold; //如無緩沖區不讀取字符
short basize; //緩沖區的大小
unsigned char *buffer; //數據緩沖區的位置
unsigned char *curp; //指針當前的指向
unsigned istemp; //臨時文件
short token; //用於有效性的檢查
}FILE;




//指向文件的指針
FILE *fp;


//例如下面的例子
//輸入一個字符並且顯示在屏幕
#include
int main()
{
char ch = getchar();
putchar();
return 0;
}


//scanf()函數原型是
//int scanf(char const *format...);
//
//putchar()函數
//原型是
//int putchar(int character)
//例子
#include
int main()
{
char ch = 'a';
puchar(ch); //輸出字符a
putchar('\n');
putchar('abc'); //只輸出一個字符
putchar('\101'); //輸出字符'A'
putchar('\015'); //輸出回車,不換行,使光標當前位置移到本行的開頭
putchar('\''); //輸出單引號字符
return 0;
}




//文件加工場
//功能 ===== //函數
//打開或關閉 fopen(),fclose();
//讀寫字符 fgetc(),fputc();
//讀寫字符串 fgets(),fputs();
//按數據塊讀寫 fread(),fwrite()
//格式化讀寫 fscanf(),fprintf()
//文件定位 fseek(),rewind(),ftell()
//判斷文件是否結束 feof();
//
//應用舉例:
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
//判斷參數個數
if(argc != 3)
{
printf("the number of arguments not correct!\n");
exit(1);
}


FILE *fpr, *fpw;
//打開源文件
if((fpr = fopen(argv[1], 'r')) == NULL)
{
printf("open source file error!");
exit(1);
}


//打開目標文件
if((fpw = fopen(argv[2], 'w')) == NULL)
{

printf("open destination file error!");
exit(1);
}


char ch;
//復制源文件到目標文件,每次讀寫一個字符
while((ch = fget(fpr)) != EOF)
{
fputc(ch, fpw);
}
//關閉源文件,目標文件
fclose(fpr);
fclose(fpw);
return 0;

}



//合並兩個已經按照遞增排序的順序的整數文件成為一個按遞增的文件排序的文件
#include
#include
using namespace std;


int main()
{
FILE *fp1;
FILE *fp2;
FILE *fp3;
int t1, t2;
if(agrc != 4)
{
printf("The number of arguments not correct!\n");
exit(1);
}




if((fp1 = fopen(argv[1], "r")) == NULL)
{
printf("open source file1 error\n");
exit(1);
}


if((fp2 = fopen(argv[2], "r")) == NULL)
{
printf("open source file2 error\n");
exit(1);
}


if((fp3 = fopen(argv[3], "r")) == NULL)
{
printf("open source file3 error\n");
exit(1);
}


fread(&t1, sizeof(int), 1, fp1);
fread(&t2, sizeof(int), 2, fp2);

while(! feof(fp1) && !feof(fp2))
{
if(t1 < t2)
{
fwrite(&t1.sizeof(int), 1, fp3);
fread(&t1, sizeof(int), 1, fp);
}
else
{
fwriter(&t2, sizeof(int), 1, fp3);
fwriter(&t2, sizeof(int), 1, fp2);
}
}


while(! feof(fp1))
{
fwrite(&t1.sizeof(int), 1, fp3);
fwrite(&t1,sizeof(int), 1, fp1);
}


while(!feof(fp2))
{
fwrite(&t2, sizeof(int), 1, fp3);
fwrite(&t2, sizeof(int), 1, fp2);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved