C++ 在C++;
伙计们,我有一个文本“sales.txt”文件,其中包含以下格式的(id、lastname、quarter、sales)C++ 在C++;,c++,C++,伙计们,我有一个文本“sales.txt”文件,其中包含以下格式的(id、lastname、quarter、sales) 123 smith 1 333.20 221 doe 1 345.50 342 johnson 1 774.50 123 smith 2 333.20 221 doe 2 555.50 342 johnson 2 25.50 123 smith 3 254.20 221 doe 3 652.50 342 johnson 3 32.50 123 smith 4 354.20 2
123 smith 1 333.20
221 doe 1 345.50
342 johnson 1 774.50
123 smith 2 333.20
221 doe 2 555.50
342 johnson 2 25.50
123 smith 3 254.20
221 doe 3 652.50
342 johnson 3 32.50
123 smith 4 354.20
221 doe 4 51.50
342 johnson 4 1000.50
我试图将该文件放入一个结构中,以便输出到另一个文本文件,但到目前为止,我在提取“id”和lastname时遇到了问题。下面是代码的一部分。第二种方法是按季度提取销售额并将其放入数组中,但不起作用,如果有人能帮我使用这两种方法的话
谢谢
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct employees { int id; string lname; double qtrSale[4]; double tsale; };
void getIdName(employees list[], ifstream& infile, int num);
int main()
{
ifstream infile;
string file("file1.txt");
infile.open(file);
employees list[lsize];
getIdName(list, infile, lsize);
/*getData(list, file, lsize);*/
for(int i = 0; i < lsize; i++)//checking struct
{
cout << list[i].id << " " << list[i].lname << endl;
}
}
void getIdName(employees list[], ifstream& infile, int lsize)
{
int id;
string lname;
double sale, temp;
for(int i = 0; i < lsize; i++)
{
infile >> list[i].id >> list[i].lname >> temp >> sale;
/*for(int j = 1; j <= 4; j++)
{
list[i].qtrSale[j] = 0.0;
}*/
}
}
void getData(employees list[], string file, int lsize)
{
int id, qtr;
double amount;
ifstream infile(file);
while(infile.good())
{
for(int j = 0; j < lsize; j++)
{
infile >> id;
for(int i = 1; i <= 4; i++)
{
infile >> qtr >> amount;
if(list[j].id == id && qtr == 1) { list[j].qtrSale[i] = amount; }
if(list[j].id == id && qtr == 2) { list[j].qtrSale[i] = amount; }
if(list[j].id == id && qtr == 3) { list[j].qtrSale[i] = amount; }
if(list[j].id == id && qtr == 4) { list[j].qtrSale[i] = amount; }
}
}
}
}
#包括
#包括
#包括
使用名称空间std;
结构雇员{int-id;字符串lname;双qtrSale[4];双tsale;};
void getIdName(员工列表[],ifstream&infle,int num);
int main()
{
河流充填;
字符串文件(“file1.txt”);
infile.open(文件);
员工名单[lsize];
getIdName(列表、填充、lsize);
/*获取数据(列表、文件、lsize)*/
for(int i=0;i>临时>>销售;
/*对于(int j=1;j>id;
对于(int i=1;i>qtr>>金额;
如果(list[j].id==id&&qtr==1){list[j].qtrSale[i]=amount;}
如果(list[j].id==id&&qtr==2){list[j].qtrSale[i]=amount;}
如果(list[j].id==id&&qtr==3){list[j].qtrSale[i]=amount;}
如果(list[j].id==id&&qtr==4){list[j].qtrSale[i]=amount;}
}
}
}
}
<代码> > p>我猜注释部分不起作用了。C++数组从0开始索引,因此应该改为
for(int j=0; j < 4; j++)
{
list[i].qtrSale[j] = 0.0;
}
for(int j=0;j<4;j++)
{
列表[i].qtrSale[j]=0.0;
}
<代码> > p>我猜注释部分不起作用了。C++数组从0开始索引,因此应该改为
for(int j=0; j < 4; j++)
{
list[i].qtrSale[j] = 0.0;
}
for(int j=0;j<4;j++)
{
列表[i].qtrSale[j]=0.0;
}
>P>学习标准C++库。魔术在 STD::ISTRAMAMIATIORATION/<代码>从代码> >代码>,重载<代码>操作符>()/<代码> <代码>雇员>代码>使用<代码> STD::向量< /代码>:
#包括
#包括
#包括
#包括
#包括
结构雇员{
int-id;
std::字符串lastname;
国际季度;
双重销售;
};
模板
std::basic_istream&operator>>(std::basic_istream&in,员工与员工)
{
返回>>e.id>>e.lastname>>e.quarter>>e.sales;
}
int main(int argc,char*argv[])
{
std::ifstream infle(“sales.txt”);
std::vector employees((std::istream_迭代器(infle)),
std::istreamu迭代器();
返回0;
}
>P>学习标准C++库。魔术在 STD::ISTRAMAMIATIORATION/<代码>从代码> >代码>,重载<代码>操作符>()/<代码> <代码>雇员>代码>使用<代码> STD::向量< /代码>:
#包括
#包括
#包括
#包括
#包括
结构雇员{
int-id;
std::字符串lastname;
国际季度;
双重销售;
};
模板
std::basic_istream&operator>>(std::basic_istream&in,员工与员工)
{
返回>>e.id>>e.lastname>>e.quarter>>e.sales;
}
int main(int argc,char*argv[])
{
std::ifstream infle(“sales.txt”);
std::vector employees((std::istream_迭代器(infle)),
std::istreamu迭代器();
返回0;
}
您可以使用sscanf函数解析数据。循环遍历文件,一次抓取一行,然后如下解析该行
`
您可以使用sscanf函数解析数据。循环遍历文件,一次获取一行,然后按如下方式解析该行
`
如果您不介意使用。下面是如何实现的(使用vc++2010进行测试):
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//步骤1,定义您的结构
结构雇员
{
int-id;
std::字符串lname;
int-qtrSale;
双沙;
};
//第二步,调整它
增强融合适应结构(
雇员,,
(int,id)
(std::string,lname)
(国际,qtrSale)
(双份,tsale)
)
//第3步,解析它
void parse_文件()
{
使用名称空间boost::spirit;
//打开文件,禁用跳过空白
std::ifstream-in(“sales.txt”);
in.unset(std::ios::skipws);
std::向量ve;
短语解析
(
//解析自
istream_迭代器(in),
//解析到
istream_迭代器(),
//读取:一个int后跟一个ascii字符串,然后是一个double,最后是一个double,至少重复一次。
+(qi::int\u>>+qi::alpha>>qi::int\u>>qi::double\u>>),
//跳过所有空格
qi::ascii::空格,
//将结果保存到ve
ve
);
//把结果打印出来
对于(int i=0;i
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//步骤1,定义您的结构
结构雇员
{
int-id;
std::字符串lname;
int-qtrSale;
双沙;
};
//第二步,调整它
增强融合适应结构(
雇员,,
(int,id)
(std::string,lname)
(国际,qtrSale)
(双份,tsale)
)
//第3步,解析它
void parse_文件()
{
使用名称空间boost::spirit;
//打开文件,禁用跳过空白
std::ifstream-in(“sales.txt”);
in.unset(std::ios::skipws);
std::向量ve;
短语解析
(
//解析自
istream_迭代器(in),
//解析到
istream_迭代器(),
//读取:一个int后跟一个ascii字符串,然后是一个double,最后是一个double,至少重复一次。
+(qi::int\u>>+qi::alpha>>qi::int\u>>qi::double\u>>),
//跳过所有空格
qi::ascii::空格,
//将结果保存到ve
ve
);
//把结果打印出来
对于(int i=0;i 您的代码未能在my box(gcc4.1.2)上编译,它正在抱怨infle.op
char line []="123 smith 1 333.20" //here for example only
char strname [20] = { 0 };
int id = 0;
int quarter = 0;
double sales = 0.0;
sscanf (line,"%d %s %d %f", &id, strname, &quarter, &sales);`
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/support_istream_iterator.hpp>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
// step 1, define your struct
struct employee
{
int id;
std::string lname;
int qtrSale;
double tsale;
};
// step 2, adapt it
BOOST_FUSION_ADAPT_STRUCT(
employee,
(int, id)
(std::string, lname)
(int, qtrSale)
(double, tsale)
)
// step 3, parse it
void parse_file()
{
using namespace boost::spirit;
// open file, disable skipping of whitespace
std::ifstream in("sales.txt");
in.unsetf(std::ios::skipws);
std::vector<employee> ve;
qi::phrase_parse
(
// parse from
istream_iterator(in),
// parse to
istream_iterator(),
// reads: an int followed by a ascii string, then by a double and finally by a double, repeatedly at least once.
+(qi::int_ >> +qi::alpha >> qi::int_ >> qi::double_),
// skip all spaces
qi::ascii::space,
// save results to ve
ve
);
// print the results out
for(int i = 0; i< ve.size(); i++)
{
std::cout
<< ve[i].id << " "
<< ve[i].lname << " "
<< ve[i].qtrSale << " "
<< ve[i].tsale << std::endl;
}
}
int main()
{
parse_file();
}