打开后缀参数

 #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++文件操作小结的更多相关文章

  1. python中的文件操作小结2

    ''' #-----------文件修改---------- f=open("test_1",'r',encoding="utf-8") f2=open(&qu ...

  2. python中的文件操作小结1

    #!/usr/local/bin/python3 # -*- coding:utf-8 -*- f=open("test_1",'r',encoding="utf-8&q ...

  3. python文件操作以及循环小结

    Python中的文件使用建议使用 with open(filename, "r") as f: 的形式进行文件操作,如果忘记关闭文件指针的话,他会帮你自己关闭文件, 如果使用原来的 ...

  4. python学习第十八天 --文件操作

    这一章节主要讲解文件操作及其文件读取,缓存,文件指针. 文件操作 (1)文件打开:open(filepath,filemode) filepath:要打开文件的路径 filemode:文件打开的方式 ...

  5. 初学Python——文件操作第二篇

    前言:为什么需要第二篇文件操作?因为第一篇的知识根本不足以支撑基本的需求.下面来一一分析. 一.Python文件操作的特点 首先来类比一下,作为高级编程语言的始祖,C语言如何对文件进行操作? 字符(串 ...

  6. VC++文件操作之最全篇

    一.剖析VC中的文件操作 各种关于文件的操作在程序设计中是十分常见,如果能对其各种操作都了如指掌,就可以根据实际情况找到最佳的解决方案,从而在较短的时间内编写出高效的代码,因而熟练的掌握文件操作是十分 ...

  7. Python 实现隐藏文件夹、文件操作

    Python通过win32api 可以实现操作文件夹文件操作,获取属性,修改属性 1.获取属性 通过win32api.GetFileAttributes 方法可以获取属性值 import win32c ...

  8. 第32课 Qt中的文件操作

    1. Qt的中IO操作 (1)Qt中IO操作的处理方式 ①Qt通过统一的接口简化了文件和外部设备的操作方式 ②Qt中的文件被看作一种特殊的外部设备 ③Qt中的文件操作与外部设备的操作相同 (2)IO操 ...

  9. Delphi文件操作函数

    文件是同一种类型元素的有序集合,是内存与外设之间传输数据的渠道.文件的本质是一个数据流,所有的文件实际上是一串二进制序列.文件管理包括:1.文件操作.2.目录操作.3.驱动器操作.三部分. 1.常见文 ...

随机推荐

  1. tf.slice()解释

    转载:https://www.jianshu.com/p/71e6ef6c121b def slice(input_, begin, size, name=None): 其中“input_”是你输入的 ...

  2. 洛谷P5087 数学

    DP. 设f[i][j]为前j个数中选i个数的所有组合的分数之和 决策: 不选这个数,得分为f[i][j - 1] 选这个数,得分为f[i - 1][j - 1] * a[j] 可以得到状态转移方程为 ...

  3. 【转】C#添加修改删除文件文件夹大全

    [转]C#添加修改删除文件文件夹大全 C#添加修改删除文件文件夹大全 StreamWriter sw = File.AppendText(Server.MapPath(".")+& ...

  4. Qt之QFileIconProvider

    简述 QFileIconProvider类为QDirModel和QFileSystemModel类提供了文件图标. 简述 共有类型 公共函数 示例 IconType 效果 源码 QFileInfo 效 ...

  5. Irrlicht 3D Engine 笔记系列 之 教程6- 2D Graphics

    作者:i_dovelemon 日期:2015 / 7 / 1 来源: CSDN 主题:2D Graphics, Irrlicht 教程翻译 本篇教程将要向大家展示怎样使用Irrlicht引擎绘制2D图 ...

  6. 浅析为什么 char 类型的范围是 : -128~+127

    在 C 语言中. signed char 类型的范围为 -128~127,每本教科书上也这么写.可是没有哪一本书上(包含老师)也不会给你为什么是 -128~127,这个问题貌似看起来也非常easyea ...

  7. 使用具体解释及源代码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter

    Adapter相当于一个数据源,能够给AdapterView提供数据.并依据数据创建相应的UI.能够通过调用AdapterView的setAdapter方法使得AdapterView将Adapter作 ...

  8. UVA 1415 - Gauss Prime(数论,高斯素数拓展)

    UVA 1415 - Gauss Prime 题目链接 题意:给定a + bi,推断是否是高斯素数,i = sqrt(-2). 思路:普通的高斯素数i = sqrt(-1),推断方法为: 1.假设a或 ...

  9. ModelForm views.py

    from django.shortcuts import render from django import forms from django.forms import fields from ap ...

  10. Eclipse输入智能提示设置

    JAVA智能提示 展开菜单 Window -> preferences -> Java -> Editor -> Content assist 找到右边的Auto-Activa ...