(C/C++) FILE 讀寫檔案操作
在C/C++ 讀寫檔案操作比較常見應該是利用 FILE、ifstream、ofstream
在這篇筆記裡頭記錄 FILE、fstream 使用方法及操作
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream> using namespace std; int main()
{
/*
r : open for reading
rb : open for reading in binary mode
w : open for writing
wb : open for writing in binary mode
r+ : support read and write. the file must exit.
w+ : it like r+ funciton that will recover the file with same file name if file name exit.
*/
FILE *pFile; /* open and create the file */
pFile = fopen("temp.txt", "w+");
char buffer[] = "write string test";
char *rd_buffer;
/* write the buffer to text file */
fwrite(buffer, sizeof(char), sizeof(buffer), pFile);
/* close file */
fclose(pFile); pFile = fopen("temp.txt", "r+");
/* assign read buffer size */
rd_buffer = (char*)malloc(sizeof(char)* sizeof(buffer));
/* read file data to read file */
fread(rd_buffer, sizeof(char), sizeof(buffer), pFile);
cout << rd_buffer << endl;
/* close file */
fclose(pFile); system("pause");
return ;
}
在開始進行讀寫之前有一段註解,裡頭主要標示在fopen開檔之後必須填入的參數代表啥意思
/*
r : open for reading
rb : open for reading in binary mode
w : open for writing
wb : open for writing in binary mode
r+ : support read and write. the file must exit.
w+ : it like r+ funciton that will recover the file with same file name if file name exit.
*/
w/r 就很簡易的只是 : 只能讀 / 只能寫,wb/rb 多個b表示2進制的檔案操作
r+/w+ 多+ : 在操作上就是能讀能寫但是 r+ 有囑意是這個檔案必須存在
在讀檔案的時,有時需求是直接讀一行,利用feof函式去判斷是否讀到最後一行
while (!feof(pFile))
{
fgets(data, , pFile);
cout << data;
}
以下是fsteram 檔案基本操作,之後有遇到比較困難的應用在來上頭記錄
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream> using namespace std; int main()
{
fstream file; /* write file operation */
file.open("temp.txt", ios::out);
file << "write data to file" << endl;
file << "fstream write file to test" << endl;
file << "fstream line 1 test" << endl;
file << "fstream line 2 test" << endl;
file << "fstream line 3 test" << endl;
file << "fstream line 4 test" << endl;
file << "fstream line 5 test" << endl;
file << "fstream line 6 test" << endl;
file << "fstream line 7 test" << endl;
file << "fstream line 8 test" << endl;
file.close(); /* read file operation */
file.open("temp.txt", ios::in);
while (!file.eof()){
char buf[];
file.getline(buf, );
cout << buf << endl;
}
file.close(); system("pause");
return ;
}
(C/C++) FILE 讀寫檔案操作的更多相关文章
- C++ 檔案、資料夾、路徑處理函式庫:boost::filesystem
原帖:https://tokyo.zxproxy.com/browse.php?u=uG7kXsFlW1ZmaxKEvCzu8HrCJ0bXIAddA1s5dtIUZ%2FYzM1u9JI7jjKLT ...
- [ASP.NET] 檔案讀寫權限問題
今天遇到一個問題,環境如下: IIS Server: Server 2008 R2 沒加域 File Server: Server 2003 加域 當我的Web程序需要把位於File Server的一 ...
- 在 Server 端存取 Excel 檔案的利器:NPOI Library
转处 http://msdn.microsoft.com/zh-tw/ee818993.aspx Codeplex 軟體套件(Package)資訊 套件名稱 NPOI 作者 tonyqus, huse ...
- [ASP.NET] 如何利用Javascript分割檔案上傳至後端合併
最近研究了一下如何利用javascript進行檔案分割上傳並且透過後端.特地記錄一下相關的用法 先寫限制跟本篇的一些陷阱 1.就是瀏覽器的支援了 因為本篇有用到blob跟webworker 在ie中需 ...
- C# 選擇本機檔案並上傳
參考自:http://www.dotblogs.com.tw/puma/archive/2008/11/07/5910.aspxhttp://www.codeproject.com/Articles/ ...
- 使用Device IO Control 讀寫 USB Mass Storage
http://www.ezblog.idv.tw/Download/USBStorage.rar 這是一個不透過檔案系統,去讀寫USB Mass Storage 任何位置(包含FAT)的方式 首先需安 ...
- mysql proxy讀寫分流(二)-加入RW splitting
上一篇中提到 安裝LUA及MySQL Proxy後,接下來就是RW splitting(讀寫分流)的部份了 整體的概念圖跟上一篇MySQL Proxy安裝方式相同,丫忠再補上一個對應port的圖表: ...
- 在 React Native 中使用 moment.js 無法載入語系檔案
moment.js 是很常見的日期時間 library,友善的 API 與極佳的執行效率是它的兩大賣點.例如 (new Date()).getFullYear(),如果使用 moment.js 我可以 ...
- 何解決 LinqToExcel 發生「無法載入檔案或組件」問題何解決 LinqToExcel 發生「無法載入檔案或組件」問題
在自己的主機上透過 Visual Studio 2013 與 IISExpress 開發與測試都還正常,但只要部署到測試機或正式機,就是沒辦法順利執行,卡關許久之後找我協助.我發現錯誤訊息確實很「一般 ...
随机推荐
- 小程序报错Do not have xx handler in current page的解决方法
看到小程序这一大串的“Do not have bindName handler in current page: pages/card/card. Please make sure that bind ...
- Android 上传文件到XP
Android部分: AsyncHttpClient client = new AsyncHttpClient(); RequestParams requestParams = new Request ...
- win7搭建node+npm+bower的环境
原文的地址:https://my.oschina.net/JeeChou/blog/219699 Windows下的NodeJS安装是比较方便的(v0.6.0版本之后,支持windows native ...
- 程序员不常用Linux命令集
1) 关闭指定网卡,如关闭网卡eth0 ifconfig eth0 down 也可以使用ifdown,通常ifdown是一个指向ifup的软链接,而ifup为一个脚本文件. 2) 命令自启动,如希望机 ...
- Vivado安装教程
Vivado的各个版本的安流程其实都差不多,本教程用Vivado2016.4为例进行安装,同样适用于之前和之后的各个版本. 下载好安装包后打开,双击xsetup.exe运行安装程序 弹出的窗口,提示现 ...
- (轉)CSS 单行溢出文本显示省略号...的方法(兼容IE FF)
轉自:http://www.cnblogs.com/hlz789456123/archive/2009/02/18/1392972.html html代码:<div><p>&l ...
- Android-SQLiteOpenHelper里增删改查
为什么要写一篇,Android-SQLiteOpenHelper里增删改查,的文章呢: 因为之前的方式是:MySQLiteOpenHelper(只负责 生成打开据库/生成打开表/升级表),在其他端:完 ...
- GlusteFS 二
通过heketi提供的RestfullApi来管理 Gluster,进而与kubernetes集成.将gluster作为kubernetes的数据存储 1 安装 安装gluster 参见 Gluste ...
- 提问的智慧——其实你真的不会提问!(转)
在黑客世界里,当提出一个技术问题时,你能得到怎样的回答?这取决于挖出答案的难度,同样取决于你提问的方法.本指南旨在帮助你提高发问技巧,以获取你最想要的答案. 首先你必须明白,黑客们只偏爱艰 ...
- 深入CSS属性(九):z-index
如果你不是一名csser新手,想必你对z-index的用法应该有个大致的了解了吧,z-index可以控制定位元素在垂直于显示屏方向(Z 轴)上的堆叠顺序,本文不去讲述基本的API如何使用,而是去更深入 ...