将一个矢量写入一个新的文本文件
writing a vector to a new text file
我试图从我的矢量写入数据到一个新的文本文件,在这个过程中创建新的文本文件,下面是我的代码,但我得到一个错误<<就在fs之后,错误状态没有操作符"<<"匹配这些操作数
struct Weather
{
int a_data;
int b_data;
double c_data;
double d_data;
double e_data;
double ans_temp;
};
ofstream &operator << (std::ofstream &f, Weather& obj)
{
f<<obj.a_data;///Etc ...code corresponding to dispaly parameters
return f;
};
int main ()
{
using std::vector;
using std::string;
using std::getline;
using std::cout;
vector<Weather> data_weather;
string line;
ifstream myfile ("weatherdata.txt");
if (myfile.is_open())
{
int count = 0;
while (getline(myfile, line))
{
if (count > 6)
{
int a, b;
double c, d, e;
std::istringstream buffer(line);
std::string e_as_string;
if (buffer >> a >> b >> c >> d >> e_as_string)
{
if (e_as_string == "---")
{
sun = 0.0;
}
else
{
std::istringstream buffer2(e_as_string);
if (!(buffer2 >> sun))
{
sun = 0.0;
}
}
Weather objName = {a, b, c, d, e};
data_weather.push_back(objName);
}
}
count++;
}
myfile.close();
for (auto it = data_weather.begin(); it != data_weather.end(); ++it)
{
it->ans_temp = it->c_data + it->d_data /2;
}
for (auto it = data_weather.begin(); it != data_weather.end(); ++it)
{
std::cout << it->ans_temp << std::endl;
}
std::ofstream fs("newdata.txt");
for(vector<Weather>::const_iterator it = data_weather.begin(); it != data_weather.end(); ++it) {
fs << *it << 'n';
}
}
else
cout << "unable to open file";
scat::pause("nPress <ENTER> to end the program.");
return 0;
}
好像没有重载<<运营商。
friend ostream &operator << (std::ostream &f, Weather& obj)
{
f<<obj.a_data; //Etc ...code corresponding to dispaly parameters
return f;
}
ofstream& operator<< (std::ofstream &f, const Weather& obj)
^^^^
{
f<<obj.a_data;///Etc ...code corresponding to dispaly parameters
return f;
};
或(不推荐)
for(vector<Weather>::iterator it = data_weather.begin(); it != data_weather.end(); ++it)
^^^^
{
fs << *it << 'n';
}
相关文章:
- C++试图读取一个文件并输出到另一个文本文件
- 将所有 *.txt 文件合并到一个文本文件中,其名称由用户使用 C++ 提供
- 我该如何循环遍历我的数组(缓冲区——包含一个文本文件),并将其打印成30字节的块
- 检查一个文本文件中的单词是否在另一个文本文件 (C++) 中
- 我想使用 ofstream 在 cpp 中创建一个文本文件
- 检查一系列单词,形成一个文本文件,保存在字符串vector C 中
- 从一个文本文件加载多个 char 和 int 类型的数组
- 使用文本文件中的值进行计算并写入另一个文本文件
- 为什么当我在Visual c ++ studio 2010中运行应用程序时光标会转到最后一个文本框
- 将三个文本文件组合成一个文本文件,形成一个句子C++
- 控制台程序读取文本文件行,做Levenstein并输出一个文本文件
- 如何将XML文件拆分为多个XML,以便每个新文件只包含原始文件中的一个文本节点
- 读取文本文件中的多行并写入另一个文本文件
- 我把矢量从一个文本文件中归档,它不会变成一行.我该怎么做
- 如何将字符串的特定部分替换为另一个文本
- 在QT中制作了一个文本浏览器,但代码就像它不存在一样?
- 如何正确地将一个文本整数作为参数作为空指针进行传递,并使用其地址作为整数的实际值
- 给定一个单词和一个文本,返回文本中单词字谜的出现次数
- 给定一个单词和一个文本,我们需要返回字谜的出现次数
- 我正试图将一个过程的结果写入一个文本文件,我遇到的问题是列的对齐问题