Ray
Ray

Reputation: 31

In c++ how do I read from file x,y coordinates of a rectangle?

The data is in the form:

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

where they are the first two lines of the text file, each representing a triangle.

#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;
}

I tried several ways to get the data into the relevant integers but with no luck. Can someone please help?

Upvotes: 0

Views: 9933

Answers (5)

ArchonOSX
ArchonOSX

Reputation: 121

Just a short program I had to write maybe you can use some of it:

#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;
} 

Upvotes: 0

Jherico
Jherico

Reputation: 29240

C++'s stream input mechanisms are, quite frankly, incredibly annoying to use. If you're able to use a very recent version of C++, you might consider using regular expressions. Fetch the line from the stream using getline and then using a regex expression on each line.. something like ^(?:(\d+),(\d+)\t){3}(\d+),(\d+)$.

Failing that, sscanf can also be used, and while it's not as C++ as you might want, it's much easier to understand than the corresponding stream operations IMO.

Upvotes: 1

derpface
derpface

Reputation: 1691

You say you want to read data into individual integers, but you're reading the entire first line into a string and then displaying it.

If you want to read integers from a text file, which are separated by whitespace characters (spaces, tabs, or newlines), then the operator >> is all you need:

#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;
}

Note that I use a stringstream to read the entire contents of the file into memory first, and then read from that stream. That is an optional step, which I do simply because I prefer the habit of opening and closing the file as quickly as possible before doing any additional work on the contents. This might not be ideal if the file is extremely large. You can use the same operator >> with the ifstream object inside the braces between where you open and close the file, and leave out the stringstream entirely, as demonstrated below:

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;
}

Also note that there is no validation to ensure that you're not trying to read past the end of the file, that what you're reading are in fact integers, that you're only reading a certain number of them, etc. For that, I would advise that you first read up on some simple input validation tutorials.

Upvotes: 0

David G
David G

Reputation: 96845

I already added <fstream> and used the >> operator, however I don't know how to deal with the tabs and the commas and the return in between lines.

The problem is not with the tabs or the newline (because they are considered whitespace by the stream which are extracted and discarded as part of a sentry operation to formatted I/O). The problem is with the commas, because they are not extracted. Doing, for example in >> x >> y will leave y uninitialized if there is a non-numeric character in the stream after the previous extraction to x.

You need a way to extract the comma after the first extraction of the integer. The most simple technique would be to use a dummy char variable to extract into:

int x1, y1;
char dummy;

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

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258618

You could use operator >>:

myfile >> x1;

Note that you should include <fstream>.

Upvotes: 0

Related Questions