C語言之基本IO操作

2021-09-11 17:02:27 字數 1536 閱讀 9750

#include

#include

// 讀取文字檔案

int main(){

char * path = "/users/shaoshuaima/workspace/xcode/c_01/c_01/files/friends.txt";

file *fp = fopen(path, "r");

if (fp == null) {

printf("檔案開啟失敗...");

return 0;

// 讀取

char buff[50]; // 緩衝

while (fgets(buff,50,fp)) {

printf("%s",buff);

// 關閉

fclose(fp);

return 0;

// 寫入文字檔案

int main(){

char * path = "/users/shaoshuaima/workspace/xcode/c_01/c_01/files/friends_new.txt";

file *fp = fopen(path, "w");

char * text = "我是乙個好學生,[email protected]\n哈哈哈換行了";

fputs(text, fp);

// 關閉流

fclose(fp);

return 0;

// 計算機的檔案儲存在物理上都是二進位制

// 文字檔案和二進位制之分,其實是乙個邏輯之分

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

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

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

int main(){

char * read_path = "/users/shaoshuaima/workspace/xcode/c_01/c_01/files/lo**iewpro.exe";

char * write_path = "/users/shaoshuaima/workspace/xcode/c_01/c_01/files/lo**iewpro_new.exe";

// 讀的檔案 b字元表示操作二進位制檔案binary

file *read_fp = fopen(read_path,"rb");

// 寫的檔案

file *write_fp = fopen(write_path,"wb");

// 複製

int buff[50]; // 快取區域

int len = 0; // 每次讀取到的資料長度

while ((len = fread(buff, sizeof(int), 50, read_fp))!= 0) {

// 將讀到的內容寫入新的檔案

fwrite(buff, sizeof(int), len, write_fp);

// 關閉流

fclose(read_fp);

fclose(write_fp);

return 0;

c語言之基本結構

程式是由語句組成的,然後程式都可以分為三種基本結構,即順序結構,選擇結構和迴圈結構。結構和流程圖息息相關,通過流程圖可以更好的了解程式結構。順序結構 結構中的語句按期先後順序依次執行。常見的輸入,計算,輸出三部曲的結構就是順序結構。選擇結構 其實就是讓程式本身具有判斷和選擇的能力。常見的選擇結構語法...

c語言之基本語句

c語言之 一,基本語句 換行 n a 32 a 三個小知識點 float a 9999.999 int b b int a 1000 1.固定格式 include int main 2.printf語句 include int main 4.printf語句輸出int資料型別 d include i...

C語言之檔案操作

fseek 函式 int fseek file stream,long offset,int whence 功能分析 第乙個引數為檔案指標,第二個引數表示偏移量,第三個引數表示從什麼地方開始偏移 返回值為0 表示成功,1表示失敗 whence的數值 seek set 檔案的起始位置 0 seek c...