2012-04-26 9 views
0

整数で整数を読み込むようにファイルを読みたいです。私は行ごとにそれを読んだが、整数で整数を読みたい。整数ファイルの読み込みによる整数

これは私のコードです:

void input_class::read_array() 
{  
     infile.open ("time.txt"); 
     while(!infile.eof()) // To get you all the lines. 
     { 
      string lineString; 
      getline(infile,lineString); // Saves the line in STRING 
      inputFile+=lineString; 
     } 
     cout<<inputFile<<endl<<endl<<endl; 
     cout<<inputFile[5]; 

     infile.close(); 
} 
+0

ファイルの形式はどのようなものです:

それを行うための別の方法は、このですか?それはバイナリですか?そうでない場合は、整数値間の区切り文字は何ですか? – Chad

答えて

0

あなたはoperator>>を使用してintにfstreamのから読み取ることができます。

int n; 
infile >> n; 
+0

といつ停止するのですか? – Yola

3

あなたがこれを行う必要があります。

#include <vector> 

std::vector<int> ints; 
int num; 
infile.open ("time.txt"); 
while(infile >> num) 
{ 
    ints.push_back(num); 
} 

のループをするときに終了しますEOFに到達するか、または非整数を読み取ろうとします。ループの仕組みを詳しく知るには、私の答えhereherehereを読んでください。

#include <vector> 
#include <iterator> 

infile.open ("time.txt"); 
std::istream_iterator<int> begin(infile), end; 
std::vector<int> ints(begin, end); //populate the vector with the input ints 
関連する問題