2013-12-12 103 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