0

I am trying to read (x,y) floating point values from a txt file in C++. The numbers are separated by a space. The ith number and the i+1th number make the (x,y) coordinates. So index positions 0 and 1 would be the first (x,y) pair and index positions (1,2) would be the next (x,y) pair.

This is what I have done but I am not sure how I can save them as floats.

ifstream randomFile;
string content;
randomFile.open("random.txt");
if(randomFile.is_open()) {
    while(getline(randomFile,content)){
        randomFile >> content;
    }
    randomFile.close();
}
cafce25
  • 15,907
  • 4
  • 25
  • 31

2 Answers2

0

Read the first float x
While additional read y succeeds:
  Add (x, y) to your list
  x = y

#include <iostream>
#include <vector>

struct xy
{
  float x, y;
  xy( float x, float y ) : x{x}, y{y} { }
};

auto read_xy_pairs( std::istream & ins )
{
  std::vector<xy> xys;
  float x, y;
  ins >> x;
  while (ins >> y)
  {
    xys.emplace_back( x, y );
    x = y;
  }
  return xys;
}

#include <sstream>
#include <string>

int main()
{
  std::cout << "list of numbers? ";
  std::string s;
  getline( std::cin, s );
  std::istringstream numbers( s );
  
  for (auto [x, y] : read_xy_pairs( numbers )) 
    std::cout << "(" << x << ", " << y << ")\n";
}

Example:

list of numbers? 1 2 3 4 5
(1, 2)
(2, 3)
(3, 4)
(4, 5)
Dúthomhas
  • 8,200
  • 2
  • 17
  • 39
0

An extra variable (prev) can be used to store the value of last input and append (prev,curr) on every iteration to the storage container. In the following code, I have used vector of pairs of float to store the pairs, but you may use arrays or structures as well.

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

int main() {
    //Declaring vector of float pairs
    vector <pair<float, float>> floatPairs;
    ifstream randomFile;
    float curr, prev;

    randomFile.open("a.txt");
    randomFile >> curr;
    while (!randomFile.eof()) {
        prev = curr;
        randomFile >> curr;

        //Appending to vector of float pairs
        floatPairs.push_back({ prev,curr });
    }

    //Printing
    for (auto i : floatPairs) {
        cout << "(" << i.first << ", " << i.second << ")\n";
    }
}

Input file content: 12.5 56.8 34.7 75.7 23.4 86.7 34.9 66.8

Output:

(12.5, 56.8)
(56.8, 34.7)
(34.7, 75.7)
(75.7, 23.4)
(23.4, 86.7)
(86.7, 34.9)
(34.9, 66.8)
Mislah
  • 318
  • 1
  • 2
  • 11
  • You are [looping on EOF](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). Add a newline to the end of your input file and you’ll see what happens. – Dúthomhas Jan 27 '23 at 05:28
  • @Dúthomhas Dude, Things are based on assumptions, Here I assume all inputs are separated by whitespaces, All inputs are floating point numbers, At least a pair of values are there in the file,.... And NO, adding a newline doesn't have any bad effects. The `>>` operator skips preceding whitespaces (including newlines). – Mislah Jan 27 '23 at 06:18
  • No, your code is wrong, and I both linked why and gave you a valid test case where you can see it fail for yourself. Don’t get defensive when someone tries to help. – Dúthomhas Jan 27 '23 at 06:23
  • This is an anti patttern `while (!randomFile.eof()) {`. Don't do that. `eof()` can be false and there is no data to read (as it does not go true until you read past the end of file. In this situation the next read will fail. – Martin York Jan 27 '23 at 12:21