(1).要在E盘建立一个文件,在c++中E通e.
(2).输出流 ofstream,输入流ifstream(读取)
(3).打开文件,流对象.open()
(4).关闭文件,流对象.close().
(5).输入就是从磁盘上的文件中读取内容到内存中。并非cin【硬盘到内存】
(6).输出就是将内存中的数据内容输出或则说写入到磁盘的文件中。【内存到硬盘】
================================
【输出流会显示在文档,不会显示在程序】
#include
using namespace std;
#include
#include
int main()
{
string s1="123";
ofstream of; //定义输出流;
of.open("e:\123.txt");
of << "输入的字符串是:"<
return 0;
【利用cin,cout s1会显示在程序,且文档上只显示输入的s1结果】
#include
using namespace std;
#include
#include
int main()
{
string s1;
ofstream of; //定义输出流;
ifstream If; //定义输入流;
of.open("e:\123.txt");
cout << "请输入一个字符串:" << endl; //用cout输出后,显示在程序上,不显示在文档上
cin >> s1; //用cin输出后,显示在程序上,而不会显示在文档上,如果用of输出那么会显示;
of << "输入的字符串是:"< of.close(); return 0; } 【读入与读取一起用】 #include using namespace std; #include #include int main() { string s1="2000"; ofstream of; //定义输出流; ifstream If; //定义只读取文件; of.open("e:\123.txt"); of << "利用ofstream的字符串是:" << s1 << endl; //被读取的文件 of.close(); If.open("e:\123.txt"); s1 = "888"; If >> s1; //读取文档123.txt到s1 cout<< "读取123.txt的文件是:" << s1 << endl; return 0; }



