文件处理实践

1.输出布尔数据

2.输出整型数据

3.输出浮点数

4.取消省略空白符

5.istringstream使用

6.ostringstream使用

7.读取文件

8.写入文件

#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <fstream>

using namespace std;

/*
 * 1.输出布尔数据
 * 2.输出整型数据
 * 3.输出浮点数
 * 4.取消省略空白符
 * 5.istringstream使用
 * 6.ostringstream使用
 * 7.读取文件
 * 8.写入文件
 * */

int main() {

    bool falg = false;
    cout << "falg="<< falg <<endl;
    cout << "falg=="<< boolalpha << falg <<endl;

    cout << showbase;
    cout << "十进制"<< dec << 9 << endl;
    cout << "八进制"<< oct << 9 << endl;
    cout << "十六进制"<< hex << 12 << endl;
    cout << noshowbase;

    double PI = 3.14159265358979323846;
    cout.precision(3);
    cout << "pi="<< PI << endl;
    cout << setprecision(5) << "pi=="<< PI << endl;
    float f = 13.10000;
    cout << showpoint << "f="<<f << noshowpoint << endl;

    cin >> noskipws;
    char c;
    cout << "请输入字符: "  << endl;
    cin >> c;
    cout << c;
    cin >> skipws;

    string s1 = "abc def";
    istringstream stream(s1);
    string s2;
    while (stream >>s2){
        cout << s2 << endl;
    }

    int aa = 5;
    double bb = 3.142351;
    string str = "";
    ostringstream oss;
    oss << aa << "---"<< bb;
    str = oss.str();
    cout << str << endl;

    fstream file("D:\\CLionProjects\\Mytest\\day07\\04_do_io\\a.txt",ios::in);

    if(file.is_open()){
        string line;
        while (getline(file,line)){
            cout<< line <<endl;
        }
        file.close();
    }else{
        cout<< "文件无法打开"<<endl;
    }

    fstream file2{"D:\\CLionProjects\\Mytest\\day07\\04_do_io\\a2.txt",ios::app};
    if(file2.is_open()){
        cout<< "正常打开文件"<<endl;
        file2 << ",ni hao";
        file2 << endl;
        file2.close();
    }else{
        cout<< "无法正常打开文件"<< endl;
    }

    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值