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;
}
效果展示:

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

相关文章
|
2天前
|
存储 分布式数据库 API
技术好文:VisualC++查看文件被哪个进程占用
技术好文:VisualC++查看文件被哪个进程占用
|
30天前
|
C++ iOS开发
C++ 文件操作的技术性文章
C++ 文件操作的技术性文章
15 0
|
4天前
|
存储 C++
C++文件操作
C++文件操作
6 1
|
4天前
|
C++
C++职工管理系统(类继承、文件、指针操作、中文乱码解决)
C++职工管理系统(类继承、文件、指针操作、中文乱码解决)
7 0
C++职工管理系统(类继承、文件、指针操作、中文乱码解决)
|
6天前
|
C++ iOS开发 开发者
C++一分钟之-文件输入输出(I/O)操作
【6月更文挑战第24天】C++的文件I/O涉及`ifstream`, `ofstream`和`fstream`类,用于读写操作。常见问题包括未检查文件打开状态、忘记关闭文件、写入模式覆盖文件及字符编码不匹配。避免这些问题的方法有:检查`is_open()`、显式关闭文件或使用RAII、选择适当打开模式(如追加`ios::app`)以及处理字符编码。示例代码展示了读文件和追加写入文件的实践。理解这些要点能帮助编写更健壮的代码。
16 2
|
23天前
|
编译器 C语言 C++
C++中.h和.hpp文件有什么区别?
C++中.h和.hpp文件有什么区别?
|
2天前
|
IDE 开发工具 C++
插件:CLion中使用C/C++ Single File Execution插件编译和运行单个文件
插件:CLion中使用C/C++ Single File Execution插件编译和运行单个文件
7 0
|
1月前
|
存储 C语言 数据安全/隐私保护
C++中的文件操作技术详解
C++中的文件操作技术详解
|
1月前
|
C++ 数据格式
【C++】C++中的【文件IO流】使用指南 [手把手代码演示] & [小白秒懂]
【C++】C++中的【文件IO流】使用指南 [手把手代码演示] & [小白秒懂]
【C++】C++中的【文件IO流】使用指南 [手把手代码演示] & [小白秒懂]
|
23天前
|
Linux C++
Linux C/C++目录和文件的更多操作
Linux C/C++目录和文件的更多操作