2013-03-26 108 views
1

我正在寫一個矢量陣列使用ofstream文件,但是某些值不獲取寫入,IE:問題與fstream的

#include <iostream> 
#include <vector> 
#include <fstream> 
#include <string> 
#include <cstdlib> 
using namespace std; 
int main(){ 
    char * hold = new char [100]; 
    vector<double> fx(2049); 
    ifstream inputFile; 
    ofstream myFile; 
    inputFile.open("data.txt"); 
    myFile.open("test.txt"); 
    for (int c=0; c<2049; c++){ 
     inputFile.getline(hold, 100); 
     fx[c] = atof(hold); 
    } 
    for (int c=0; c<2049; c++){ 
     myFile << fx[c] << "\n"; 
    } 
} 

在FX,下半場是所有等於0(FX [1024]至fx [0] = 0)。但是,在test.txt中,這些0值都不存在,因此應用回車符。有什麼想法嗎? 謝謝! (這些問題的格式化的新...任何提示,使這更容易理解)將不勝感激。

注:我意識到這個程序是相當多餘的。實際的程序有更多的功能,這只是一個工作不正確的領域。

+2

我們可以看到'input()'? – chris 2013-03-26 00:39:52

+1

我無法在您發佈的代碼中發現任何問題。也許更多的代碼會有幫助? :o – 2013-03-26 00:42:46

+0

我加入了我用作fx [c]的輸入。 – 2013-03-26 00:47:03

回答

1

試試這個

#include <iostream> 
#include <vector> 
#include <fstream> 
#include <string> 
#include <cstdlib> 

#define MAX_FILE_LINES 2048 


using namespace std; 

//genarate random double number 
double fRand() 
{ 
    double fMin = 100, fMax = 200; 
    double f = (double)rand();  
    return fMin + (f/(fMax - fMin)); 
} 

//init file (if you need to create sample file with list of double numbers, you can use this function) 
void fileInit(){  
    ofstream sourceFile;  
    sourceFile.open("D:\\source.txt"); 
    if (sourceFile.is_open()) 
    { 
     for (int i=0; i<MAX_FILE_LINES; i++){ 
      sourceFile << fRand() << endl; 
     } 
    } 
} 


int main(){ 
    string buffer; 
    vector<double> fx(MAX_FILE_LINES); 
    ifstream sourceFile; 
    ofstream destinationFile; 
    sourceFile.open("D:\\source.txt"); 
    destinationFile.open("D:\\destination.txt"); 

    //reading file lines to vector 
    int lineCount =0; 
    if (sourceFile.is_open()) 
     { 
     while (sourceFile.good()) 
     { 
      getline (sourceFile,buffer); 
      fx[lineCount] = atof(buffer.c_str()); 
      lineCount++; 
      if (lineCount == (MAX_FILE_LINES-1)){ 
       break; 
      } 
     } 
     sourceFile.close(); 
     } 

    //write lines to new file 
    if (destinationFile.is_open()) 
    { 
     for (int i=0; i<MAX_FILE_LINES; i++){ 
     destinationFile << fx[i] << endl; 
     } 
    } 
} 
+0

這效果更好!謝謝!我認爲輸出的緩衝區內存在一些缺陷。我不確定。再次感謝! – 2013-03-26 01:46:32

0

爲什麼用handroll緩衝區螺釘,用於一次性的?無法節省百萬分之一的成本來考慮循環,沒有足夠的浪費來補償。

首先考慮消除不必要的陳述和未經檢查的失敗。

#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <string> 
#include <vector> 
using namespace std; 
int main() 
{ 
    vector<float> data; 
    { 
     ifstream ids("source.txt",ios_base::in); 
     int linenr = 0; 
     for (string line ; getline(ids,line) ;) { 
      ++linenr; 
      decltype(data)::value_type x; 
      istringstream s(line); 
      if (s >> x) 
       data.push_back(x); 
      else 
       cerr << "crap line "<<linenr<<" ignored: " << line << '\n'; 
     } 
    } 
    ofstream ods("object.txt"); 
    for (auto x : data) 
     ods << x << '\n'; 
}