(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 開發與測試都還正常,但只要部署到測試機或正式機,就是沒辦法順利執行,卡關許久之後找我協助.我發現錯誤訊息確實很「一般 ...
随机推荐
- 25- 解决'python -m pip install --upgrade pip' 报错问题
转载于:https://blog.csdn.net/cxs123678/article/details/80659273 再安装包的时候提示 You are using pip version 9.0 ...
- 事务传播性、隔离性与MVCC
一.事务传播性 1.1 什么是事务的传播性 事务的传播性一般在事务嵌套时候使用,比如在事务A里面调用了另外一个使用事务的方法,那么这俩个事务是各自作为独立的事务执行提交,还是内层的事务合并到外层的事务 ...
- cakephp获取最后一条sql语句
.在app\config\core.php中设置Configure::write(); .页面上追加如下代码: $dbo = ConnectionManager::getDataSource('def ...
- HTML5 本地存储+layer弹层组件制作记事本
什么是 HTML5 Web 存储? 使用HTML5可以在本地存储用户的浏览数据. 早些时候,本地存储使用的是 cookie.但是Web 存储需要更加的安全与快速. 这些数据不会被保存在服务器上,但是这 ...
- UVa 247 Calling Circles (DFS+Floyd)
题意:如果两个人互通电话,那么他们就在一个电话圈里,现在给定 n 个人,并且给定 m 个通话记录,让你输出所有的电话圈. 析:刚开始没想到是Floyd算法,后来才知道是这个算法,利用这个算法进行连通性 ...
- curl:get,post 以及SoapClien访问webservice数据
一.curl get模式 public function close_order(){ $url="http://192.168.2.50:7777/U8API.asmx?op=Insert ...
- MFC中的一般经验之谈----OnInitialUpdate
在MFC程序设计中,按照传统的设计,如果处理WM_PAINT消息,一般会派生一个OnPaint函数,映射到WM_PAINT消息上进行绘图处理.但是很多程序中并没有出现OnPaint,一个OnDraw函 ...
- [翻译]Writing Custom Report Components 编写自定义报表组件
摘要:简单介绍了如何编写一个FastReport的组件,并且注册到FastReport中使用. Writing Custom Report Components 编写自定义报表组件 FastRep ...
- PHP爬虫(3)PHP DOM开源代码里的大坑和字符编码
一.开源代码的问题 在PHP爬虫(2)中介绍了开源工程Sunra.PhpSimple.HtmlDomParser.在实际工作中发现一个问题,例如http://www.163.com的网页数据怎么也抓取 ...
- yum反查某个命令或so库在哪个包里面
yum whatprovides "*/XXX.so.1"