Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
code_shenbing
cpp_14_file
提交
2705599d
C
cpp_14_file
项目概览
code_shenbing
/
cpp_14_file
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
cpp_14_file
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2705599d
编写于
4月 09, 2023
作者:
W
wsb
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
封装文件操作类
上级
0524c1e5
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
32 addition
and
41 deletion
+32
-41
fileReadWrite.cpp
fileReadWrite.cpp
+7
-22
fileReadWrite.h
fileReadWrite.h
+4
-4
main.cpp
main.cpp
+21
-15
test.txt
test.txt
+0
-0
未找到文件。
fileReadWrite.cpp
浏览文件 @
2705599d
...
...
@@ -6,29 +6,14 @@
#include "iostream"
#include "string"
/*
//空构造方法
fileReadWrite::fileReadWrite() {
if (filePath.empty()) cout << "路径为空,请设置文件路径" << endl;
//初始化一些文件对象,便于openFile使用
this->readStream=new ifstream ;
this->writeStream=new ofstream ;
this->ReadWriteStream=new fstream ;
}
fileReadWrite::fileReadWrite(string path) : filePath(path) {
if (path.length() == 0) cout << "文件路径为空!";
}*/
fileReadWrite
::
fileReadWrite
(
char
*
path
,
ofstream
*
w
)
:
fileReadWrite
::
fileReadWrite
(
string
path
,
ofstream
*
w
)
:
filePath
(
path
),
writeStream
(
w
)
{
if
(
path
==
""
)
cout
<<
"文件路径为空!"
;
}
fileReadWrite
::
fileReadWrite
(
char
*
path
,
ifstream
*
r
)
:
fileReadWrite
::
fileReadWrite
(
string
path
,
ifstream
*
r
)
:
filePath
(
path
),
readStream
(
r
)
{
if
(
path
==
""
)
cout
<<
"文件路径为空!"
;
}
...
...
@@ -51,7 +36,7 @@ void fileReadWrite::openFile(fileReadWrite::model model, bool display) {
return
;
}
writeStream
->
open
(
filePath
,
ios
::
app
);
//所有文件读追加到末尾
writeStream
->
open
(
filePath
);
//所有文件读追加到末尾
//从控制台接收数据,敲击回车键结束输入
cout
<<
"请输入要保存到文中的名字,不超过100个字符,按下回车键结束输入!"
<<
endl
;
cin
.
getline
(
c
,
100
);
//接收输入
...
...
@@ -72,7 +57,7 @@ void fileReadWrite::openFile(fileReadWrite::model model, bool display) {
return
;
}
writeStream
->
open
(
filePath
,
ios
::
in
);
//读模式打开
readStream
->
open
(
filePath
);
//读模式打开
//把输入的字符写入到文件中
readStream
->
read
(
c
,
100
);
//最大读取100个字符,并存储在char数组中
cout
<<
"读取的文件完毕!:"
<<
endl
;
...
...
@@ -96,7 +81,7 @@ void fileReadWrite::openFile(fileReadWrite::model model, bool display) {
if
(
flag
==
0
)
{
//写入文件
ReadWriteStream
->
open
(
filePath
,
ios
::
app
);
ReadWriteStream
->
open
(
filePath
,
ios
::
app
);
//从控制台接收数据,敲击回车键结束输入
cout
<<
"请输入要保存到文中的名字,不超过100个字符,按下回车键结束输入!"
<<
endl
;
cin
.
getline
(
c
,
100
);
//接收输入
...
...
@@ -106,7 +91,7 @@ void fileReadWrite::openFile(fileReadWrite::model model, bool display) {
}
else
if
(
flag
==
1
)
{
//开始读取
ReadWriteStream
->
open
(
filePath
,
ios
::
in
);
//读模式打开
ReadWriteStream
->
open
(
filePath
,
ios
::
in
);
//读模式打开
//把输入的字符写入到文件中
ReadWriteStream
->
read
(
c
,
100
);
//最大读取100个字符,并存储在char数组中
cout
<<
"读取的文件完毕,内容为:"
<<
c
<<
endl
;
...
...
@@ -127,6 +112,6 @@ void fileReadWrite::close() {
if
(
ReadWriteStream
!=
nullptr
)
ReadWriteStream
->
close
();
}
fileReadWrite
::
fileReadWrite
(
char
*
path
,
fstream
*
w
)
:
filePath
(
path
),
ReadWriteStream
(
w
)
{
fileReadWrite
::
fileReadWrite
(
string
path
,
fstream
*
w
)
:
filePath
(
path
),
ReadWriteStream
(
w
)
{
}
fileReadWrite.h
浏览文件 @
2705599d
...
...
@@ -13,7 +13,7 @@ using namespace std;
//文件的读取与写入
class
fileReadWrite
{
public:
char
*
filePath
;
//文件路径
string
filePath
;
//文件路径
enum
model
{
read
,
write
,
readWrite
...
...
@@ -28,9 +28,9 @@ public:
/*fileReadWrite();
fileReadWrite(string path);*/
fileReadWrite
(
char
*
path
,
ofstream
*
writeStream
);
fileReadWrite
(
char
*
path
,
ifstream
*
readStream
);
fileReadWrite
(
char
*
path
,
fstream
*
ReadWriteStream
);
fileReadWrite
(
string
path
,
ofstream
*
writeStream
);
fileReadWrite
(
string
path
,
ifstream
*
readStream
);
fileReadWrite
(
string
path
,
fstream
*
ReadWriteStream
);
void
openFile
(
model
model
,
bool
display
);
//打开文件,参数的含义是选择文件的操作模式、文件内容输出目的地
void
close
();
//关闭文件
...
...
main.cpp
浏览文件 @
2705599d
#include <iostream>
#include <vector>
#include "fileReadWrite.h"
using
namespace
std
;
...
...
@@ -7,22 +8,27 @@ using namespace std;
int
main
()
{
//文件路径: 在本项目路径下新建一个文本文件叫test.txt
string
filePath
=
"test.txt"
;
ifstream
infile
;
infile
.
open
(
filePath
,
ios
::
in
);
if
(
!
infile
.
is_open
())
{
cout
<<
"读取文件失败"
<<
endl
;
}
//第二种读取方法
char
buf
[
1024
];
while
(
infile
.
getline
(
buf
,
sizeof
(
buf
)))
{
cout
<<
buf
<<
endl
;
}
cout
<<
buf
<<
endl
;
string
filePath
=
"..
\\
test.txt"
;
//注意文件路径的写法,因为当前的程序编译路径不一致,所有需要用两个点回退到父级目录才能找到
//初始化读写文件对象
ifstream
*
in
=
new
ifstream
;
ofstream
*
on
=
new
ofstream
;
fstream
*
io
=
new
fstream
;
//初始化读文件封装类对象
/*fileReadWrite *frw=new fileReadWrite(filePath,in);
frw->openFile(fileReadWrite::read, true);
frw->close();*/
//初始化写文件封装类对象
/* fileReadWrite *fwr2=new fileReadWrite(filePath,on);
fwr2->openFile(fileReadWrite::write, true);
fwr2->close();*/
//初始化读写文件封装类对象
fileReadWrite
*
fwr3
=
new
fileReadWrite
(
filePath
,
io
);
fwr3
->
openFile
(
fileReadWrite
::
readWrite
,
true
);
fwr3
->
close
();
return
0
;
}
test.txt
浏览文件 @
2705599d
无法预览此类型文件
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录