码迷,mamicode.com
首页 > 编程语言 > 详细

C++文件读取

时间:2014-10-16 18:12:02      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:io   os   sp   文件   ef   bs   size   c++   as   

#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

 

C++文件读取

标签:io   os   sp   文件   ef   bs   size   c++   as   

原文地址:http://www.cnblogs.com/niupan369/p/4029176.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!