c++中如何利用vector fstream进行文件的读取
2012-12-21 14:27
786 查看
#include<iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
int filetovector(string filename,vector<string>& sevc)
{
ifstream infile(filename.c_str());
if(!infile)
return 1;
string s;
while (getline(infile,s))
sevc.push_back(s);
infile.close(); //--关闭文件
if (infile.eof()) //--遇到文件结束符
return 4;
if(infile.bad()) //-- 发生系统故障
return 2;
if(infile.fail()) //读入数据失败
return 3;
}
int t_main(int argc, char *argv[])
{
vector<string> sevc;
string filename,s;
//读入文件名
cout<<"Entet the FileName :";
cin>>filename;
//处理文件
switch(filetovector(filename,sevc))
{
case 1:
cout<<"error:can't open file:"
<<filename<<endl;
return -1;
case 2:
cout<<"error:system failure"<<endl;
return-1;
case 3:
cout<<"error:read failure"<<endl;
return -1;
default:
return 1;
}
//使用istringstream从vector里每次读取一个单词的形式读取所存储的行
istringstream isstream;
for(vector<string>::iterator iter=sevc.begin();iter!=sevc.end();++iter)
{
isstream.str(*iter);
while (isstream>>s)
{
cout<<s<<endl;
}
isstream.clear(); //--将istringstream流设置为有效状态
}
return 0;
}
getline()是按行读取的,
isstream>>s 从流中读取字符串到s 注意 此时流中字符串是以行存储的当读到s中的时候遇到空格就结束读取(开头的空格不算),然后循环读取下空格下一个位置的字符串
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
int filetovector(string filename,vector<string>& sevc)
{
ifstream infile(filename.c_str());
if(!infile)
return 1;
string s;
while (getline(infile,s))
sevc.push_back(s);
infile.close(); //--关闭文件
if (infile.eof()) //--遇到文件结束符
return 4;
if(infile.bad()) //-- 发生系统故障
return 2;
if(infile.fail()) //读入数据失败
return 3;
}
int t_main(int argc, char *argv[])
{
vector<string> sevc;
string filename,s;
//读入文件名
cout<<"Entet the FileName :";
cin>>filename;
//处理文件
switch(filetovector(filename,sevc))
{
case 1:
cout<<"error:can't open file:"
<<filename<<endl;
return -1;
case 2:
cout<<"error:system failure"<<endl;
return-1;
case 3:
cout<<"error:read failure"<<endl;
return -1;
default:
return 1;
}
//使用istringstream从vector里每次读取一个单词的形式读取所存储的行
istringstream isstream;
for(vector<string>::iterator iter=sevc.begin();iter!=sevc.end();++iter)
{
isstream.str(*iter);
while (isstream>>s)
{
cout<<s<<endl;
}
isstream.clear(); //--将istringstream流设置为有效状态
}
return 0;
}
getline()是按行读取的,
isstream>>s 从流中读取字符串到s 注意 此时流中字符串是以行存储的当读到s中的时候遇到空格就结束读取(开头的空格不算),然后循环读取下空格下一个位置的字符串
相关文章推荐
- 如何利用C/C++逐行读取txt文件中的字符串(可以顺便实现文本文件的复制)
- 如何利用C/C++逐行读取txt文件中的字符串(可以顺便实现文本文件的复制)
- 如何利用C/C++逐行读取txt文件中的字符串(可以顺便实现文本文件的复制)
- 如何利用C++ vector的sort算法对元素是自定义类型进行排序
- C++中利用文件输入对象(即ifstream对象)来读取文本文件中带空格的字符串以及混合输入数字与字符串
- 如何利用C/C++逐行读取txt文件中的字符串(可以顺便实现文本文件的复制)
- 如何利用C/C++逐行读取txt文件中的字符串(可以顺便实现文本文件的复制)
- 如何利用C/C++逐行读取txt文件中的字符串(可以顺便实现文本文件的复制)
- C++ 按行读取文件并按空格将数据存储为vector
- C++读取文件夹中特定后缀的文件—dirent—vector
- c++分行读取并利用数字类型文件数据
- C++ 读取配置文件的 ofstream和ifstream详细用法
- c++中ifstream一次读取整个文件
- 如何使用opencv的c++接口来读取、写结构体数组到xml文件中
- 文件读取C++文件读写操作(三)如何统计文本的行数及如何读取文件某一行内容
- 利用OpenCV进行【视频的读取】与【保存视频文件或者图像】源码!~
- 如何使用10M的内存读入100M的文件,并且保证在读取过程中可进行删除、修改和增加内容
- C++读取写入.txt文件(ifstream/ofstream)—读取指定行,修改指定行,复制文件,清除文件,统计文件行数
- c++对文件进行操作之读取文件夹下面的图片名称
- c++ std::ifstream 读取文件不完整? 是不是忘记了ios_base::binary