[源碼下載]
作者:webabcd
介紹
不可或缺 Windows Native 之 C++
示例
CppIO2.h
#pragma once
#include <string>
using namespace std;
namespace NativeDll
{
class CppIO2
{
public:
string Demo(string rootPath);
};
}
CppIO2.cpp
/*
* 文件
*/
#include "pch.h"
#include "CppIO2.h"
#include <fstream> // ifstream(繼承自istream), ofstream(繼承自ostream)
using namespace NativeDll;
void cppio2_demo1();
void cppio2_demo2();
void cppio2_demo3();
void cppio2_demo4();
string _rootPath;
string CppIO2::Demo(string rootPath)
{
_rootPath = rootPath;
// 寫入文本文件
cppio2_demo1();
// 讀取文本文件
cppio2_demo2();
// 寫入二進制文件
cppio2_demo3();
// 讀取二進制文件
cppio2_demo4();
return "演示文件的保存路徑:" + _rootPath;
}
// 寫入文本文件
void cppio2_demo1()
{
string fileName = _rootPath + "\\cpp_file_demo1.txt";
// 打開文件
// ofstream outfile;
// outfile.open(fileName, ios::out);
// 打開文件也可以這樣寫
ofstream outfile(fileName, ios::out); // 第 2 個參數是文件的輸入輸出方式(多個用“|”分隔),其說明參見後面的注釋
// 如果既想寫又想讀就用 fstream
if (!outfile) // 如果文件打開失敗,返回值為 0
{
// err
}
for (int i = 0; i < 3; i++)
{
// 寫入文本數據
outfile << "abc" << "\n";
}
// 格式化寫入的文本數據
outfile << hex << 100 << std::endl; // 關於格式化請參見:CppIO1.cpp
// 關閉文件
outfile.close();
}
// 讀取文本文件
void cppio2_demo2()
{
string fileName = _rootPath + "\\cpp_file_demo1.txt";
char buf[1024];
string result;
ifstream infile(fileName, ios::in); // 第 2 個參數是文件的輸入輸出方式(多個用“|”分隔),其說明參見後面的注釋
// 如果既想寫又想讀就用 fstream
// 文件打開成功,說明這個文件之前是存在的
if (infile.is_open())
{
// 返回下一個字符,但是文件內部位置指針不變
char c = infile.peek(); // 如果返回的字符是文件結束符,則其值為 EOF
// 文件正常,且文件內部位置指針不在結尾
while (infile.good() && !infile.eof()) // eof - end of line
{
memset(buf, 0, 1024); // 清空 buf
infile.getline(buf, 1204); // 讀當前文件內部位置指針所指的一行數據給 buf, 然後文件內部位置指針指向下一行
result += buf;
result += "\n";
}
infile.close();
}
}
// 寫入二進制文件
void cppio2_demo3()
{
struct employee
{
int num;
char name[32];
} w[3] = { { 100, "wanglei" }, { 200, "webabcd" }, { 300, "diandian" } };
string fileName = _rootPath + "\\cpp_file_demo2.b";
ofstream outfile(fileName, ios::binary); // 第 2 個參數是文件的輸入輸出方式(多個用“|”分隔),其說明參見後面的注釋
// 如果既想寫又想讀就用 fstream
if (!outfile)
{
// err
}
for (int i = 0; i < 3; i++)
{
// 寫入數據(要將地址轉換為 char* 類型指針)
outfile.write((char *)&w[i], sizeof(w[i]));
}
outfile.close();
}
// 讀取二進制文件
void cppio2_demo4()
{
struct employee
{
int num;
char name[32];
} r[2];
string fileName = _rootPath + "\\cpp_file_demo2.b";
ifstream infile(fileName, ios::binary); // 第 2 個參數是文件的輸入輸出方式(多個用“|”分隔),其說明參見後面的注釋
// 如果既想寫又想讀就用 fstream
if (!infile)
{
// err
}
// 移動文件內部位置指針。關於文件內部位置指針的操作函數請參見後面的注釋
infile.seekg(sizeof(employee), ios::cur);
for (int i = 0; i < 2; i++)
{
// 讀取數據(要將地址轉換為 char* 類型指針)
infile.read((char *)&r[i], sizeof(r[i]));
}
infile.close();
}
/*
* 文件的輸入輸出方式:
*
* ios::in - 以輸入方式打開文件
* ios::out -以輸出方式打開文件(這是默認方式),如果已有此名字的文件,則將其原有內容全部清除
* ios::app - 以輸出方式打開文件,寫入的數據添加在文件末尾
* ios::ate - 打開一個已有的文件,文件指針指向文件末尾
* ios::trunc - 打開一個文件,如果文件已存在,則刪除其中全部數據,如文件不存在,則建立新文件。如已指定了 ios::out 方式,而未指定 ios::app, ios::ate, ios::in, 則同時默認此方式
* ios::binary - 以二進制方式打開一個文件,如不指定此方式則默認為 ASCII 方式
* ios::in | ios::out - 以輸入和輸出方式打開文件,文件可讀可寫
* ios::out | ios::binary - 以二進制方式打開一個輸出文件
* ios::in | ios::binar - 以二進制方式打開一個輸入文件
*/
/*
* 文件內部位置指針的操作函數:
*
* gcount() - 返回最後一次輸入所讀入的字節數
* tellg() - 返回輸入文件指針的當前位置
* seekg(文件中的位置) - 將輸入文件中指針移到指定的位置
* seekg(位移量, 參照位置) - 以參照位置為基礎移動若干字節
* tellp() - 返回輸出文件指針當前的位置
* seekp(文件中的位置) - 將輸出文件中指針移到指定的位置
* seekp(位移量, 參照位置) - 以參照位置為基礎移動若干字節
*
*
*
* 參照位置的說明:
* ios::beg - 文件開頭(beg 是 begin 的縮寫),這是默認值
* ios::cur - 指針當前的位置(cur 是 current 的縮寫)
* ios::end - 文件末尾
*/
OK
[源碼下載]