四種讀寫檔案的方式:系統呼叫(open/read/write),C語言(fopen,fgets, fputs),C++(ifstream, ofstream getline,)泛型演算法
第一種方法是系統呼叫
(1)open系統呼叫
原型:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
作用:
用flag 的方式開啟路徑名為path 的檔案
引數:
第一個引數是路徑名,第二個引數是開啟檔案的方式(O_RDONLY, O_WRONLY, O_RDWR)
(2)read 系統呼叫
原型:
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
作用:
把檔案描述符所描述的檔案裡面的內容讀到buf 裡面,指定讀count 個位元組
小插曲:
位元組和字元的區別
(a)"位元組":位元組是一種計量單位,表示資料量的多少,是計算機用來儲存容量的一種計量單位
(b)"字元":計算機使用的文字和符號
(c)區別:
ASCII 碼中,一個英文字母佔一個位元組的空間,一箇中文漢字佔兩個位元組的空間
UTF_8 編碼中,一個英文字母等於一個位元組,一箇中文字母等於三個位元組
UNicode編碼中,一個英文等於兩個位元組,一箇中文等於兩個位元組,英文標點佔一個位元組,中文標點佔兩個位元組
UTF-16 編碼中,一個英文字母或者一個漢字都需要兩個位元組
UTF-32編碼中,世界上任何字元的儲存都需要4個位元組
(3)write 系統呼叫
原型:
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
作用:
把buffer 中count 位元組的資料寫到fd 檔案描述符所指的檔案中
測試:
結果:#include<sys/stat.h> #include<unistd.h> #include<fcntl.h> #include<sys/types.h> #include<iostream> using namespace std; int main() { int fd1 = open("/home/cjy/test.cpp", O_RDONLY); int fd2 = open("/home/cjy/test1.cpp", O_WRONLY); char buf[10240]; size_t nread = read(fd1, buf, 10240); size_t nwrite = write(fd2, buf, nread); cout<<buf<<endl; cout<<nread<<endl; cout<<nwrite<<endl; }
我們再來看一下test1.cpp 裡面的內容
OK,驗證成功
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
第二種方法是C語言檔案的操作(fopen/fgets/fputs)
(1)fopen函式
原型:
#include <stdio.h>
FILE *fopen(const char *path, const char *mode);
作用:以mode 的方式開啟路徑名為path 的檔案
(2)fgets函式
原型:
char *fgets(char *s, int size, FILE *stream);
功能:從stream 所指的檔案中讀取大於1小於size 的位元組數,把它們儲存到s 中,讀取到EOF標誌或者讀取到新的一行的時候,讀取結束,‘\0’被儲存在最後
(3)fputs函式
原型:
int fputs(const char *s, FILE *stream);
功能:把s所指的緩衝區的內容讀取到stream 所指的檔案中,直到遇到‘\0’就結束
成功返回一個非負的數字,失敗則返回EOF
程式碼驗證:
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
FILE *fp1 = fopen("/home/cjy/test.cpp", "r");
FILE *fp2 = fopen("/home/cjy/test1.cpp", "w");
char s[10240];
while(fgets(s,10240, fp1))
{
cout<<s;
fputs(s, fp2);
}
fclose(fp1);
cout<<endl;
return 0;
}
結果:
對比test1.cpp 的內容
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
第三種方法是:C++裡面的ifstream
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
ifstream fin("/home/cjy/test.cpp");
ofstream fout("/home/cjy/test1.cpp");
string s;
while(getline(fin, s))
{
fout<<s<<endl;
cout<<s<<endl;
}
fin.close();
fout.close();
return 0;
}
結果:檢視test1.cpp 裡面的內容
____________________________________________________________________________________________________________
第四中方法是泛型演算法
#include<iterator>
#include<algorithm>
#include<fstream>
#include<vector>
#include<iostream>
using namespace std;
int main()
{
ifstream fin("/home/cjy/test.cpp");
vector<string>content;
istream_iterator<string>begin(fin);
istream_iterator<string>end;
copy(begin, end, back_inserter(content));
copy(content.begin(), content.end(), ostream_iterator<string>(cout, "\n"));
return 0;
}
結果:
也可以用繼承和過載的方法:
#include<iostream>
#include<iterator>
#include<fstream>
#include<algorithm>
#include<vector>
using namespace std;
class myline:public string{};
istream& operator>>(istream &is, myline &line)
{
getline(is, line);
return is;
}
int main()
{
ifstream fin("/home/cjy/test.cpp");
istream_iterator<myline>begin(fin);
istream_iterator<myline>end;
vector<string> content;
copy(begin, end, back_inserter(content));
copy(content.begin(), content.end(), ostream_iterator<string>(cout, "\n"));
return 0;
}
結果驗證:
相關推薦
四種讀寫檔案的方式:系統呼叫(open/read/write),C語言(fopen,fgets, fputs),C++(ifstream, ofstream getline,)泛型演算法
第一種方法是系統呼叫 (1)open系統呼叫 原型: #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>
總結:linux驅動之I2C至少四種讀寫方法
總結在linux環境下四種讀寫I2C裝置的方法:一:讀寫/dev/i2c-x裝置結點 /dev/i2c-x裝置結點對應的驅動檔案為核心目錄drivers/i2c下自帶的i2c-dev.c檔案,通讀此檔案可以發現它的工作流程。 1、通過register_chrde
四種讀寫方案IO流 (JAVA)
File類用於訪問檔案或目錄的屬性 流:指一連串流動的字元,是以先進先出的方式傳送資訊的通道。程式和資料來源之間是通過流聯絡起來的。 第一套:位元組流讀取寫入方案 FileInputStream :位元組流方式讀取文字檔案 FileInputStream fi
python 4種讀寫檔案方法
#encoding:utf-8 """ @project_name = pytest @file = demo_readAndWrite_File.py @author = angel @create_
【Unity3D外掛】在Unity中讀寫檔案資料:LitJSON快速教程
介紹 JSON是一個簡單的,但功能強大的序列化資料格式。它定義了簡單的型別,如布林,數(int和float)和字串,和幾個資料結構:list和dictionnary。可以在http://JSON.org瞭解關於JSON的更多資訊。 litjson是用C #編寫的,它的目的是要小,快速,易用。它使
fstream與 C 風格(例如fread 和 fwrite )兩種讀寫檔案方法的效率比較
為了探錄c++ 風格的fstream與 C 風格(例如fread 和 fwrite )兩種讀寫檔案的方法的效率,我特意做了兩個實驗。 我的機器是Windows XP, Visual Studio 2008 1. 測試寫檔案速度 程式設計思路: 將TEST_SIZE個
Linux系統函數open,read,write
() lseek sed fse prot 地址 stdio.h buffer pri 實現代碼 #include<sys/stat.h> #include<sys/types.h> #include<fcntl.h> #include
java指定編碼的按行讀寫txt檔案(幾種讀寫方式的比較)
輸入輸出的幾種形式 1.FileReader,FileWriter File r = new File("temp.txt") FileReader f = new FileReader(name);//讀取檔案name BufferedReader b = new Buf
Python讀寫檔案的幾種不同方式,特別對出現‘b’的解讀
一、Python檔案讀寫的幾種模式: 'r':預設值,表示從檔案讀取資料。 'w':表示要向檔案寫入資料,並截斷以前的內容 'a':表示要向檔案寫入資料,新增到當前內容尾部 'r+':表示對檔案進行可讀寫操作(刪除以前的所有資料) 'r+a':表示對檔案可進行讀寫操作(新增到當前檔案尾部)
Go實戰--golang中讀寫檔案的幾種方式
讀寫檔案應該是在開發過程中經常遇到的,今天要跟大家一起分享的就是在golang的世界中,如何讀寫檔案。 使用io/ioutil進行讀寫檔案 其中提到了兩個方法: func ReadFile func ReadFile(filename string) ([]by
java.io幾種讀寫文件的方式
同事 com pan 語言 格式 str 字節流 get 實用 一、Java把這些不同來源和目標的數據都統一抽象為數據流。 Java語言的輸入輸出功能是十分強大而靈活的。 在Java類庫中,IO部分的內容是很龐大的,因為它涉及的領域很廣泛:標準輸入輸出,文件的操作,
Linux下用root許可權新增使用者,並給使用者或使用者組指定目錄的讀寫許可權(比如:tomcat檔案的讀寫許可權,可以用於新使用者部署專案)
目的: 在linux環境中為了安全起見,不能讓所有專案都用root部署(防止root許可權過大,對重要檔案的誤操作),所以新加使用者或使用者組,對新使用者或使用者組授予部分檔案操作的許可權。 1.建立使用者newuser,並設定密碼(預設連帶建立newuser組) # useradd n
SpringBoot四種讀取properties檔案的方式
原文地址:https://www.imooc.com/article/18252 一、@ConfigurationProperties方式 自定義配置類:PropertiesConfig.java package com.zyd.property.config; import java
C# & JAVA:讀寫檔案
using System; using System.IO; using System.Text; namespace ConsoleApplication4 { class Program { public static void Read(string path) { FileS
系統學習 Java IO (十四)----字元讀寫快取和回退 BufferedReader/BufferedWriter & PushbackReader
目錄:系統學習 Java IO---- 目錄,概覽 BufferedReader BufferedReader 類構造器接收一個 Reader 物件,為 Reader 例項提供緩衝。 緩衝可以加快 IO 的速度。 BufferedReader 不是一次從網路或磁碟讀取一個字元,而是一次讀取一個更大的塊。
C++ :四種強制型別轉換方式 以及const常量摺疊
遇到的幾個問題: (1)reinterept_cast:Int和float不能轉換 :傳引用 (2)cons_cast:去掉const屬性,在編譯階段 const int num = 12; *c++裡
C#讀寫檔案宜取方式.md
C#讀寫檔案宜取方式 首先,不推薦用如下方式讀寫 //讀 using (FileStream fs = File.OpenRead(filePath)) { byte[] b = new byt
C語言二進位制方式讀寫檔案資料
fwrite(buffer, size, count,fp):以二進位制的形式向指定的檔案中寫入若干資料項(由count決定),返回實際寫入的資料項數目,各引數含義如下: buffer:一個儲存區的起始地址,以該地址開始的儲存區的資料即是儲存到檔案中的資料,可以是陣列或指標型別; siz
Python:python3類-安裝和使用庫-讀寫檔案
一、類 init方法必須有self引數 二、安裝和使用庫 1.使用庫 2.安裝庫 pip install resquests import requests html = rewuests.get(‘http://king.info’).content
cordova檔案系統建立資料夾與讀寫檔案例項
一、cordova 檔案系統 1. 安裝外掛 cordova 需要安裝外掛才能對對手機檔案系統進行操作 cordova plagin add cordova-plugin-file 2. 需要了解的外掛api 1 . cordova.file.externalRootD