在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 讀寫檔案操作的更多相关文章

  1. C++ 檔案、資料夾、路徑處理函式庫:boost::filesystem

    原帖:https://tokyo.zxproxy.com/browse.php?u=uG7kXsFlW1ZmaxKEvCzu8HrCJ0bXIAddA1s5dtIUZ%2FYzM1u9JI7jjKLT ...

  2. [ASP.NET] 檔案讀寫權限問題

    今天遇到一個問題,環境如下: IIS Server: Server 2008 R2 沒加域 File Server: Server 2003 加域 當我的Web程序需要把位於File Server的一 ...

  3. 在 Server 端存取 Excel 檔案的利器:NPOI Library

    转处 http://msdn.microsoft.com/zh-tw/ee818993.aspx Codeplex 軟體套件(Package)資訊 套件名稱 NPOI 作者 tonyqus, huse ...

  4. [ASP.NET] 如何利用Javascript分割檔案上傳至後端合併

    最近研究了一下如何利用javascript進行檔案分割上傳並且透過後端.特地記錄一下相關的用法 先寫限制跟本篇的一些陷阱 1.就是瀏覽器的支援了 因為本篇有用到blob跟webworker 在ie中需 ...

  5. C# 選擇本機檔案並上傳

    參考自:http://www.dotblogs.com.tw/puma/archive/2008/11/07/5910.aspxhttp://www.codeproject.com/Articles/ ...

  6. 使用Device IO Control 讀寫 USB Mass Storage

    http://www.ezblog.idv.tw/Download/USBStorage.rar 這是一個不透過檔案系統,去讀寫USB Mass Storage 任何位置(包含FAT)的方式 首先需安 ...

  7. mysql proxy讀寫分流(二)-加入RW splitting

    上一篇中提到 安裝LUA及MySQL Proxy後,接下來就是RW splitting(讀寫分流)的部份了 整體的概念圖跟上一篇MySQL Proxy安裝方式相同,丫忠再補上一個對應port的圖表: ...

  8. 在 React Native 中使用 moment.js 無法載入語系檔案

    moment.js 是很常見的日期時間 library,友善的 API 與極佳的執行效率是它的兩大賣點.例如 (new Date()).getFullYear(),如果使用 moment.js 我可以 ...

  9. 何解決 LinqToExcel 發生「無法載入檔案或組件」問題何解決 LinqToExcel 發生「無法載入檔案或組件」問題

    在自己的主機上透過 Visual Studio 2013 與 IISExpress 開發與測試都還正常,但只要部署到測試機或正式機,就是沒辦法順利執行,卡關許久之後找我協助.我發現錯誤訊息確實很「一般 ...

随机推荐

  1. hack vba password, en useful...

    Unbelivibale, but I found a very simple way that really works! Do the follwoing: 1. Create a new sim ...

  2. windows cmake安装

    https://blog.csdn.net/u013832707/article/details/53127710

  3. JAVA array,map 转 json 字符串

    public class User { private String username; private String password; public String getUsername() { ...

  4. java grpc简单例子

    原文地址:http://blog.csdn.net/jek123456/article/details/53465033 用eclipse新建一个maven项目,Id信息如下 <groupId& ...

  5. 《Forward团队-爬虫豆瓣top250项目-开发文档》

    码云地址:https://github.com/xyhcq/top250 模块功能:获取豆瓣top250网页的源代码,并分析. def getHTMLText(url,k): # 获取网页源代码 tr ...

  6. 桥接模式及C++实现

    桥接模式 先说说桥接模式的定义:将抽象化(Abstraction)与实现化(Implementation)分离,使得二者可以独立地变化. 桥接模式号称设计模式中最难理解的模式之一,关键就是这个抽象和实 ...

  7. 2016-2017-2 20155223 实验二 《Java面向对象程序设计》

    2016-2017-2 苏黄永郦 实验二 实验报告 前期准备工作--程序安装 -问题一 开始的时候我就在老师博客的指导下安装IDEA插件JUnit Generator V2.0.当然我的IDEA肯定没 ...

  8. HDU2544 最短路 2017-04-12 18:51 31人阅读 评论(0) 收藏

    最短路 Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissio ...

  9. 咏南新CS插件开发框架支持DELPHI7

    咏南新CS插件开发框架支持DELPHI7 适用软件:基于数据管理的应用软件适用行业:生产制造.物流.贸易.零售行业的ERP.MRP.CRM.MIS.MES.POS等基于C/S架构的数据库管理系统 运行 ...

  10. C#泛型使用小记

    最近C#的泛型使用频次略多,特在此记下一个印象深刻的. 情景如下, 基类BaseClass 有一系列的子类 SubClass1, SubClass2, SubClass3... 且其构造函数的参数较多 ...