2013-12-12 68 views
0

數據的格式爲:在C++中,如何從文件x,y座標矩形讀取?

0,0 2,0 2,4 0,4 (there are tabs in between each pair) 
5,5 7,5 7,9 0,9 

其中它們的文本文件的前兩行,每個代表一個三角形。

#include <iostream> 
#include <fstream> 
using namespace std; 
int main() 
{ 
    int x1, x2, x3, x4, y1, y2, y3, y4; 
    string coordinates; 
    ifstream myfile; 
    myfile.open("coordinates.txt"); 
    string line = myfile.getline(); 
    myfile>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4; 
    cout << line; 
} 

我嘗試了幾種方法來獲取數據到相關的整數,但沒有運氣。有人可以幫忙嗎?

+0

你嘗試過什麼方法?我只看到你讀一行字符串。如果您向我們展示您的代碼實際上正在嘗試,我們可以糾正您在實際遇到問題的位置。 – derpface

+0

啊忘了提及。我已經添加了fstream並使用了>>操作符,但是我不知道如何處理製表符和逗號以及行間的返回。 – Ray

+1

類似。 MYFILE >> X1 >> X2 >> X3 X4 >>; – Ray

回答

0

你可以使用operator >>

myfile >> x1; 

請注意,您應該包括<fstream>

0

我已經添加<fstream>和使用的>>運營商,但是我不知道如何處理的選項卡和逗號和線之間的回報。

問題不在於製表符或換行符(因爲它們被視爲流的空白,它們作爲格式化I/O的哨兵操作的一部分被提取和丟棄)。問題是用逗號,因爲他們提取的是而不是。例如,如果在先前提取到x之後,流中存在非數字字符,則例如in >> x >> y將使y未初始化。

您需要一種方法在第一次提取整數後提取逗號。最簡單的方法是使用一個虛擬char變量提取到:

int x1, y1; 
char dummy; 

if (myfile >> x1 >> dummy >> y1) 
{ 
    // ... 
} 
+0

謝謝我認爲這樣做 – Ray

0

只是一個短期的計劃,我不得不也許寫的,你可以使用它的一些:

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

int main() 
{ 
    ifstream inFile; 
    float num1, num2, num3, num4, num5, sum, avg; 


    cout << "Reading data from the Z:CLM1.dat file:\n\n"; 

    // Open the file. 
    inFile.open("Z:\\CLM1.dat"); 

    // Read the five numbers from the file. 
    inFile >> num1; 
    inFile >> num2; 
    inFile >> num3; 
    inFile >> num4; 
    inFile >> num5; 

    // Close the file. 
    inFile.close(); 

    // Calculate the sum of the numbers. 
    sum = num1 + num2 + num3 + num4 + num5; 
    avg = sum/5; 

    // Display the five numbers. 
    cout << setprecision(3) << fixed << "Number 1 is " << num1 << "\n" 
     << "Number 2 is " << num2 << "\n" 
     << "Number 3 is " << num3 << "\n" 
     << "Number 4 is " << num4 << "\n" 
     << "Number 5 is " << num5 << "\n" 
     << endl; 

    // Display the sum of the numbers. 
    cout << "The sum of the 5 numbers is: " << sum << endl; 
    cout << "The average of these 5 numbers is: " << avg << endl; 
    getchar(); 
    return 0; 
} 
0

你說你要將數據讀入單個整數,但是您將整個第一行讀入一個字符串並顯示它。

如果你想從一個文本文件,這是由空白字符(空格,製表符,或換行符),那麼運營商>>閱讀分離整數是你所需要的:

#include <iostream> 
#include <fstream> 
#include <sstream> 

using namespace std; 

int main() 
{ 
    // Read in the entire file to a string 
    string fileContents; 

    ifstream fin; 
    fin.open("myfile.txt"); 
    if(fin.is_open()) 
    { 
     getline(fin,fileContents,'\x1A'); 
     fin.close(); 
    } 

    // Use the string to init an istringstream from which you can read 
    istringstream iss(fileContents); 

    // Read from the file contents 
    int x,y,z; 

    iss >> x; 
    iss >> y; 
    iss >> z; 

    // Display the integers 
    cout << x << ' ' << y << ' ' << z << '\n'; 

    return 0; 
} 

注意,我使用stringstream首先將文件的全部內容讀入內存,然後從該流中讀取。這是一個可選步驟,我只是因爲我喜歡在對內容進行任何額外工作之前儘可能快地打開和關閉文件的習慣。如果文件非常大,這可能並不理想。您可以使用同一個運營>>與您打開和關閉文件,並完全離開了字符串流之間的括號內的ifstream的對象,如下面所示:

int main() 
{ 
    int x,y,z; 

    // Open the file 
    ifstream fin; 
    fin.open("myfile.txt"); 
    if(fin.is_open()) 
    { 
     // Read from the file contents 
     fin >> x; 
     fin >> y; 
     fin >> z; 

     fin.close(); 
    } 

    // Display the integers 
    cout << x << ' ' << y << ' ' << z << '\n'; 

    return 0; 
} 

另外請注意,沒有驗證,確保你沒有試圖閱讀文件的結尾,你正在閱讀的內容實際上是整數,你只是閱讀一定數量的文件等等。爲此,我建議你先閱讀一些簡單的輸入驗證教程。

1

非常坦率地說,C++的流輸入機制令人難以置信地令人討厭使用。如果您能夠使用最新版本的C++,則可以考慮使用正則表達式。使用getline從流中獲取行,然後在每行上使用regex表達式。類似^(?:(\d+),(\d+)\t){3}(\d+),(\d+)$

如果失敗了,也可以使用sscanf,雖然它不像您想要的那樣是C++,但它比對應的流操作IMO更容易理解。

+1

我相信一個正則表達式和使用正則表達式的代碼在OP的情況下是過分的。 –

+0

直到我開始使用Perl時,正則表達式讓我覺得太過於誇張。現在我知道了正則表達式,我經常因爲在C++和Java中的笨拙而感到沮喪。在perl中,解析代碼是'($ x1,$ y1,$ x2,$ y2,$ x3,$ y3,$ x4,$ y4)= $ line =〜/ ^(?:(\ d +), d +)\ t)的{3}(\ d +),(\ d +)$ /' – Jherico