在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. 命令: go build

    命令: go build 参考: https://studygolang.com/articles/9463 go help build 构建编译由导入路径命名的包,以及它们的依赖关系,但它不会安装结 ...

  2. Spring Boot配置FastJson报错'Content-Type' cannot contain wildcard type '*'

    升级到最新版本的fastjson以后报的错,查了一下资料,发现 fastjson从1.1.41升级到1.2.28之后,请求报错:json java.lang.IllegalArgumentExcept ...

  3. php利用mpdf生成pdf并保存到目录

    下载mpdf7.0两种方法 (1). github地址:https://github.com/mpdf/mpdf (2). composer require mpdf/mpdf <?php er ...

  4. MySql MyBatis 自动生成主键返回null

    <insert id="insert" parameterType="cn.zno.smse.pojo.UserScan" useGeneratedKey ...

  5. 机器学习—SVM

    一.原理部分: 依然是图片~ 二.sklearn实现: import pandas as pd import numpy as np import matplotlib.pyplot as plt i ...

  6. GPU的历史:从固定管线到可编程管线再到通用计算平台

    开始的时候GPU不能编程,也叫固定管线的,就是把数据按照固定的通路走完. 和CPU同样作为计算处理器,顺理成章就出来了可编程的GPU,但是那时候想在GPU上编程可不是容易的事,你只能使用GPU汇编来写 ...

  7. TSQL--逻辑查询处理

    1. 查询处理可分成逻辑处理和物理处理,逻辑处理上各阶段有特定的顺序,但为优化查询,在保证结果集正确的条件下,物理处理顺序并不按照逻辑处理顺序执行,如果在INNER JOIN时,WHERE语句中的过滤 ...

  8. airport 抓包

    链接airport命令: ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources ...

  9. 手动编译安装LAMP之httpd

    安装前准备: 开发环境:Development Libraries 和 Development Tools httpd环境包:apr-1.4.6.tar.bz2 和 apr-util-1.4.1.ta ...

  10. jQuery限制文本框的输入长度

    jQuery限制文本框输入,包含粘贴. //限制文本框的输入长度 $(function () { $(document).on("keypress", ".txt-val ...