使用向量C ++从.txt文件读取数据ID数据

时间:2019-06-03 06:59:52

标签: c++ for-loop vector struct

我可以从txt文件获取id数据并使用矢量显示它吗,并且如果输入数据> 1,我想进行循环

这是结构

struct databrg{ 
        int id;
        char namabrg[10];
        int harga;
        int jumlahbrg;

}databrg;

这是向量

vector<string> explode(string const & s, char delim){ 
    vector<string> result;
    istringstream iss(s);
    for (string id; getline(iss, id, delim); ){
        result.push_back(id);
    }
    return result;
}

这是条目data()

void entri_data(){ //proses entri data
  ofstream file ("stok.txt", ios::app); 

   char tambah;
   cout<<"Apakah anda ingin entri data ? (Y/N) "<<endl; 
    cin>>tambah;
    while(tambah == 'y'){
    cout<<"Jumlah data yang ingin dientri: ";cin>>n;
     // n is how much the data entry and if the data > 1 and will loop the entri data again ?
    cout<<"Data ke "; // i want to show the current entry data id from txt file using vector but i don't know how ?

    cout<<"Nama Barang: ";
    cin>>databrg.namabrg;
    cout<<"Harga Barang: ";
    cin>>databrg.harga;
    cout<<"Jumlah Barang: ";
    cin>>databrg.jumlahbrg; 
      if(file.is_open()){ 
        file << databrg.id <<" "<< databrg.namabrg <<" "<< databrg.harga << " "<< databrg.jumlahbrg << "\n";
        cout<<"Data Berhasil ditambahkan\n";
        file.clear();
      }else cout << "Tidak bisa membuka file\n"; 
      break;
    }
}

0 个答案:

没有答案