128.C++文件操作小结
打开后缀参数
#include <fstream>
#include <iostream>
using namespace std; //文本读写
//文件写入
void main1()
{
//写入到文件 in读o写
//创建一个输出流
ofstream out;
//输出到文件
out.open("C:\\123.txt");
out << "hello file" << endl;
out.close();
system("C:\\123.txt");
cin.get();
} //文件读取
void main2()
{
//创建一个输出流
ifstream in;
in.open("C:\\123.txt");
char str[]{ };
//in >> str;
//从文件读取
in.getline(str, );
in.close();
cout << str << endl;
cin.get();
system("C:\\123.txt");
} //文件打开方式,追加
void main3()
{
//写入到文件 in读o写
//创建一个输出流
ofstream out;
//输出到文件(追加模式)
out.open("C:\\123.txt",ios::app);
out << " hello world" << endl;
out.close();
system("C:\\123.txt");
cin.get();
} //二进制文件读写
struct info
{
char name[];
int id;
double price;
}; //把结构体的信息按二进制方式写入文件
void main4()
{
struct info infs[] = { {"xiaowang1",,},{ "xiaowang2",, },{ "xiaowang3",, } };
//二进制方式读
ofstream fout("bin.bin", ios::binary);
//内存写入到磁盘 (必须要转换成char *)
fout.write((char *)infs, sizeof(infs));
fout.close(); struct info infs2[];
//二进制方式写(必须要转换成char *)
ifstream fin("bin.bin",ios::binary);
fin.read((char*)infs2, sizeof(infs2));
fin.close();
for (auto i : infs2)
{
cout << i.id << i.name << i.price << endl;
}
system("pause");
} //把结构体的信息按文本方式写入到文件
void main5()
{
struct info infs[] = { { "xiaowang1",, },{ "xiaowang2",, },{ "xiaowang3",, } }; //文本文件读写
ofstream fout("1.txt", ios::out|ios::app); for (auto i : infs)
{
fout << i.name << i.price << i.id << endl;
} fout.close(); ifstream fin("1.txt");
for (int i = ; i < ; i++)
{
char str[]{ };
fin.getline(str, );
cout << str << endl;
}
fin.close();
system("pause");
} //文件指针移动到指定位置读seekg
void main6()
{
ofstream fout("hello.txt");
if (!fout)
{
cout << "操作失败";
} fout << "123456789abcdefghijklmnopqrstuvwxyz";
fout.close(); ifstream fin("hello.txt");
if (fin.fail())
{
cout << "操作失败";
}
//从开始往后移动9个位置开始读
fin.seekg(, ios::beg);
char ch;
while (fin.get(ch))
{
cout << ch;
}
fin.close();
system("pause");
} //追加方式文件指针移动到指定位置写(失效)
void main7()
{
char ch;
ifstream fin("hello.txt");
while (fin.get(ch))
{
cout << ch;
}
fin.close(); //以追加的方式的打开
ofstream fout("hello.txt",ios::app);
//从头开始往后移动五个位置(到第五个位置之后)
//如果是追加移动无效
fout.seekp(, ios::beg);
fout << "ceshiceshi";
fout.close();
system("hello.txt");
system("pause");
} //读写方式文件指针移动到指定位置写(seekp)
void main()
{
char ch;
ifstream fin("hello.txt");
while (fin.get(ch))
{
cout << ch;
}
fin.close(); //以读写的方式的打开(会造成重写)
ofstream fout("hello.txt", ios::in | ios::out);
//从头开始往后移动五个位置(到第五个位置之后)
//如果是追加移动无效
fout.seekp(, ios::beg);
fout << "ceshiceshi";
fout.close();
system("hello.txt");
system("pause");
}
128.C++文件操作小结的更多相关文章
- python中的文件操作小结2
''' #-----------文件修改---------- f=open("test_1",'r',encoding="utf-8") f2=open(&qu ...
- python中的文件操作小结1
#!/usr/local/bin/python3 # -*- coding:utf-8 -*- f=open("test_1",'r',encoding="utf-8&q ...
- python文件操作以及循环小结
Python中的文件使用建议使用 with open(filename, "r") as f: 的形式进行文件操作,如果忘记关闭文件指针的话,他会帮你自己关闭文件, 如果使用原来的 ...
- python学习第十八天 --文件操作
这一章节主要讲解文件操作及其文件读取,缓存,文件指针. 文件操作 (1)文件打开:open(filepath,filemode) filepath:要打开文件的路径 filemode:文件打开的方式 ...
- 初学Python——文件操作第二篇
前言:为什么需要第二篇文件操作?因为第一篇的知识根本不足以支撑基本的需求.下面来一一分析. 一.Python文件操作的特点 首先来类比一下,作为高级编程语言的始祖,C语言如何对文件进行操作? 字符(串 ...
- VC++文件操作之最全篇
一.剖析VC中的文件操作 各种关于文件的操作在程序设计中是十分常见,如果能对其各种操作都了如指掌,就可以根据实际情况找到最佳的解决方案,从而在较短的时间内编写出高效的代码,因而熟练的掌握文件操作是十分 ...
- Python 实现隐藏文件夹、文件操作
Python通过win32api 可以实现操作文件夹文件操作,获取属性,修改属性 1.获取属性 通过win32api.GetFileAttributes 方法可以获取属性值 import win32c ...
- 第32课 Qt中的文件操作
1. Qt的中IO操作 (1)Qt中IO操作的处理方式 ①Qt通过统一的接口简化了文件和外部设备的操作方式 ②Qt中的文件被看作一种特殊的外部设备 ③Qt中的文件操作与外部设备的操作相同 (2)IO操 ...
- Delphi文件操作函数
文件是同一种类型元素的有序集合,是内存与外设之间传输数据的渠道.文件的本质是一个数据流,所有的文件实际上是一串二进制序列.文件管理包括:1.文件操作.2.目录操作.3.驱动器操作.三部分. 1.常见文 ...
随机推荐
- 【BZOJ 1406】 [AHOI2007]密码箱
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] \(x^2%n=1\) \(x^2-1 = k*n\) \((x+1)*(x-1) % n == 0\) 设\(n=a*b\) 对于 ...
- 【转载】Failed to load class "org.slf4j.impl.StaticLoggerBinder".问题解决
在进行hibernate配置好后运行测试类的时候出现: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" ...
- enterprise architect (EA) 源码生成UML类图,帮助理解项目工程
用VS看大型工程代码,尤其是很多层类的,很容易头晕,即便是装了visual assist 插件.用VS生成类图吧,只能生成一堆框,只有一些小的类关系有箭头表示.远远不能满足要求.下面介绍建模工具EA来 ...
- js模拟支付宝提交表单
弄过支付宝的程序猿可能都知道,里面有非常多地方都用到了自提交表单的方式,支付宝的接口通过请求API的形式取得server返回的表单字符串,使用out.print("表单字符串")在 ...
- QTP11.5公布,改名UFT
QTP11.5公布,改名UFT,支持多脚本编辑调试.PDF检查点.持续集成系统.手机測试等 http://www.learnqtp.com/hp-uft11-5-qtp-new-features/ T ...
- UVA10491 - Cows and Cars(概率)
UVA10491 - Cows and Cars(概率) 题目链接 题目大意:给你n个门后面藏着牛.m个门后面藏着车,然后再给你k个提示.在你作出选择后告诉你有多少个门后面是有牛的,如今问你作出决定后 ...
- sklearn.preprocessing OneHotEncoder——仅仅是数值型字段才可以,如果是字符类型字段则不能直接搞定
>>> from sklearn.preprocessing import OneHotEncoder >>> enc = OneHotEncoder() > ...
- 制作 Gif 工具
ScreenToGif:非常小,非常强大: 从此可以十分方便地从视频中抠 gif 出来了: 以及制作一些教学类小 gif,插入到网页中: 丰富的编辑功能: 插入文本,插入标题,插入图像等: 下载地址: ...
- 转】关于cgi、FastCGI、php-fpm、php-cgi
首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. web server(比如说nginx)只是内容的分发者.比如,如果请求/index.h ...
- IDEA中FindBugs编码规范工具使用
IDEA中安装FindBugs插件: file--Settings--Plugins 在Plugins搜索FindBugs: 安装完成后在项目中选中文件右键找到findBugs: 检查代码结果: 按照 ...