C++ 结构数组写入文本文件
我的结构数组的文件写入函数有问题。我收到错误,C++ 结构数组写入文本文件,c++,struct,writetofile,C++,Struct,Writetofile,我的结构数组的文件写入函数有问题。我收到错误,无法将'cars[n]'从'car'转换为'std::string{aka std::basic_string}' 我对文件写作有点困惑,也许有人能解释一下或者给我一些提示,如何让我的写作功能发挥作用 我的代码: #include <iostream> #include <string> #include <sstream> #include <stdlib.h> #include <fstrea
无法将'cars[n]'从'car'转换为'std::string{aka std::basic_string}'
我对文件写作有点困惑,也许有人能解释一下或者给我一些提示,如何让我的写作功能发挥作用
我的代码:
#include <iostream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <fstream>
using namespace std;
#define N_CARS 2
struct car{
string model;
int year;
double price;
bool available;
}cars [N_CARS];
void writeToFile(ofstream &outputFile, string x )
{
outputFile << x << endl;
}
int main ()
{
string mystr;
string mystr2;
string mystr3;
int n;
for (n=0; n<N_CARS; n++)
{
cout << "Enter title: ";
getline (cin,cars[n].model);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> cars[n].year;
cout << "Enter price: ";
getline (cin,mystr2);
stringstream(mystr2) >> cars[n].price;
cout << "Choose availability: ";
getline (cin,mystr3);
stringstream(mystr3) >> cars[n].available;
}
ofstream outputFile;
outputFile.open("bla.txt");
for (n=0; n<N_CARS; n++)
writeToFile(outputFile, cars[n]);
outputFile.close();
system("PAUSE");
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
#定义N_车2
结构车{
弦模型;
国际年;
双倍价格;
bool可用;
}汽车;
无效写入文件(流和输出文件,字符串x)
{
输出文件
outputFile是否正确?确保错误消息与此错误无关:WriteFile(outputFile,cars[n]);
?编译器显示存在此问题。但如何更改它?再次检查函数的参数类型。Car
能否转换为字符串
?
void writeToFile(ofstream &outputFile, string x )
{
outputFile << x << endl;
}
writeToFile(outputFile, cars[n]);