2016-12-25 88 views
0

這是我的項目的代碼..現在我想存儲名稱在一個字符串變量,然後性別在Char類型變量等等。 此代碼存儲名字在sepreate變量和姓氏在其他變量。 我如何存儲不同的變量?如何在一個文件中讀取不同的數據類型? C++

#include<iostream> 
#include<string> 
#include<fstream> 
using namespace std; 


int main() 
{ 

    string array[14][10]; 
    ifstream read("file.txt"); 
    if(read.fail()) 
    cerr << "ërrier"<< endl; 


    for(int i=0;!read.eof();i++) 
    { 
     for(int j=0;j<10;j++) 
     { 
      read>> array[i][j]; 
      cout<< array[i][j]<<" "; 
     } 

     cout<< endl; 

} 

    read.close(); 

    return 0; 
} 

這個文件我想讀..幫我

+0

我想你是焦炭從文件中讀取字符,你必須閱讀你寫一個模式文件它; –

+0

我正在閱讀字符串的字符串...我不知道如何閱讀給定的模式 –

+2

請發送代碼的文件 –

回答

0

我還沒有仔細地知道你的目的。但如果你的目的是在幾個字符串變量或其他類型的商店名稱和家庭, 您可以使用指針:

std::string *name; 
std::vector<std::string *> names; 
for(int i=0;!read.eof();i++) 
    { 
     for(int j=0;j<10;j++) 
     { 
      name = new std::string; 
      read>> *name; 
      cout<< *name<<" "; 
     //this line help us to keep pointers, you can access to pointers,  
     names.pushback(name). 
     } 

     cout<< endl; 
} 
相關問題