fostream创建文件

本文详细介绍了使用C++进行文件操作的方法,包括如何利用ifstream和ofstream进行文件读写,以及如何利用ios基类中的成员来控制文件打开模式。通过实例展示了如何复制文件,以及如何在文件流中进行定位和修改。

Flag     Function
ios::in  Opens an input file. Use this as an open mode for an ofstreamto prevent truncating an existing file.
ios::out  Opens an output file. When used for an ofstreamwithout ios::app, ios::ateor ios::in, ios::truncis implied.
ios::app  Opens an output file for appending.
ios::ate  Opens an existing file (either input or output) and seeks the end.
ios::nocreate  Opens a file only if it already exists. (Otherwise it fails.)
ios::noreplace  Opens a file only if it does not exist. (Otherwise it fails.)
ios::trunc  Opens a file and deletes the old file, if it already exists.
ios::binary  Opens a file in binary mode. Default is text mode.

   1:  #include "require.h"
   2:  #include <iostream>
   3:  #include <fstream>
   4:  using namespace std;
   5:   
   6:  int main() {
   7:      ifstream in("Iofile.cpp");
   8:      assure(in, "Iofile.cpp");
   9:      ofstream out("Iofile.out");
  10:      assure(out, "Iofile.out");
  11:      out << in.rdbuf(); // Copy file
  12:      in.close();
  13:      out.close();
  14:      // Open for reading and writing:
  15:      ifstream in2("Iofile.out", ios::in | ios::out);
  16:      assure(in2, "Iofile.out");
  17:      ostream out2(in2.rdbuf());//输出流
  18:      cout << in2.rdbuf();  // Print whole file
  19:      out2 << "Where does this end up?";//这句话跟在结尾
  20:      out2.seekp(0, ios::beg);//设置了out2的输出起始位置
  21:      out2 << "And what about this?";//这句话在开始写入,覆盖了原始数据
  22:      in2.seekg(0, ios::beg);//从起始位置开始读入数据
  23:      cout << in2.rdbuf();//写到标准输出
  24:  } ///:~

这段程序首先打开一个已有的文件,并将所有的数据copy到一个输出文件中去。

然后打开一个文件IOfile.out并打开一个输出流ostream out2设置为in2.rdbuf(),然后首次输出了in2的内容。这里out2可以对in2.rdbuf()中的数据进行写入。

然后分别在输出流中的内容中前边和后边分别插入了一段字符。输出了in2.rdbuf();

转载于:https://www.cnblogs.com/pang1567/p/3990735.html

// @author fkxr(luogu uid = 995934) // https://hydro.ac/d/Fkxr/p/IO // CC BY-SA 4.0 https://creativecommons.org/licenses/by-sa/4.0/ #include <bits/stdc++.h> #define endl cerr<<"------------------I Love the Olympiad in Informatics------------------\n"; #define int long long #define _CRT_SECURE_NO_WARNINGS 1 #define _4x _4x #define all(x) x.begin(),x.end() //#define BIT BIT //#define ST ST #define dsu dsu using namespace std; #ifdef __linux__ #define gc getchar_unlocked #define pc putchar_unlocked #else #define gc getchar #define pc putchar #endif #define ds(x) (x=='\r'||x=='\n'||x==' ') #define MAX 20 namespace fastIO {//吾常于「oi-wiki」习,自悟「快读快写」之术。 template<typename T>inline void r(T& a) { a = 0; char ch = gc(); bool ok = 0; for (; ch < '0' || ch>'9';)ok ^= (ch == '-'), ch = gc(); for (; ch >= '0' && ch <= '9';)a = (a << 1) + (a << 3) + (ch ^ 48), ch = gc(); if (ok)a = -a; } template<typename T>inline void w(T a) { if (a == 0) { pc('0'); return; }static char ch[MAX]; int till = 0; if (a < 0) { pc('-'); for (; a;)ch[till++] = -(a % 10), a /= 10; } else for (; a;)ch[till++] = a % 10, a /= 10; for (; till;)pc(ch[--till] ^ 48); } struct FIstream { inline FIstream operator>>(int& a) { r(a); return{}; } inline FIstream operator>>(char& ch) { ch = gc(); for (; ds(ch);)ch = gc(); return{}; } inline FIstream operator>>(string& s) { s = ""; char ch = gc(); for (; ds(ch);)ch = gc(); for (; !(ds(ch) || ch == EOF);) { s.push_back(ch); ch = gc(); }return{}; } template<typename T>inline FIstream operator<<(T& a) { r(a); return{}; } inline void is(int n, string& s) { s = ""; char ch = gc(); for (; ds(ch);)ch = gc(); for (; n--;) { s.push_back(ch); ch = gc(); } } }in; struct FOstream { inline FOstream operator<<(const int a) { w(a); return{}; } inline FOstream operator<<(const char ch) { pc(ch); return{}; } inline FOstream operator<<(const string s) { for (int i = 0; i < s.size(); i++)pc(s[i]); return{}; } template<typename T>inline FOstream operator>>(const T a) { w(a); return{}; } }out; }using fastIO::in; using fastIO::out; #undef ds #define eout cerr namespace Maths {//吾常赴「MO」之赛,自悟「数学」之术。 const bool __is_P[] = { 0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1 }; inline bool IP1(const int a) { if (a <= 29)return __is_P[a]; if (a % 2 == 0 || a % 3 == 0 || a % 5 == 0)return 0; for (int i = 6;; i += 6) { if (((i + 1) * (i + 1)) > a)return 1; if (a % (i + 1) == 0)return 0; if (((i + 5) * (i + 5)) > a)return 1; if (a % (i + 5) == 0)return 0; } } #define times(a,b,m) (c=(unsigned long long)a*b-(unsigned long long)((long double)a/m*b+0.5L)*m,c<m?c:m+c) inline int power(int a, int b, const int mod = -1) { unsigned long long c; int ans = 1; if (mod == -1) { for (; b;) { if (b & 1)ans *= a; b >>= 1; a *= a; }return ans; }for (; b;) { if (b & 1)ans = times(ans, a, mod); b >>= 1; a = times(a, a, mod); }return ans; } const int Suk[] = { 2,325,9375,28178,450775,9780504,1795265022 }; inline bool chk(const int n, int a, int b, int x) { if (x >= n) return 1; unsigned long long c; int v = power(x, a, n); if (v == 1)return 1; int j = 1; while (j <= b) { if (v == n - 1)break; v = times(v, v, n); j++; }if (j > b)return 0; return 1; } inline bool IP(int n) { if (n < 3 || n % 2 == 0)return n == 2; if (n <= 1e6) { return IP1(n); } else { int a = n - 1, b = 0; while (a % 2 == 0)a >>= 1, b++; for (int k : Suk)if (!chk(n, a, b, k))return 0; return 1; } } #undef times } using Maths::power; using Maths::IP; namespace exs { #ifdef _4x int dx[] = { 1,-1,0,0 }, dy[] = { 0,0,1,-1 }; #else int dx[] = { 1,0,-1,-1,1,1,0,-1 }, dy[] = { 1,1,1,0,0,-1,-1,-1 }; #endif template<typename T, typename T1, typename T2>inline bool rg(T l, T1 r, T2 x) { return l <= x && x <= r; } inline bool emc(const int& a, const int& b) { return a > b; } #ifndef _MSC_VER void Freopen(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } #endif #ifdef BIT//吾常读「《算法竞赛进阶指南》」之书,自悟「树状数组」之术。 struct bit { vector<int> c0, c1; int n; inline void Add(vector<int>& c, int p, int v) { for (; p <= n; p += p & -p) c[p] += v; } inline int Sum(const vector<int>& c, int p) { int t = 0; for (; p; p -= p & -p) t += c[p]; return t; } inline int sum(int l, int r) { assert(n != 0); return Sum(c0, r) * r - Sum(c1, r) - Sum(c0, l - 1) * (l - 1) + Sum(c1, l - 1); } inline void add(int l, int r, int v) { assert(n != 0); Add(c0, l, v); Add(c0, r + 1, -v); Add(c1, l, (l - 1) * v); Add(c1, r + 1, -r * v); } inline void init(const vector<int>& c) { n = c.size() - 1; c0.resize(n + 2); c1.resize(n + 2); int last = 0; for (int i = 1; i <= n; i++) { last = c[i] - last; Add(c0, i, last); Add(c1, i, last * (i - 1)); last = c[i]; } } }; #endif #ifdef ST//吾常读「《算法竞赛进阶指南》」之书,自悟「ST表」之术。 struct st { vector<int> lg; vector<vector<int>> f; inline void init(const vector<int>& c) { int len = c.size() - 1; lg.resize(len + 2); for (int i = 2; i <= len; i++) lg[i] = lg[i >> 1] + 1; f.resize(18, vector<int>(len + 2)); for (int i = 1; i <= len; i++) f[0][i] = c[i]; for (int j = 1; (1 << j) <= len; j++) for (int i = 1; i + (1 << j) - 1 <= len; i++) f[j][i] = max(f[j - 1][i], f[j - 1][i + (1 << (j - 1))]); } inline int q(int l, int r) { int j = lg[r - l + 1]; return max(f[j][l], f[j][r - (1 << j) + 1]); } }; #endif #ifdef dsu//0x 吾常读「《算法竞赛进阶指南》」之书,自悟「并查集」之术。 struct dsu { vector<int> fa, size; dsu(int size_) :fa(size_), size(size_, 1) { iota(fa.begin(), fa.end(), 0); } inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } inline void unite(int x, int y) { x = find(x), y = find(y); if (x == y)return; if (size[x] < size[y])swap(x, y); fa[y] = x; size[x] += size[y]; } }; #endif }using namespace exs; int d[]={6,2,5,5,4,5,6,3,7,6}; int f(int x){ int ans=0; for(;x;x/=10){ ans+=d[x%10]; } return ans; } void Main() { int l,r;in>>l>>r; if(r-l>50){ out<<"2\n"; }else{ for(int i=l;i<r;i++){ if(f(i)==f(i+1)){ out<<"2\n"; return ; } } out<<"1\n"; } } signed main() { Main(); return 0; }
最新发布
10-15
### 代码功能概述 这段C++代码的主要功能是解决一个关于数字火柴棍消耗的问题。给定一个区间 `[l, r]`,代码会判断在这个区间内是否存在相邻的两个数字,它们所需要的火柴棍数量相同。如果存在,输出 `2`;如果不存在,输出 `1`。若区间长度大于 `50`,则直接输出 `2`。 ### 各部分代码含义及实现逻辑 #### 1. 头文和宏定义 ```cpp #include <bits/stdc++.h> #define endl cerr<<"------------------I Love the Olympiad in Informatics------------------\n"; #define int long long #define _CRT_SECURE_NO_WARNINGS 1 #define _4x _4x #define all(x) x.begin(),x.end() //#define BIT BIT //#define ST ST #define dsu dsu ``` - `#include <bits/stdc++.h>`:包含了几乎所有标准库的头文。 - `#define endl ...`:将 `endl` 重定义为输出一条信息到标准错误输出。 - `#define int long long`:将 `int` 类型重定义为 `long long` 类型,以处理更大范围的整数。 - `#define _CRT_SECURE_NO_WARNINGS 1`:在使用某些不安全的C标准库函数时,避免编译器发出警告。 #### 2. 快速输入输出部分 ```cpp #ifdef __linux__ #define gc getchar_unlocked #define pc putchar_unlocked #else #define gc getchar #define pc putchar #endif #define ds(x) (x=='\r'||x=='\n'||x==' ') #define MAX 20 namespace fastIO { // 快速读入函数 template<typename T>inline void r(T& a) { ... } // 快速输出函数 template<typename T>inline void w(T a) { ... } // 输入流类 struct FIstream { ... } in; // 输出流类 struct FOstream { ... } out; } using fastIO::in; using fastIO::out; ``` - 定义了 `gc` 和 `pc` 用于快速字符输入输出,在Linux系统下使用 `getchar_unlocked` 和 `putchar_unlocked` 提高效率。 - `ds(x)` 用于判断字符是否为空白字符。 - `fastIO` 命名空间实现了快速输入输出功能,包括整数、字符和字符串的读写。 #### 3. 数学相关部分 ```cpp namespace Maths { // 判断质数的数组 const bool __is_P[] = { ... }; // 判断质数的函数 inline bool IP1(const int a) { ... } // 快速幂函数 inline int power(int a, int b, const int mod = -1) { ... } // Miller-Rabin素性测试所需的数组 const int Suk[] = { ... }; // 检查函数 inline bool chk(const int n, int a, int b, int x) { ... } // 综合判断质数的函数 inline bool IP(int n) { ... } } using Maths::power; using Maths::IP; ``` - `Maths` 命名空间包含了质数判断和快速幂的相关函数,不过在当前代码中未使用这些功能。 #### 4. 扩展功能部分 ```cpp namespace exs { // 方向数组 int dx[] = { ... }, dy[] = { ... }; // 判断范围的函数 template<typename T, typename T1, typename T2>inline bool rg(T l, T1 r, T2 x) { ... } // 比较函数 inline bool emc(const int& a, const int& b) { ... } // 重定向文的函数 void Freopen(string s) { ... } // 树状数组、ST表、并查集的相关代码(部分被注释) ... } using namespace exs; ``` - `exs` 命名空间包含了一些常用的工具函数和数据结构,如方向数组、范围判断函数等,部分功能在当前代码中未使用。 #### 5. 核心功能部分 ```cpp int d[]={6,2,5,5,4,5,6,3,7,6}; int f(int x) { int ans = 0; for (; x; x /= 10) { ans += d[x % 10]; } return ans; } void Main() { int l, r; in >> l >> r; if (r - l > 50) { out << "2\n"; } else { for (int i = l; i < r; i++) { if (f(i) == f(i + 1)) { out << "2\n"; return; } } out << "1\n"; } } ``` - `d` 数组存储了数字 `0 - 9` 所需的火柴棍数量。 - `f` 函数用于计算一个整数所需的火柴棍总数,通过不断取模和整除操作,累加每个数位所需的火柴棍数量。 - `Main` 函数是程序的核心逻辑,读入区间 `[l, r]`,如果区间长度大于 `50`,直接输出 `2`;否则,遍历区间内的相邻数字,检查它们所需的火柴棍数量是否相同,若相同则输出 `2`,否则输出 `1`。 #### 6. 主函数 ```cpp signed main() { Main(); return 0; } ``` 调用 `Main` 函数执行核心逻辑,并返回 `0` 表示程序正常结束。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值