2016-10-17 64 views
-1

我試圖從文本文件中的傳感器寫入當前時間和一些數據(幅度距離&)。數據量非常大(傳感器以50Hz頻率旋轉,數據數量可能爲5000次)。現在,我想在這樣一個單一的行頭,然後寫當前時間的所有數據,C++中的多重for循環

11:23:17 (time) 
distance1 amplitude1; distance2 amplitude2; ... distance5000 amplitude5000; 
11:23:18 
distance1 amplitude1; distance2 amplitude2; ... distance5000 amplitude5000; 
. 
. 
. 
11:27:00 
distance1 amplitude1; distance2 amplitude2; ... distance5000 amplitude5000; 

「所以我的問題是如何獲得的?」
我可以只寫像下面

for(int t=0; t<distances.size(); t++) 
{ 
    pfsave << distances[t] <<"\t" << amplitudes[t]<<";"; 
} 
pfsave<<endl; 

注意距離和數據:距離的數據類型&幅度

vector<uint32_t> distacnes; 
vector<uint32_t> amplitudes; 
+2

看來你忘了,包括在你的問題一個問題如下。 – Biffen

+1

當你想把所有的數據放在一行上時,我建議不要寫'endl'。 – flyx

回答

1

你可以寫當前時間這樣的,開始前您for循環:

auto t = std::time(nullptr); 
auto tm = *std::localtime(&t); 
pfsave << std::put_time(&tm, "%H:%M:%S") << std::endl; 

那麼你對於沒有endl循環,寫一行代碼:

for(int t=0; t<distances.size(); t++) 
{ 
    pfsave << distances[t] <<"\t" << amplitudes[t]<<";"; 
} 

最後加endl,完成行:

pfsave << endl; 

編輯:您的評論

bool canContinue = true; // Condition used to stop the loop when needded 
while(canContinue) 
{ 
    // Read data from your scanning device 
    distances = ... ; 
    amplitudes = ... ; 

    // Write output file 
    auto t = std::time(nullptr); 
    auto tm = *std::localtime(&t); 
    pfsave << std::put_time(&tm, "%H:%M:%S") << std::endl; 

    for(int t=0; t<distances.size(); t++) 
     pfsave << distances[t] <<"\t" << amplitudes[t]<<";"; 
    pfsave << endl; 

    // Update of canContinue 
    canContinue = ... ; 
} 
+0

感謝您的建議。我已經這樣做了。 –

+0

事情是,如果我這樣做,它只會寫一行(所有數據只從一次掃描),然後它會停止。我想連續寫入數據。 –

+1

然後你需要把所有這些放在'while'或'for'循環中,在每次迭代中抓住'距離'和'振幅'。 – Gwen