10 Replies - 278 Views - Last Post: 04 July 2013 - 10:09 AM Rate Topic: -----

#1 aries0152  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 13-September 09

Read data from a text file and check them using a function

Posted 03 July 2013 - 03:31 AM

I have a text file named "RectCoordinates.txt" and it contains data like this-

Quote

1 0.0000 0.0000 0.8147 0.0000 0.8147 0.1355 0.0000 0.1355 0.1104
2 0.8147 0.0000 1.0000 0.0000 1.0000 0.1355 0.8147 0.1355 0.0251
3 0.8147 0.1355 0.9058 0.1355 0.9058 0.8350 0.8147 0.8350 0.0637
4 0.0000 0.1355 0.1270 0.1355 0.1270 0.9689 0.0000 0.9689 0.1058
5 0.9058 0.1355 0.9134 0.1355 0.9134 0.2210 0.9058 0.2210 0.0006
6 0.9058 0.8350 1.0000 0.8350 1.0000 1.0000 0.9058 1.0000 0.0155



I want to take input from that txt file and check if it satisfy the function named "get_touching_rectangles". So I write the code below-




#include <iostream>
#include <vector>
#include <fstream>

using namespace std;


struct Rectangle {
    int x1, y1;
    int x2, y2;
    int x3, y3;
    int x4, y4;
};


bool operator==(const Rectangle& lhs, const Rectangle& rhs) {
    return  lhs.x1 == rhs.x1 && lhs.y1 == rhs.y1 &&
            lhs.x2 == rhs.x2 && lhs.y2 == rhs.y2 &&
            lhs.x3 == rhs.x3 && lhs.y3 == rhs.y3 &&
            lhs.x4 == rhs.x4 && lhs.y4 == rhs.y4;
}


vector<int> get_touching_rectangles(Rectangle base,
        vector<Rectangle> rectangles) {
    vector<Rectangle> ret;


    for (auto it = rectangles.begin(); it != rectangles.end(); it++) {
        Rectangle other = *it;
        if (base == other) {
            continue;
        }

        
        if ((other.x2 >= base.x1 && other.x1 <= base.x2) &&
                (other.y1 == base.y3 || other.y3 == base.y1)) {
            ret.push_back(other);
            continue;
        }

        
        if ((other.y3 >= base.y2 && other.y2 <= base.y3) &&
                (other.x1 == base.x3 || other.x3 == base.x1)) {
            ret.push_back(other);
            continue;
        }

    }

    return ret;
}

int main()
{
vector<Rectangle> rectangles;


ifstream inputFile;
inputFile.open("RectCoordinates.txt");


get_touching_rectangles(rectangles.at(2) /* Rectangle #3 */, rectangles);

 inputFile.close();
    return 0;
}



But there is somthing wrong. It shows the error-

Quote

terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check
Aborted


As far as I know it means, that my code could not get the input from the file.
How can I take the input from the file and check them using the "get_touching_rectangles" function?

This post has been edited by aries0152: 03 July 2013 - 03:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Read data from a text file and check them using a function

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: Read data from a text file and check them using a function

Posted 03 July 2013 - 07:46 AM

Quote

As far as I know it means, that my code could not get the input from the file.

No, actually it means your accessing your vector out of range.

Where do you actually read the file and populate the rectangles vector?

You should also be insuring that the file properly opened by checking the stream state.

You may want to read the following tutorial: File IO.


Jim
Was This Post Helpful? 0
  • +
  • -

#3 aries0152  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 13-September 09

Re: Read data from a text file and check them using a function

Posted 03 July 2013 - 08:11 AM

View Postjimblumberg, on 03 July 2013 - 07:46 AM, said:

Where do you actually read the file and populate the rectangles vector?


In my text file I have the values of x1,y1, x2,y2, x3,y3 and x4,y4 in columns. I want to use the values for the "bool operator" and "vector<int> get_touching_rectangles(Rectangle base,vector<Rectangle> rectangles)" functions.

I searched through internet but can not find a way to specify the values for the code. Can you please help?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: Read data from a text file and check them using a function

Posted 03 July 2013 - 08:21 AM

Where are you actually reading the file? I don't see anywhere in your code where you are actually trying to read your input file.

Did you read the link I posted?

Jim
Was This Post Helpful? 0
  • +
  • -

#5 aries0152  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 13-September 09

Re: Read data from a text file and check them using a function

Posted 03 July 2013 - 08:52 AM

View Postjimblumberg, on 03 July 2013 - 08:21 AM, said:

Did you read the link I posted?


Yes I did. Let me rewrite the main function -

int main(){

ifstream namefile(filename);
vector<Rectangle> rectangles;
string input;
while( namefile >> input)
        rectangles.push_back(input);
    fstream InFile;
InFile.open("RectCoordinates.txt", ios::in);

   if (InFile.fail())
      {
      cout << "Could not open RectCoordinates.txt" << endl;
      exit(1);
      }
            while (! InFile.fail())
      {
     get_touching_rectangles(rectangles.at(2) /* Rectangle #3 */, rectangles);
 }      return 0;
}


Now it gives me the error-

Quote

read_data.cpp: In function ‘int main()’:
read_data.cpp:70:35: error: no matching function for call to ‘std::vector<Rectangle>::push_back(std::string&)’
read_data.cpp:70:35: note: candidates are:
In file included from /usr/include/c++/4.7/vector:65:0,
from read_data.cpp:2:
/usr/include/c++/4.7/bits/stl_vector.h:881:7: note: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Rectangle; _Alloc = std::allocator<Rectangle>; std::vector<_Tp, _Alloc>::value_type = Rectangle]
/usr/include/c++/4.7/bits/stl_vector.h:881:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const value_type& {aka const Rectangle&}’
/usr/include/c++/4.7/bits/stl_vector.h:899:7: note: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = Rectangle; _Alloc = std::allocator<Rectangle>; std::vector<_Tp, _Alloc>::value_type = Rectangle]
/usr/include/c++/4.7/bits/stl_vector.h:899:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘std::vector<Rectangle>::value_type&& {aka Rectangle&&}’


What am I doing wrong?
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: Read data from a text file and check them using a function

Posted 03 July 2013 - 09:56 AM

Quote

What am I doing wrong?

Quite a few things. First input is a string not a Rectangle. You can't push_back() a string into a vector expecting a Rectangle.

Next why are you trying to open a file inside the loop?

What does this file contain?

Most importantly what value does filename contain? Where did you initialize this variable?

I suggest you re-read the link I provided and insure you try to compile and run some of the sample programs to insure you understand what is actually happening in those simple samples.


Jim
Was This Post Helpful? 0
  • +
  • -

#7 aries0152  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 13-September 09

Re: Read data from a text file and check them using a function

Posted 03 July 2013 - 10:07 AM

View Postjimblumberg, on 03 July 2013 - 09:56 AM, said:

Next why are you trying to open a file inside the loop?


I want to take the values of x1,y1,x2,y2.... and compare them with each other. There will be about 1000 values.

Quote

What does this file contain?


In my first post, I paste the data from my file. Please check.


Quote

Most importantly what value does filename contain? Where did you initialize this variable?

Yes, I used const char* filename = "RectCoordinates.txt"; at the beginning of the file

Quote

I suggest you re-read the link I provided and insure you try to compile and run some of the sample programs to insure you understand what is actually happening in those simple samples.


I already tried them on sample program. They run quite smoothly. Just having problem when I try to use it with my program. :(/>
Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,396
  • Joined: 19-February 09

Re: Read data from a text file and check them using a function

Posted 03 July 2013 - 05:34 PM

Hi, are your rectangles right-angled rectangles? If they are you are writing twice as much data as is required.

If you want to use an operator it is something like :

istream& operator>>(istream &in, const Rectangle &rect) 
{
  // get data from in and store in rect

  return in;
}


Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: Read data from a text file and check them using a function

Posted 03 July 2013 - 06:36 PM

Please post your complete current code.

Jim

This post has been edited by jimblumberg: 03 July 2013 - 06:37 PM

Was This Post Helpful? 0
  • +
  • -

#10 aries0152  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 13-September 09

Re: Read data from a text file and check them using a function

Posted 04 July 2013 - 09:22 AM

Here is the samples of data , for the unterstanding, I have mentioned which column refers what-
(Rectangle
Number)      (x1)     (y1)         (x2)       (y2)      (x3)        (y3)      (x4)       (y4)       Area
1          0.0000     0.0000     0.8147     0.0000     0.8147     0.1355     0.0000     0.1355     0.1104
2          0.8147     0.0000     1.0000     0.0000     1.0000     0.1355     0.8147     0.1355     0.0251
3          0.8147     0.1355     0.9058     0.1355     0.9058     0.8350     0.8147     0.8350     0.0637
4          0.0000     0.1355     0.1270     0.1355     0.1270     0.9689     0.0000     0.9689     0.1058
5          0.9058     0.1355     0.9134     0.1355     0.9134     0.2210     0.9058     0.2210     0.0006
6          0.9058     0.8350     1.0000     0.8350     1.0000     1.0000     0.9058     1.0000     0.0155


And here is the full updated code-

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

using namespace std;

const char* filename = "RectCoordinates.txt";


struct Rectangle {
//Need to grab the values of x1,y1,x2,y2,x3,y3,x4,y4  from the file
    double x1, y1;
    double x2, y2;
    double x3, y3;
    double x4, y4;
};

//Need to check if the values satisfy these condition
bool operator==(const Rectangle& lhs, const Rectangle& rhs) {
    return  lhs.x1 == rhs.x1 && lhs.y1 == rhs.y1 &&
            lhs.x2 == rhs.x2 && lhs.y2 == rhs.y2 &&
            lhs.x3 == rhs.x3 && lhs.y3 == rhs.y3 &&
            lhs.x4 == rhs.x4 && lhs.y4 == rhs.y4;
}



vector<Rectangle> get_touching_rectangles(Rectangle base,
        vector<Rectangle> rectangles) {
    vector<Rectangle> ret;



    for (auto it = rectangles.begin(); it != rectangles.end(); it++) {
        Rectangle other = *it;
        if (base == other) {
            continue;
        }

        
        if ((other.x2 >= base.x1 && other.x1 <= base.x2) &&
                (other.y1 == base.y3 || other.y3 == base.y1)) {
            ret.push_back(other);
            continue;
        }

        
        if ((other.y3 >= base.y2 && other.y2 <= base.y3) &&
                (other.x1 == base.x3 || other.x3 == base.x1)) {
            ret.push_back(other);
            continue;
        }

    }

    return ret; // return the numbers of rectangle if they match


}

int main(){

ifstream namefile(filename);
namefile.open("RectCoordinates.txt", ios::in);
vector<Rectangle> rectangles;
string input;

while( namefile >> input)
        rectangles.push_back(input);
    


    fstream InFile;
        



   if (namefile.fail())
      {
      cout << "Could not open RectCoordinates.txt" << endl;
      exit(1);
      }
      
      while (! namefile.fail())
      {


     get_touching_rectangles(rectangles.at(2) /* Rectangle #3 */, rectangles);
 
}
    

    return 0;
}






Here are my new code. I have added some comments in the code for describing what I want to do.
If I compile the code it still shows the error-

Quote

read_data.cpp: In function ‘int main()’:
read_data.cpp:71:35: error: no matching function for call to ‘std::vector<Rectangle>::push_back(std::string&)’


:(/>

This post has been edited by aries0152: 04 July 2013 - 09:25 AM

Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: Read data from a text file and check them using a function

Posted 04 July 2013 - 10:09 AM

Look at the following snippet and answer the following questions.

 
int main() 
{
   ifstream namefile(filename);
   namefile.open("RectCoordinates.txt", ios::in);
 
   vector<Rectangle> rectangles;
   string input;
 
   while( namefile >> input)
      rectangles.push_back(input);
 
   fstream InFile;
   
   if (namefile.fail())


1. Why are you trying to open RectCoordinates.txt twice? Right now your trying to open the same file twice, once in the constructor and once in the open(). Get rid of the open() call, just use the constructor.

2. Why are you trying to retrieve the information contained in your file into a string instead as numbers to be used to populate a Rectangle instance? This the source of your error message. You can't insert a string into your Rectangle vector.

3. What good does it do to test if the stream "failed" after you've already read the file? At this point the if you did properly read the file the stream should be in an error state, it reached eof().

4. Why doesn't your Rectangle store the area?

5. What is the purpose of the fstream InFile?


Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1