c++读取txt文件
时间:2010-08-16 来源:飞跃时尚
/*
工程文件夹下即可
次程序涉及文件操作,以下是文件名和文件内容:
data.txt输入文件
-----------------------
8
zhangSan 23 beijing
lisi 123 shanghai
mawu 56 liaoning22
zhao3LIU 12 guangxi
huangqi 30 jiangxi
wangba -32 hu2nan
haijiu 43 yunnan
haoshi 21 xinjiang
------------------------
err.txt输出文件,运行之后会显示如下内容
------------------------
lisi 123 shanghai
mawu 56 liaoning22
zhao3LIU 12 guangxi
wangba -32 hu2nan
*/
#include<iostream>
#include<fstream>
#include<strstream>
#include<string>
#include<vector>
using namespace std;
typedef struct
{
string name;
int age;
string add;
void out(){cout<<"Name:"<<name<<endl<<"Age:"<<age<<endl<<"Add:"<<add<<endl;}
}student;
int main()
{
ifstream infile("data.txt",ios::in);
vector<int>err;
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
string s;
int n,i=0,flag=0;
//student st[8];
infile>>n;
student *st=new student[n];
while(n--)
{
//getline(infile,s);
flag=0;
infile>>st[i].name>>st[i].age>>st[i].add;
//infile>>st[i].age;
//infile>>st[i].add;
string::size_type j;
string c;
for(j=0;j<st[i].name.size();j++)
if(!isalpha(st[i].name[j]))
{
flag=1;
break;
}
if(flag==0)
{
if(st[i].age>120||st[i].age<0)
flag=1;
}
if(flag==0)
{
for(j=0;j<st[i].add.size();j++)
if(!isalpha(st[i].add[j]))
{
flag=1;
break;
}
}
if(flag==1)err.push_back(i);
i++;
}
infile.close();
ofstream outfile("err.txt");
if(!outfile)
{
cerr<<"open err.txt error!"<<endl;
exit(1);
}
vector<int>::size_type ei;
for(ei=0;ei<err.size();ei++)
outfile<<st[err[ei]].name<<" "<<st[err[ei]].age<<" "<<st[err[ei]].add<<endl;
outfile.close();
delete [] st;
return 0;
}
工程文件夹下即可
次程序涉及文件操作,以下是文件名和文件内容:
data.txt输入文件
-----------------------
8
zhangSan 23 beijing
lisi 123 shanghai
mawu 56 liaoning22
zhao3LIU 12 guangxi
huangqi 30 jiangxi
wangba -32 hu2nan
haijiu 43 yunnan
haoshi 21 xinjiang
------------------------
err.txt输出文件,运行之后会显示如下内容
------------------------
lisi 123 shanghai
mawu 56 liaoning22
zhao3LIU 12 guangxi
wangba -32 hu2nan
*/
#include<iostream>
#include<fstream>
#include<strstream>
#include<string>
#include<vector>
using namespace std;
typedef struct
{
string name;
int age;
string add;
void out(){cout<<"Name:"<<name<<endl<<"Age:"<<age<<endl<<"Add:"<<add<<endl;}
}student;
int main()
{
ifstream infile("data.txt",ios::in);
vector<int>err;
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
string s;
int n,i=0,flag=0;
//student st[8];
infile>>n;
student *st=new student[n];
while(n--)
{
//getline(infile,s);
flag=0;
infile>>st[i].name>>st[i].age>>st[i].add;
//infile>>st[i].age;
//infile>>st[i].add;
string::size_type j;
string c;
for(j=0;j<st[i].name.size();j++)
if(!isalpha(st[i].name[j]))
{
flag=1;
break;
}
if(flag==0)
{
if(st[i].age>120||st[i].age<0)
flag=1;
}
if(flag==0)
{
for(j=0;j<st[i].add.size();j++)
if(!isalpha(st[i].add[j]))
{
flag=1;
break;
}
}
if(flag==1)err.push_back(i);
i++;
}
infile.close();
ofstream outfile("err.txt");
if(!outfile)
{
cerr<<"open err.txt error!"<<endl;
exit(1);
}
vector<int>::size_type ei;
for(ei=0;ei<err.size();ei++)
outfile<<st[err[ei]].name<<" "<<st[err[ei]].age<<" "<<st[err[ei]].add<<endl;
outfile.close();
delete [] st;
return 0;
}
相关阅读 更多 +