Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
code_shenbing
cpp_14_file
提交
0524c1e5
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,体验更适合开发者的 AI 搜索 >>
提交
0524c1e5
编写于
4月 09, 2023
作者:
W
wsb
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
封装文件操作类
上级
1b4e42c3
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
91 addition
and
64 deletion
+91
-64
fileReadWrite.cpp
fileReadWrite.cpp
+57
-52
fileReadWrite.h
fileReadWrite.h
+10
-11
main.cpp
main.cpp
+23
-1
test.txt
test.txt
+1
-0
未找到文件。
fileReadWrite.cpp
浏览文件 @
0524c1e5
...
...
@@ -3,109 +3,115 @@
//
#include "fileReadWrite.h"
#include "io.h"
#include "iostream"
#include "string"
/*
//空构造方法
fileReadWrite::fileReadWrite() {
this
->
filePath
=
""
;
cout
<<
"路径为空,请设置文件路径"
<<
endl
;
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
(
string
path
,
ofstream
*
writeStream
)
:
filePath
(
path
),
writeStream
(
w
riteStream
)
{
if
(
path
.
length
()
==
0
)
cout
<<
"文件路径为空!"
;
fileReadWrite
::
fileReadWrite
(
char
*
path
,
ofstream
*
w
)
:
filePath
(
path
),
writeStream
(
w
)
{
if
(
path
==
""
)
cout
<<
"文件路径为空!"
;
}
fileReadWrite
::
fileReadWrite
(
string
path
,
ofstream
*
writeStream
,
ifstream
*
readStream
)
:
filePath
(
path
),
writeStream
(
writeStream
),
readStream
(
readStream
)
{
if
(
path
.
length
()
==
0
)
cout
<<
"文件路径为空!"
;
fileReadWrite
::
fileReadWrite
(
char
*
path
,
ifstream
*
r
)
:
filePath
(
path
),
readStream
(
r
)
{
if
(
path
==
""
)
cout
<<
"文件路径为空!"
;
}
void
fileReadWrite
::
openFile
(
fileReadWrite
::
model
model
,
bool
display
)
{
char
c
[
100
];
//保存控制台输入,最大字数不超过100个
char
c
[
100
];
//保存控制台输入,最大字数不超过100个
//路径为空,给出提示结束函数运行
if
(
filePath
.
empty
()
)
{
if
(
filePath
==
""
)
{
cout
<<
"文件路径为空!"
<<
endl
;
return
;
}
if
(
writeStream
==
nullptr
){
cout
<<
"写入对象为空,没有被初始化!"
<<
endl
;
return
;
}
if
(
readStream
==
nullptr
){
cout
<<
"读取对象为空,没有被初始化!"
<<
endl
;
return
;
}
if
(
ReadWriteStream
==
nullptr
){
cout
<<
"读写对象为空,没有被初始化!"
<<
endl
;
return
;
}
switch
(
model
)
{
//如果是写文件
case
write
:
if
(
writeStream
==
nullptr
)
{
cout
<<
"写入对象为空,没有被初始化!"
<<
endl
;
return
;
}
writeStream
->
open
(
filePath
,
ios
::
app
);
//所有文件读追加到末尾
//从控制台接收数据,敲击回车键结束输入
cout
<<
"请输入要保存到文中的名字,不超过100个字符,按下回车键结束输入!"
<<
endl
;
cin
.
getline
(
c
,
100
);
//接收输入
cout
<<
"请输入要保存到文中的名字,不超过100个字符,按下回车键结束输入!"
<<
endl
;
cin
.
getline
(
c
,
100
);
//接收输入
//把输入的字符写入到文件中
writeStream
->
write
(
c
,
100
);
writeStream
->
write
(
c
,
100
);
//判断是显示到屏幕还是写入到文件
if
(
display
)
{
cout
<<
"你输入的数据为:"
<<
endl
;
cout
<<
c
<<
endl
;
cout
<<
"你输入的数据为:"
<<
endl
;
cout
<<
c
<<
endl
;
}
break
;
//如果是读文件
case
read
:
if
(
readStream
==
nullptr
)
{
cout
<<
"读取对象为空,没有被初始化!"
<<
endl
;
return
;
}
writeStream
->
open
(
filePath
,
ios
::
in
);
//读模式打开
//把输入的字符写入到文件中
readStream
->
read
(
c
,
100
);
//最大读取100个字符,并存储在char数组中
cout
<<
"读取的文件完毕!:"
<<
endl
;
readStream
->
read
(
c
,
100
);
//最大读取100个字符,并存储在char数组中
cout
<<
"读取的文件完毕!:"
<<
endl
;
//判断是显示到屏幕还是写入到文件
if
(
display
)
{
cout
<<
"读取的文件内容如下:"
<<
endl
;
cout
<<
c
<<
endl
;
cout
<<
"读取的文件内容如下:"
<<
endl
;
cout
<<
c
<<
endl
;
}
break
;
//如果是读写文件
case
readWrite
:
if
(
ReadWriteStream
==
nullptr
)
{
cout
<<
"读写对象为空,没有被初始化!"
<<
endl
;
return
;
}
int
flag
;
cout
<<
"请输入文件的操作模式,输入0代表写,输入1代表读"
<<
endl
;
cin
>>
flag
;
cout
<<
"请输入文件的操作模式,输入0代表写,输入1代表读"
<<
endl
;
cin
>>
flag
;
if
(
flag
==
0
)
{
if
(
flag
==
0
)
{
//写入文件
ReadWriteStream
->
open
(
filePath
,
ios
::
app
);
ReadWriteStream
->
open
(
filePath
,
ios
::
app
);
//从控制台接收数据,敲击回车键结束输入
cout
<<
"请输入要保存到文中的名字,不超过100个字符,按下回车键结束输入!"
<<
endl
;
cin
.
getline
(
c
,
100
);
//接收输入
cout
<<
"请输入要保存到文中的名字,不超过100个字符,按下回车键结束输入!"
<<
endl
;
cin
.
getline
(
c
,
100
);
//接收输入
//把输入的字符写入到文件中
ReadWriteStream
->
write
(
c
,
100
);
cout
<<
"写入文件完成,你写入的内容是:"
<<
c
<<
endl
;
ReadWriteStream
->
write
(
c
,
100
);
cout
<<
"写入文件完成,你写入的内容是:"
<<
c
<<
endl
;
}
else
if
(
flag
==
1
){
}
else
if
(
flag
==
1
)
{
//开始读取
ReadWriteStream
->
open
(
filePath
,
ios
::
in
);
//读模式打开
//把输入的字符写入到文件中
ReadWriteStream
->
read
(
c
,
100
);
//最大读取100个字符,并存储在char数组中
cout
<<
"读取的文件完毕,内容为:"
<<
c
<<
endl
;
}
else
{
cout
<<
"你输入有误,程序结束!"
<<
endl
;
ReadWriteStream
->
read
(
c
,
100
);
//最大读取100个字符,并存储在char数组中
cout
<<
"读取的文件完毕,内容为:"
<<
c
<<
endl
;
}
else
{
cout
<<
"你输入有误,程序结束!"
<<
endl
;
}
break
;
...
...
@@ -118,10 +124,9 @@ void fileReadWrite::close() {
//只要读写对象不为空,则调用close方法释放对象
if
(
writeStream
!=
nullptr
)
writeStream
->
close
();
if
(
readStream
!=
nullptr
)
readStream
->
close
();
if
(
ReadWriteStream
!=
nullptr
)
ReadWriteStream
->
close
();
}
fileReadWrite
::
fileReadWrite
(
string
path
,
ofstream
*
writeStream
,
ifstream
*
readStream
,
fstream
*
ReadWriteStream
)
:
filePath
(
path
),
writeStream
(
writeStream
),
readStream
(
readStream
),
ReadWriteStream
(
ReadWriteStream
)
{
fileReadWrite
::
fileReadWrite
(
char
*
path
,
fstream
*
w
)
:
filePath
(
path
),
ReadWriteStream
(
w
)
{
}
fileReadWrite.h
浏览文件 @
0524c1e5
...
...
@@ -5,8 +5,7 @@
#ifndef CPP_14_FILE_FILEREADWRITE_H
#define CPP_14_FILE_FILEREADWRITE_H
#include "iostream"
#include "string"
#include <fstream> //操作文件的头文件
using
namespace
std
;
...
...
@@ -14,7 +13,7 @@ using namespace std;
//文件的读取与写入
class
fileReadWrite
{
public:
string
filePath
;
//文件路径
char
*
filePath
;
//文件路径
enum
model
{
read
,
write
,
readWrite
...
...
@@ -22,16 +21,16 @@ public:
bool
display
=
false
;
//是否把数据显示到屏幕
ofstream
*
writeStream
=
nullptr
;
//写文件对象
ifstream
*
readStream
=
nullptr
;
//读文件对象
fstream
*
ReadWriteStream
=
nullptr
;
//读写文件
ofstream
*
writeStream
;
//写文件对象
ifstream
*
readStream
;
//读文件对象
fstream
*
ReadWriteStream
;
//读写文件
fileReadWrite
();
/*fileReadWrite();
fileReadWrite(string path);*/
fileReadWrite
(
string
path
);
fileReadWrite
(
string
path
,
ofstream
*
writeStream
);
fileReadWrite
(
string
path
,
ofstream
*
writeStream
,
ifstream
*
readStream
);
fileReadWrite
(
string
path
,
ofstream
*
writeStream
,
ifstream
*
readStream
,
fstream
*
ReadWriteStream
);
fileReadWrite
(
char
*
path
,
ofstream
*
writeStream
);
fileReadWrite
(
char
*
path
,
ifstream
*
readStream
);
fileReadWrite
(
char
*
path
,
fstream
*
ReadWriteStream
);
void
openFile
(
model
model
,
bool
display
);
//打开文件,参数的含义是选择文件的操作模式、文件内容输出目的地
void
close
();
//关闭文件
...
...
main.cpp
浏览文件 @
0524c1e5
#include <iostream>
#include "fileReadWrite.h"
using
namespace
std
;
int
main
()
{
std
::
cout
<<
"Hello, World!"
<<
std
::
endl
;
//文件路径: 在本项目路径下新建一个文本文件叫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
;
return
0
;
}
test.txt
0 → 100644
浏览文件 @
0524c1e5
hello world!
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录