4.6
来源:互联网 发布:lol刷点卷软件免费版 编辑:程序博客网 时间:2024/06/05 04:43
/*
*编写函数打开文件用于输入,、
*将文件内容读入string类型的vector容器,
*每一行存储为该容器对象的一个元素
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int fileToVector(string fileName, vector<string>& svec)
{
//创建ifstream对象inFile并绑定到由形参fineName指定的文件
ifstream inFile(fileName.c_str());
if(!inFile)
return 1;//打开文件失败
//将文件内容读入到string类型的vector容器
//每一行存储为该容器对象的一个元素
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()
{
vector<string> svec;
string fileName,s;
//读入文件名
cout << "enter filename:" << endl;
cin >> fileName;
//处理文件
switch( fileToVector(fileName, svec))
{
case 1:
cout << "error :can not to open file :"
<< fileName << endl;
return -1;
case 2:
cout << "error:system failure"<< endl;
return -1;
case 3:
cout << "error:read failure" << endl;
return -1;
}
//使用istringstream从vector里每次读一个单词的形式读取所存储的行
string word;
istringstream isstream;
for(vector<string>::iterator iter =svec.begin();iter != svec.end();++iter)
{
//将vector对象的当前元素复制给istringstream对象
isstream.str(*iter);
//从istringstream对象中读取单词并输出
while(isstream >> word)
{
cout <<word << endl;
}
isstream.clear(); //将istringstream流置为有效状态
}
system("pause");
return 0;
}
- 4.6
- 4.6
- 4.6
- 4.6
- 4.6
- 4.6
- 4.6
- 4.6
- 4.6
- 4.6层叠
- 4.6笔记
- 串4.6
- flex 4.6
- CareerCup-4.6
- CareerCup 4.6
- Exercise 4.6
- 4.6 Successor
- 4.6 总结
- noj 邮票分你一半&&z的生日
- 软件系统架构有感
- 一个项目多文件组织
- DWR3.0简单实例
- C#自动登录开源中国
- 4.6
- Ghost11之DISK TO DISK引发的血案
- HDOJ 4524 郑厂长系列故事——逃离迷宫 杭电 ACM
- Java面试宝典系列之基础面试题String、变量、类与对象、集合类、SSH
- 苹果iPad强于分销渠道:平板领域将继续强势
- SGU 101
- 两列布局
- 防御型编程
- 谷歌的愿景