zoukankan      html  css  js  c++  java
  • C++文件读取

    #include"stdfx.h"

    int main()
    {
    string strPath = "D:\TEST\vs2013Test\TextQuery\Beautiful Story.txt";
    vector<string> strData;
    ifstream sourceFiles(strPath, ios::in);////构造函数内包含open,自动打开
    if (!sourceFiles.is_open())
    {
    cout << "Open Files Failed!" << endl;
    sourceFiles.close();
    return -1;
    }

    // 获取文件长度
    streamoff i = sourceFiles.tellg();
    sourceFiles.seekg(0, ios::end);
    streamoff datasNum = sourceFiles.tellg();////tell get 获取输入流的位置 tell put (获取写文件指针)

    sourceFiles.seekg(0, 0);
    strData.resize(datasNum, "");

    int dataIndex = 0;
    while (!sourceFiles.eof())
    {
    sourceFiles >> strData[dataIndex];
    dataIndex++;
    }
    sourceFiles.close();


    auto pbegin = strData.begin(),pend = strData.end();
    while (pbegin !=pend)
    {
    cout << *pbegin++<<' ';
    }
    cout << endl;

    return 0;
    }

    #ifndef STDFX_H_H
    #define STDFX_H_H

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

    #endif

    博客内容只为本人学习所感转载亦或自写,不足或错误之处请大家不吝赐教
  • 相关阅读:
    POJ 3630
    Codeforces Round #219 (Div. 2) D题
    Codeforces Round #232 (Div. 2) On Sum of Fractions
    Codeforces Round #232 (Div. 2) C
    撸呀撸的左手(KMP+DP)
    hdu poj KMP简单题目总结
    LCT总结
    bzoj1019 [SHOI2008]汉诺塔
    NOIP2016总结
    p1199八数码问题
  • 原文地址:https://www.cnblogs.com/niupan369/p/4029176.html
Copyright © 2011-2022 走看看