2017-08-31 24 views
0

我有4行3列如何打印二維對向量的值?

(0.165334,0) (0.166524,-0.0136064) (-0.144899,0.0207161) 
(0.205171,0) (0.205084,-0.0139042) (-0.205263,0.0262445) 
(0.216684,0) (0.215388,-0.0131107) (-0.193696,0.0251303) 
(0.220137,0) (0.218849,-0.0135667) (-0.194153,0.025175) 

我寫了下面的代碼打印的FFTfile值的文本文件。該腳本不會引發任何錯誤,但不會打印值。任何想法什麼是錯的?

#include <iostream> 
#include <stdio.h> 
#include <fstream> 
#include <stdlib.h> 
#include <math.h> 
#include <vector> 
#include <algorithm> 
#include <dirent.h> 
#include <string> 
#include <sstream> 
using namespace std; 

class Point 
{ 
public: 
    double x; 

    double y; 

    friend istream& operator>>(istream& input, Point& p); 
    friend ostream& operator<<(istream& s, Point& p); 

    double getX(void); 

    double getY(void); 


}; 

    double Point::getX(void){ 
     return x; 
     } 

    double Point::getY(void){ 
     return y; 
     } 

istream& operator>>(istream& input, Point& p) 
{ 
    char c; 
    input >> c; // Read open parenthesis 
    input >> p.x; 
    input >> c; // Read comma 
    input >> p.y; 
    input >> c; // Read closing parenthesis 

    return input; 
}; 

ostream& operator<<(ostream& s, Point& p) 
{ 
    s << p.getX() << ", " << p.getY(); 
    return s; 
} 

vector<vector<Point> > LoadFFT(string path){ 
    string Filename; 
    vector<vector<Point> > matrix; 
    Filename.append(path);                 
    Filename.append("....txt"); 
    ifstream fileFFT(Filename.c_str()); 
    string raw_text; 
    while(getline(fileFFT, raw_text)){ 

     vector<Point> row; 
     istringstream(raw_text); 
     Point p; 
     while(raw_text >> p){ 
      row.push_back(p); 

     } 

    matrix.push_back(row); 
    } 

return(matrix); 
} 

int main(){ 
    vector<vector<Point> > FFTfile=LoadFFT("..."); 

    for (int i = 0; i < FFTfile.size(); i++) 
    { 
     for (int j = 0; j < FFTfile[i].size(); j++){ 
      cout << FFTfile[i][j]; 
     } 

    } 

    return(0); 
} 
+0

你永遠不會測試文件是否成功打開。你怎麼能確定該文件實際打開? – Fureeish

+0

我只是一個小菜人!用C/C++經驗還不到一個月,你能幫上忙嗎? – Spandy

+0

你缺乏經驗並不能讓你受到不同的待遇。我建議檢查文件打開是否成功。你有沒有試過檢查你的程序是否適用於它? – Fureeish

回答

2

如果文件成功打開,一個問題似乎是下列設置的行:

istringstream(raw_text); 
    Point p; 
    while(raw_text >> p) 

您還沒有建立在你在哪裏發出>>調用點std::istringstream對象。相反,臨時對象被創建並立即銷燬。

這應該是:

istringstream strm(raw_text); 
    Point p; 
    while(strm >> p) 

話雖如此,我很驚訝,沒有錯誤編譯的代碼。

+0

與你一起驚喜。仍試圖找出那一個。它看起來像是'istringstream(raw_text);'是被看作是'istringstream raw_text;'並且允許編譯。例如:https://godbolt.org/g/CTcWw6爲什麼呢,我會留給那些比我更擅長語言的細節的人。 – user4581301

+0

@ user4581301你問過嗎?我也想了解它...... – bezet

+0

奇怪,因爲這裏是OP的代碼(有一些變化),[編譯成功](https://godbolt.org/g/kXWsvb)。 – PaulMcKenzie

1

您不加載文件。您撥打:LoadFFT("...")函數的結果文件名爲:....... txt這不是有效的文件名。 stringstream變量(重新)定義是錯誤的,不需要中間字符串,也不需要它們的對應關係。蒸餾LoadFFT()功能是:

vector<vector<Point>> LoadFFT(const char* path){ 
    vector<vector<Point> > matrix; 
    ifstream fileFFT(path); 
    string raw_text; 
    while (getline(fileFFT, raw_text)){ 
     vector<Point> row; 
     istringstream iss(raw_text); 
     Point p; 
     while (iss >> p){ 
      row.push_back(p); 
     } 
     matrix.push_back(row); 
    } 
    return(matrix); 
} 

修改後的main()功能,允許每行後的新行:

int main(){ 
    vector<vector<Point> > FFTfile = LoadFFT("myfile.txt"); 
    for (size_t i = 0; i < FFTfile.size(); i++){ 
     for (size_t j = 0; j < FFTfile[i].size(); j++){ 
      cout << FFTfile[i][j]; 
     } 
     cout << std::endl; 
    } 
} 

這些標題,您需要:

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

您可以刪除其他。

1

您正在使用raw_text(string)作爲istream類型的輸入參數。我不認爲這種轉換是可能的。 嘗試使用istringstream返回值作爲「< <」的參數或更改您的代碼,以便從文件直接複製到您的矩陣。

編輯:上面的評論做得更好解釋它