打开后缀参数

 #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");
}

最新文章

  1. 2.4使用属性在 ASP.NET Web API 2 路由创建一个 REST API
  2. 【转】Mysql中varchar存放中文与英文所占字节异同
  3. iOS 图片拉伸 resizableImageWithCapInsets
  4. 20条IPTables防火墙规则用法!
  5. mac端口占用查找进程并杀死
  6. JavaScript学习总结【12】、JS AJAX应用
  7. newlisp获得bash该命令的退出状态
  8. HDU 2579/BFS/ Dating with girls(2)
  9. java_JDBC(3)
  10. 【Java学习笔记之一】java关键字及作用
  11. js 小数取整数
  12. macbook air 获取root权限
  13. 面向对象设计原则 里氏替换原则(Liskov Substitution Principle)
  14. PostgreSQL LIKE 查询效率提升实验&lt;转&gt;
  15. Swift 使用 LLDB 调试命令
  16. Google.ProtocolBuffers.dll 之.Net应用(一)
  17. Virtualbox中win7虚拟机中U盘不可用问题的解决
  18. Android下拉刷新控件--PullToRefresh的简单使用
  19. JAVA获取txt文件内容
  20. C#控制台输出退格实现变换闪烁的字符效果

热门文章

  1. 洛谷 P1088 火星人 (全排列)
  2. JAVA jsp page指令的属性 errorPage 和isErrorPage
  3. nested exception is java.lang.NoClassDefFoundError: org/codehaus/jettison/json/JSONObject异常的解决办法
  4. C# winform压缩文件夹带进度条
  5. Step by Step Do IOS Swift CoreData Simple Demo
  6. 杭电OJ(HDU)-ACMSteps-Chapter Two-《An Easy Task》《Buildings》《decimal system》《Vowel Counting》
  7. mysql-创建和操作表
  8. ufldl学习笔记与编程作业:Linear Regression(线性回归)
  9. 英语影视台词---六、Saving Private Ryan Quotes
  10. nyoj--218--Dinner(语法)