C++文件IO操作的简单示例
CppIODemo1.cpp
#include <iostream>
#include <fstream>
#include <chrono>
#define INPUT_BUFFER_SIZE 1024 * 1024
int main()
{
using namespace std;
cout << "Type an input file name: ";
string filename;
getline(cin, filename);
ifstream ifs(filename, ifstream::in | ifstream::binary);
if (ifs)
{
cout << "Type an output file name: ";
getline(cin, filename);
ofstream ofs(filename, ofstream::out | ofstream::binary);
if (ofs)
{
cout << "Copying file..." << endl;
char buffer[INPUT_BUFFER_SIZE];
chrono::system_clock::time_point startTime = chrono::system_clock::now();
while (!ifs.eof())
{
streamsize numberOfCharacters = ifs.read(buffer, INPUT_BUFFER_SIZE).gcount();
ofs.write(buffer, numberOfCharacters);
}
chrono::system_clock::time_point endTime = chrono::system_clock::now();
float elapsedTime = static_cast<chrono::duration<float, ratio<, >>>(endTime - startTime).count();
cout << "File copied, elapsed time: " << elapsedTime << endl;
}
else
cerr << "Cannot open output file: '" << filename << "'!" << endl;
}
else
cerr << "Cannot open input file: '" << filename << "'!" << endl;
return ;
}
CppIODemo2.cpp
#include <iostream>
#include <fstream>
#define INPUT_BUFFER_SIZE 1024 * 1024
int main()
{
std::cout << "Enter an input file name: ";
std::string filename;
std::getline(std::cin, filename);
std::ifstream ifs(filename, std::fstream::in);
if (ifs.is_open())
{
std::cout << "Enter an output file name: ";
std::getline(std::cin, filename);
std::ofstream ofs(filename, std::fstream::out);
if (ofs.is_open())
{
char buffer[INPUT_BUFFER_SIZE];
while (!ifs.eof())
{
std::streamsize numberOfCharacters = ifs.read(buffer, INPUT_BUFFER_SIZE).gcount();
std::cout.write(buffer, numberOfCharacters);
ofs.write(buffer, numberOfCharacters);
}
}
else
std::cout << "Cannot open file: " << filename << std::endl;
}
else
std::cout << "Cannot open file: " << filename << std::endl;
return ;
}
附带一句:http://en.cppreference.com,此站点为C/C++权威参考手册^_^若不习惯英文,可浏览此站点的中文版http://zh.cppreference.com
C++文件IO操作的简单示例的更多相关文章
- 文件IO操作
前言 本文介绍使用java进行简单的文件IO操作. 操作步骤 - 读文件 1. 定义一个Scanner对象 2. 调用该对象的input函数族进行文件读取 (参见下面代码) 3. 关闭输入流 说明:其 ...
- Linux文件IO操作
来源:微信公众号「编程学习基地」 目录 文件操作 Linux文件类型 Linux文件权限 修改文件权限 Linux error 获取系统调用时的错误描述 打印错误信息 系统IO函数 open/clos ...
- 树莓派学习笔记——使用文件IO操作GPIO SysFs方式
0 前言 本文描写叙述假设通过文件IO sysfs方式控制树莓派 GPIO端口.通过sysfs方式控制GPIO,先訪问/sys/class/gpio文件夹,向export文件写入GPIO编号, ...
- 9.2 Go 文件IO操作
9.2 Go 文件IO操作 1.1.1. bufio包 带有缓冲区的IO读写操作,用于读写文件,以及读取键盘输入 func main() { //NewReader返回一个结构体对象指针 reader ...
- Linux学习记录--文件IO操作相关系统编程
文件IO操作相关系统编程 这里主要说两套IO操作接口,各自是: POSIX标准 read|write接口.函数定义在#include<unistd.h> ISO C标准 fread|fwr ...
- QML从文件加载组件简单示例
QML从文件加载组件简单示例 文件目录列表: Project1.pro QT += quick CONFIG += c++ CONFIG += declarative_debug CONFIG += ...
- linux文件IO操作篇 (一) 非缓冲文件
文件IO操作分为 2 种 非缓冲文件IO 和 缓冲文件IO 它们的接口区别是 非缓冲 open() close() read() write() 缓冲 fopen() fclose() fread() ...
- imx6用文件io操作gpio
具体请参考: http://blog.csdn.net/u014213012/article/details/53140781 这里要注意的是: 要让linux支持文件io方式操作gpio,首先驱动必 ...
- Java文件IO操作应该抛弃File拥抱Paths和Files
Java7中文件IO发生了很大的变化,专门引入了很多新的类: import java.nio.file.DirectoryStream;import java.nio.file.FileSystem; ...
随机推荐
- NBOJv2——Problem 1002: 蛤玮的财宝(多线程DP)
Problem 1002: 蛤玮的财宝 Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit interger IO format: %ll ...
- Codeforces 333E Summer Earnings ——Bitset
[题目分析] 找一个边长最大的三元环. 把边排序,然后依次加入.加入(i,j)时,把i和j取一个交集,看看是否存在,存在就找到了最大的三元环. 输出即可,n^3/64水过. [代码] #include ...
- Query on The Trees(hdu 4010)
题意: 给出一颗树,有4种操作: 1.如果x和y不在同一棵树上则在xy连边 2.如果x和y在同一棵树上并且x!=y则把x换为树根并把y和y的父亲分离 3.如果x和y在同一棵树上则x到y的路径上所有的点 ...
- gdb 远程调试android进程
原文:http://blog.csdn.net/xinfuqizao/article/details/7955346?utm_source=tuicool 什么是gdb 它是gnu组织开发的一个强大的 ...
- curl抓取数据
抓取数据的代码: $url='抓取数据的网站路径'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); //参数为1表示传输数据,为0表示 ...
- python--错误了就需要调试(异常处理)
python提供了两个非常重要的功能来处理python程序在运行中出现的异常和错误.你可以使用该功能来调试python程序. 我们可打开idle-->F1进行查看文档,里面很多异常类型,如图: ...
- CentOS 7.5 初始网络配置
最近刚装完 CentOS 7.5 系统,由于网络不通,导致无法用 yum 命令下载软件,经过了各种折腾,终于搞定了,这里讲解一下 如何设置初始网络. 本案例环境 VmWare 11.0 , 操作系统 ...
- git(三):第一次github了解使用
第一次使用github,看了一下使用说明,创建第一个repository,以后还要多学习. 一.Github创建一个新仓库 ······Creat a new repository 创建一个新仓库,点 ...
- Codeforces Gym 100286I iSharp 模拟
原题地址:http://codeforces.com/gym/100286/attachments/download/2013/20082009-acmicpc-northeastern-europe ...
- Codeforces Gym 100418K Cards 组合数学
CardsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action? ...