编程论坛
注册
登录
编程论坛
→
C++教室
从键盘输入一个字符串、一个整数和一个实数,将其写入文件f1.txt中,再用读字符方式从此文本文件中逐个读出并显示在屏幕上
jojo170
发布于 2020-05-17 17:38, 3362 次点击
从键盘输入一个字符串、一个整数和一个实数,将其写入文件f1.txt中,再用读字符方式从此文本文件中逐个读出并显示在屏幕上
1 回复
#2
吕孟伟
2020-05-18 08:13
程序代码:
#include
<iostream>
#include
<fstream>
using
namespace
std;
int
main(
void
)
{
char
buf[
1024
];
int
value;
double
number;
ofstream ofs;
ofs.open(
"
test.txt
"
);
if
(ofs.is_open()){
cout
<<
"
opening file success!
"
<< endl;
}
cout
<<
"
Enter your string :
"
;
cin
.getline(buf,
100
);
ofs << buf << endl;
cout
<<
"
Enter your int :
"
;
cin
>> value;
ofs << value << endl;
cout
<<
"
Enter your double :
"
;
cin
>> number;
ofs << number << endl;
ifstream ifs;
ifs.open(
"
test.txt
"
, ios::in);
if
(ifs.is_open()){
cout
<<
"
opening file success!
"
<< endl;
}
cout
<<
"
Reading the file:
"
<< endl;
ifs >> buf;
cout
<< buf << endl;
ifs >> value;
cout
<< value << endl;
ifs >> number;
cout
<< number << endl;
return
0
;
}
[此贴子已经被作者于2020-5-18 08:14编辑过]
1