
c++输入与输出流操作与文件操作
编程,打开exampli.txt,向其中写入4行字符,关闭文件,然后以读的方式打开,输出文件内容。...
编程,打开exampli.txt ,向其中写入4行字符,关闭文件,然后以读的方式打开,输出文件内容。
展开
1个回答
2012-05-15
展开全部
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
int main()
{
using namespace std;
string str;
ofstream fout("exampli.txt");
if (!fout)
{
cerr << "写入 exampli.txt 文件失败。\n";
exit(EXIT_FAILURE);
}
while (getline(cin, str) && str != "")
{
fout << str << endl;
}
fout.close();
ifstream fin("exampli.txt");
if (!fin)
{
cerr << "打开 exampli.txt 文件失败。\n";
exit(EXIT_FAILURE);
}
while (!fin.eof())
{
getline(fin, str);
cout << str << endl;
}
fin.close();
return 0;
}
#include <fstream>
#include <cstdlib>
#include <string>
int main()
{
using namespace std;
string str;
ofstream fout("exampli.txt");
if (!fout)
{
cerr << "写入 exampli.txt 文件失败。\n";
exit(EXIT_FAILURE);
}
while (getline(cin, str) && str != "")
{
fout << str << endl;
}
fout.close();
ifstream fin("exampli.txt");
if (!fin)
{
cerr << "打开 exampli.txt 文件失败。\n";
exit(EXIT_FAILURE);
}
while (!fin.eof())
{
getline(fin, str);
cout << str << endl;
}
fin.close();
return 0;
}
追问
getline()这个函数什么意思,怎么用
追答
getline() 函数在默认情况下读取整行。
至于怎么用,看我写的参考资料。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询