40.【C++最全文件操作,少一个你打我】(二)

简介: 40.【C++最全文件操作,少一个你打我】

3.2第二种方法:

判断流对象(!of)
代码展示:
#include <iostream>
using namespace std;
#include <string>
#include <fstream>
int main()
{
 string s1="123";
 ofstream of;     
 of.open("e:\\123.txt");  
 if (!of) //利用流对象进行判断
 {
  cout << "打开文件失败" << endl;
 }
 cout << "成功打开文件:" << endl;
 of << "输出到硬盘的数为::"<<s1 << endl;
 of.close();
 return 0;
}
效果展示:

(七)、文件的写入与读出

1.二进制的写入:

write(const char* str,int n)str是 字符指针或字符 数组,
用来存放一个字符串。n是int型数,它用来表示输出显示字符串中字符的个数。
代码展示:
#include <iostream>
using namespace std;
#include <string>
#include <fstream>
int main()
{
 ofstream of;
 of.open("e:\\123.txt");
 of << 1000 << endl;
 of << "hello 硬盘!" << endl;
 of.put('Ys'); //写操作中put()函数,只能向硬盘中输出一个,
 int var = 12345;
 of.write((const char*)&var, sizeof(int)); //char*并不是指的字符串,而是指的地址,在这边要强行转换(地址,长度)
 of.close();
 return 0;
}
效果展示:

2.二进制的读出:

memset(清除的数组,清楚为多少,数组的原来缓存区)函数,
         read(char* dst, streamsize count);
代码展示:
#include <iostream>
using namespace std;
#include <string>
#include <fstream>
int main()
{
 ofstream of;
 of.open("e:\\123.txt");
 of << 2000<< endl;
 of << "helloworld" << endl;
 of.put('Y'); //写操作中put()函数,只能向硬盘中输出一个,
 int var = 12345;
 of.write((const char*)&var, sizeof(int)); //char*并不是指的字符串,而是指的地址,在这边要强行转换(地址,长度)
 of.close();
 ifstream If;
 If.open("e://123.txt");
 //int va = 12345;
 //If >> va; // >>读出整形
 //cout << "硬盘到内存的数据为:" << va<<endl;
 //char s[100] = {0};
 //If >> s;
 //cout << s << endl; // 读出字符串
 //char ch;                                       
 //ch=If.get(); //这个读出来的是换行,没显示
 //ch = If.get();
 //cout << "读出的put字符是:" << ch << endl;
 char s[100] = { 0 };
 If.getline( s,sizeof(s));
 cout << s << endl;
 memset(s, 0, sizeof(s)); //memset(清除的数组,清楚为多少,数组的原来缓存区)函数,
 If.getline(s, sizeof(s));
 cout << s << endl;
 memset(s, 0, sizeof(s));
 int va = 1235;
 If.read((char*) & va, sizeof(int));
 cout << "通过二进制读出的数据为:" << va<<endl;
 If.close();
 return 0;
}
效果展示:

3.put()函数对字符的输出

代码展示:
#include <iostream>
using namespace std;
#include <string>
#include <fstream>
int main()
{
 ofstream of;     
 of.open("e:\\123.txt");  
 of << 1000 << endl;
 of << "hello 硬盘!" << endl;
 of.put('Ys'); //写操作中put()函数,只能向硬盘中输出一个,
 of.close();
 return 0;
}
效果展示:

4.get()函数对字符的输入

代码展示:
#include <iostream>
using namespace std;
#include <string>
#include <fstream>
int main()
{
 ofstream of;
 of.open("e:\\123.txt");
 of << 2000<< endl;
 of << "helloworld" << endl;
 of.put('Y'); //写操作中put()函数,只能向硬盘中输出一个,
 int var = 12345;
 of.write((const char*)&var, sizeof(int)); //char*并不是指的字符串,而是指的地址,在这边要强行转换(地址,长度)
 of.close();
 ifstream If;
 If.open("e://123.txt");
  int va = 12345;
 If >> va; // >>读出整形
 cout << "硬盘到内存的数据为:" << va<<endl;
 char s[100] = {0};
 If >> s;
 cout << s << endl; // 读出字符串
 char ch;                                       
 ch=If.get(); //这个读出来的是换行,没显示
 ch = If.get();
 cout << "读出的put字符是:" << ch << endl;
 If.close();
 return 0;
}
效果展示:

5.getline()函数的输入

代码展示:
#include <iostream>
using namespace std;
#include <string>
#include <fstream>
int main()
{
 ofstream of;
 of.open("e:\\123.txt");
 of << 2000<< endl;
 of << "helloworld" << endl;
 of.put('Y'); //写操作中put()函数,只能向硬盘中输出一个,
 int var = 12345;
 of.write((const char*)&var, sizeof(int)); //char*并不是指的字符串,而是指的地址,在这边要强行转换(地址,长度)
 of.close();
 ifstream If;
 If.open("e://123.txt");
 char s[100] = { 0 };
 If.getline( s,sizeof(s));
 cout << s << endl;
 memset(s, 0, sizeof(s)); //memset(清除的数组,清楚为多少,数组的原来缓存区)函数,
 If.getline(s, sizeof(s));
 cout << s << endl;
 memset(s, 0, sizeof(s));
 If.close();
 return 0;
}
效果展示:

制作不易!!!!! 谢谢支持!!!!!

相关文章
|
3月前
|
存储 分布式数据库 API
技术好文:VisualC++查看文件被哪个进程占用
技术好文:VisualC++查看文件被哪个进程占用
|
4月前
|
C++ iOS开发
C++ 文件操作的技术性文章
C++ 文件操作的技术性文章
25 0
|
7天前
|
JavaScript 前端开发 测试技术
一个google Test文件C++语言案例
这篇文章我们来介绍一下真正的C++语言如何用GTest来实现单元测试。
9 0
|
1月前
|
存储 算法 C++
【C++】C++ QT实现Huffman编码器与解码器(源码+课程论文+文件)【独一无二】
【C++】C++ QT实现Huffman编码器与解码器(源码+课程论文+文件)【独一无二】
|
1月前
|
存储 数据挖掘 C语言
【C/C++】C/C++车辆交通违章管理系统(源码+数据文件)【独一无二】
【C/C++】C/C++车辆交通违章管理系统(源码+数据文件)【独一无二】
|
30天前
|
监控 编译器 C++
【代码讲解】【C/C++】获取文件最后修改的时间(系统时间)
【代码讲解】【C/C++】获取文件最后修改的时间(系统时间)
34 0
|
1月前
|
安全 C++ Windows
Windows下C++使用gRPC(Qt和VS,含文件包和使用方法)
Windows下C++使用gRPC(Qt和VS,含文件包和使用方法)
|
1月前
|
C++
C++通过文件指针获取文件大小
C++通过文件指针获取文件大小
24 0
|
3月前
|
存储 C++
C++文件操作
C++文件操作