CppPrimer自学(2)文件流

1214CPP文件流(1)

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    //====== 1 =====
    ifstream infile; //创建读文件的变量infile
    infile.open("one.txt");//用open函数读取one.txt文件 而且要用c风格的c_str()
    string s;//创建字符串变量s
    while(infile >> s)//将读取到的数据写入s中 while可以读取多行
        cout << s << endl;//输出s
    infile.close();
//========  2  =====
    ofstream outfile("123.txt");//创建名字为123的txt文件
    outfile << "aaaaaaa";//在名为123文件里写入aaaaaaaa
    outfile.close();//关闭文件

//========= 3 ======
    string file("two.txt"); //文件名放在string变量里
    ifstream infile1(file.c_str());
    string s1;
    while (infile1 >> s1) //infile1的状态是endoffile的时候循环结束,此时用infile1去读第二个文件的时候会失败,因为此时infile1已结是eof
        cout << s1 << endl;
    infile1.close();
    infile1.clear();//恢复infile1的状态, 避免在之前已结eof
//======== 4 ======
    string file1("three.txt");
    ifstream infile2;//创建一个没有绑定文件的流文件
    infile2.open(file1.c_str());//用open绑定文件
    if (!infile2)
    {
        cerr << "Error to open: " << file1 << "." << endl;
        return -1;
    }
    else
    {
        string s2;
        while (infile2 >> s2)
            cout << s2 << endl;
    }
    infile2.close();
    system("pause");
} 

1214CPP文件流(2)

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
void procrss(string s)
{
    cout << s << endl;
}

int main()
{
    vector<string>files;
    files.push_back("one.txt");
    files.push_back("two.txt");
    files.push_back("three.txt");
    files.push_back("1.jpg");

    string s;
    vector<string>::iterator it = files.begin();
    while (it != files.end())
    {
        ifstream input(it->c_str());
        if (!input) //打开失败
        {
            cerr << " Erro: can not open file:" << *it << endl;
            input.clear();
            ++it;
            //continue; //可以用可以不用 不用的时候在下面加上else 
        }
        else{
            while (input >> s)
                procrss(s);
            input.close();
            input.clear();
            ++it;
        }
    }
    system("pause");
}

1214CPP标准IO库

//自定义头文件get 
#ifndef GET_H
#define GET_H
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

std::istream &get(std::istream & in);

#endif

//添加get.cpp
#include "get.h"

istream &get(istream & in)
{
    int ival;

    while (in >> ival, !in.eof())
    {
        if (in.bad())
            throw runtime_error("IO stream corrupted");
        if (in.fail())
        {
            cerr << "bad data, try again" << endl;
            in.clear();
            in.ignore(200, '\n');
            continue;
        }
        cout << "输入的数据 " << ival << endl;
    }
    in.clear();

    return in;
}
//主函数
#include "get.h"

int main()
{
    cout << "测试新写的函数" << endl;
    //==========1=======
    string fileName; 
    cout << "Enter file name:" << endl;
    cin >> fileName;

    ifstream inFile(fileName.c_str());
    if (!inFile)
    {
        cerr << "error:can not open:" << fileName << endl;
        return -1;
    }
    get(inFile);//inFile是属于ifstream的 而ifstream是istream的子类 所以可以用infile作为参数
//========= 2 ========
    double dval;
    get(cin);
    cout << "继续使用cin,输入一个double: " << endl;
    cin >> dval;
    cout << "输入的数据是:" << dval << endl;

    system("pause");
}

2 输出一个文件里每一行的字符(用向量表示)

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int fileToVector(string fileName, vector<string> &svec)
{
    ifstream inFile(fileName.c_str());
    if (!inFile)
    {
        return 1;
    }
    string s;
    while (getline(inFile, s))
        svec.push_back(s);
    inFile.close();
    if (inFile.eof())
        return 4;
    if (inFile.bad())
        return 2;
    if (inFile.fail())
        return 3;
}

int main()
{
    cout << " 测试一下" << endl;
    vector<string>svec;
    string fileName, s;

    cout << "enter fileName: ";
    cin >> fileName;

    switch (fileToVector(fileName, svec))
    {
    case 1:
        cout << "error to open file" << fileName << endl;
        return -1;
    case 2:
        cout << "error systeme failture" << endl;
        return -1;
    case 3:
        cout << "error read filename " << endl;
        return -1;
    }
    cout << " 向量里的内容: " << endl;
    for (vector<string>::iterator iter = svec.begin(); iter < svec.end(); iter++)
        cout << *iter << endl;
    cout << “行数:“ << svec.size() << endl; //行就是向量的大小或者个数
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值