NDK學習之C語言IO

2021-08-10 07:41:34 字數 4274 閱讀 7449

//1.讀寫文字檔案

#define _crt_secure_no_warnings

#include

#include

void

main()

//讀取檔案

char

buff[50];//緩衝區

while (fgets(buff,50,f_read))

//關閉檔案

fclose(f_read);

fclose(f_write);

getchar();

}

//2.讀寫二進位制檔案

//c讀寫文字檔案與二進位制檔案的差別僅僅體現在回車換行符

//1.寫文字時,每遇到乙個'\n',會將其轉換成'\r\n'(回車換行)

//2.讀文字時,每遇到乙個'\r\n',會將其轉換成'\n'

//3.但是讀寫二進位制檔案的例子(的複製)

#define _crt_secure_no_warnings

#include

#include

void

main()

//讀取檔案

//緩衝區,注意讀寫二進位制檔案的時候,是用int型別的緩衝區

intbuff[50];

//每次讀取的長度

intlen

=0;

//讀到的內容放到緩衝區,緩衝區的單位大小(乙個緩衝區大小讀50個int的大小(4個位元組)的資料)

//也就是一次讀50*4 200位元組的資料

//返回的len 是讀取到的長度,小於等於50

while ((len

=fread(buff, sizeof(int), 50, f_read)) !=

0)

//關閉檔案

fclose(f_read);

fclose(f_write);

getchar();

}

//獲取檔案大小

//函式一 fseek

//函式二 ftell

#define _crt_secure_no_warnings

#include

#include

void

main()

//seek_end 到檔案的結尾

//0l 代表向前偏移幾個位元組

fseek(f_read,0l,seek_end);

//返回當前的檔案指標相當於檔案開頭的位移量

long

len=

ftell(f_read);

printf("%ld",len);

//關閉檔案

fclose(f_read);

getchar();

}

//檔案加密

//用簡單的異或運算進行加密,解密的話就是乙個逆向過程

//規則:1^1=0,0^0=0,0^1=1,1^0=1 同為0,不同為1

#define _crt_secure_no_warnings

#include

#include

void

crpypt(char*

filepath,char*

crpyptfilepath)

intch;//一次讀取乙個字元,每乙個字元都是乙個資料,用int來表示

while ((ch

=fgetc(f_read))!=

eof)

//關閉檔案

fclose(f_read);

fclose(f_write);

}

void

main()

//二進位制檔案加密

//讀取二進位制檔案中的資料時,乙個乙個字元讀取

//密碼 password

#define _crt_secure_no_warnings

#include

#include

void

crpypt(char*

normal_path, char*

crpypt_path,char

password)

//關閉檔案

fclose(normal_fp);

fclose(crpypt_fp);

}

void

main()

總結:

1.普通的:fopen(

path,"r"

)  fclose(

f_read

) fopen(

path,"w"

)  fclose(

f_write

)

二進位制:

fopen(

path,"rb"

)  fclose(

f_read

)

fopen(

path,"wb"

)  fclose(

f_write)

2.file* fp  返回的是檔案指標

3.fgets()  fputs()

4.fgetc()  fputc()

5.buff  緩衝區

6.fread函式    

fread(buff, sizeof(int), 50, f_read)

7.fwrite函式   

fwrite(buff, sizeof(int), len, f_write)

NDK學習之C語言結構體struct例子

include include include include 定義乙個girl結構體,包括屬性和方法 typedef struct girl girl 給結構體取了乙個別名girl 別名可以與結構體原來的名字相同 girl結構體指標取別名girlp typedef girl girlp 結構體的成...

ndk系列 C語言01

define crt secure no warnings 巨集定義 引入標頭檔案 只有函式的宣告,編譯時會去找到函式的實現 include include includevoid main 基本資料型別 int short long float double char int d short d ...

C 學習之IO流

主要i o流類 格式化i o 1 格式化函式 成員函式 cout 10 3.0 endl cout.precision 10 cout 10 3.0 endl 3333 2 流控制符 全域性函式 include cout 10 3.0 endl cout setprecision 10 10 3.0...