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.常见文 ...
随机推荐
- luogu P1586 四方定理(背包)
题意 题解 首先吐槽一下体面的第一句话.反正我不知道(可能是因为我太菜了) 可能没有睡醒,没看出来是个背包. 但告诉是个背包了应该就好做了. #include<iostream> #inc ...
- harbor 安装过程
[root@master01 harbor]# ./prepare Clearing the configuration file: ./common/config/adminserver/env ...
- mariadb数据库基础知识及备份
数据库介绍 1.什么是数据库? 简单的说,数据库就是一个存放数据的仓库,这个仓库是按照一定的数据结构(数据结构是指数据的组织形式或数据之间的联系)来组织,存储的,我们可以通过数据库提供的多种方法来管理 ...
- ActiveMQ maven
http://outofmemory.cn/java/mq/apache-activemq-demo
- HDU 3108 Ant Trip
Ant Trip Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 基于Core Text实现的TXT电子书阅读器
本篇文章的项目地址基于Core Text实现的TXT电子书阅读器. 最近花了一点时间学习了iOS的底层文字处理的框架Core Text.在网上也参考很多资料,具体的资料在文章最后列了出来,有兴趣的可参 ...
- asp.net C# 获取网页源代码的几种方式
1 方法 System.Net.WebClient aWebClient = new System.Net.WebClient(); aWebClient.Encoding = System.Text ...
- Mock+Proxy在SDK项目的自己主动化測试实战
项目背景 广告SDK项目是为应用程序APP开发者提供移动广告平台接入的API程序集合,其形态就是一个植入宿主APP的jar包.提供的功能主要有以下几点: - 为APP请求广告内容 - 用户行为打点 - ...
- C++ double转string类型以及MFC控件简单使用方法
这两天项目须要,測试c++库里面内容.生成jar再给Android调用.我没有学过C++,如今開始记录C++简单使用方法.測试时候一般都是使用mfc程序来測试.要输入值.显示结果吗.我用的编译环境vs ...
- iOS RegexKitLite 提取匹配的内容
使用RegexKitLite正则表达式需要以下工作: 1.RegexKitLite官方网址(内含使用教程):http://regexkit.sourceforge.net/RegexK ...