I記録システムに取り組んできた、すべてがよさそうだけど構造体データをファイルに挿入するにはどうすればよいですか?
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
struct students{
string studentID;
string surname;
string firstname;
string birthdate;
string sex;
};
int main()
{
fstream collection;
string filename;
short choice;
do{
int ctr=1;
system("cls");
if(collection.is_open()){
cout<<"Active File: ["<<filename<<"]"<<endl;
}else{
cout<<"Active File|: [None opened]"<<endl;
}
cout<<"[1] Create new file"<<endl;
cout<<"[2] Open existing file"<<endl;
cout<<"[3] Manage data"<<endl;
cout<<"[4] Exit"<<endl;
cout<<"Enter operation index: ";
cin>>choice;
switch(choice){
case 1:
cout<<"Enter file name: ";
cin>>filename;
collection.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
collection<<"------------------------------------------------------------------------------"<<endl;
collection<<"Rec \t Student ID \t Surname \t Firstname \t Birthdate \t Sex \t"<<endl;
collection<<"------------------------------------------------------------------------------"<<endl;
collection.close();
collection.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
break;
case 2:
cout<<"Enter file name: ";
cin>>filename;
collection.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
break;
case 3:
string lines;
char menu;
students student[10];
do{
ifstream collection(filename, std::fstream::in | std::fstream::out | std::fstream::app);
if(collection.is_open()){
cout<<"Active File: ["<<filename<<"]";
system("cls");
while(getline(collection,lines)){
cout<<lines<<endl;
}
}
collection.close();
cout<<"[A]dd [E]dit [D]elete [S]ort [F]ilter Sa[V]e e[X]it";
cin>>menu;
if(menu=='A'){
string lines2;
collection.open(filename,ios::app);
system("cls");
ifstream collection(filename, std::fstream::in | std::fstream::out | std::fstream::app);
if(collection.is_open()){
while(getline(collection,lines)){
cout<<lines<<endl;
}
}
cout<<endl<<"Adding data to "<<filename<<endl;
cout<<"Student ID: ";
cin>>student[ctr].studentID;
cout<<"Surname: ";
cin>>student[ctr].surname;
cout<<"Firstname: ";
cin>>student[ctr].firstname;
cout<<"Birthdate: ";
cin>>student[ctr].birthdate;
cout<<"Sex: ";
cin>>student[ctr].sex;
私はここにエラーが発生しておくのはなぜ?
//data insertion code heree
collection.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
collection<<ctr<<"\t"student[ctr].studentID<<"\t"student[ctr].surname<<"\t"student[ctr].firstname<<"\t"student[ctr].birthdate<<"\t"student[ctr].sex<<endl;
collection.close();
ctr++;
}else if(menu=='E'){
}else if(menu=='D'){
}else if(menu=='F'){
}else if(menu=='V'){
cout<<"Saving file..."<<endl;
collection.close();
cout<<"File saved."<<endl;
system("pause");
}else{
cout<<"Invalid input."<<endl;
system("pause");
};
}while(menu!='X');
break;
}
}while(choice!=4);
}
は、なぜ私はエラーを取得していないのです: '演算子< <' の一致 'をコレクション< < CTR' に|エラー?コードはシステムの早い段階で働いていて、うまく機能しませんでした。
このコードはおそらく[mcve](http://stackoverflow.com/help/mcve)以上のものです。できるだけ少ない行でトリミングして、問題を再現してください。 **そして、コンパイラの完全なエラーメッセージを含めてください。** –
'" \ t "student"はあなたの文字列リテラルとあなたの次の値の間に '<<'がありません。あなたが疑問視している行でこれは横行している。 – WhozCraig