C++中如何换行写入文件?

来源:百度知道 编辑:UC知道 时间:2024/07/07 10:06:55
在C++中,ofstream fp;
fp.open("stu.txt",ios_base::binary);
fp.write("tiny",4);
我这样向stu.txt中输入了tiny这几个字,如何在下一行再输入几个字?也就是这么实现换行输入的啊??????

在C++中,换行写入文件可以使用endl进行换行,实例如下:

// file.cpp : Defines the entry point for the console application.
//
//文件流 fstream
//输入文件流 ifstream
//输出文件流 ofstream
#include "stdafx.h"
#include<iomanip.h>
#include<fstream.h>
void main()
{
    ofstream f1("d:\\1.txt"); //打开文件用于写,若不存在则创建它
    if(!f1)return;  //打开文件失败则退出运行
    //f1<<setw(20)<<"姓名:"<<"吴国燕"<<endl; //使用插入运算符写文件内容,endl即为换行符号
    //f1<<setw(20)<<"地址:"<<"香港路99号"<<endl;
    f1<<"姓名:"<<"吴军平"<<endl; //使用插入运算符写文件内容
    f1<<"地址:"<<"太昊路3号"