Reputation: 45
Is there a way thorough which I can see what character occupies a certain coordinate. Let's say I have the following output:
Hello World !
I want to able to to see the x character on the y line. Something like:
readCoordinates(0,3);
This function should return 'l' , because the 4th character (because I started counting from 0) from the 1st line is 'l'. Can I do this type of readings in C++ from an already printed string ?
Upvotes: 3
Views: 226
Reputation: 42103
"Can I do this type of readings in C++ from an already printed string ?"
Good approach would be to store output of your program in memory so that your function readCoordinates
could access random character in O(1). I would definitely use std::vector<std::string> outputBuffer
which would allow you something like this: outputBuffer[0][3]
.
Example:
#include <iostream>
#include <vector>
#include <string>
std::vector<std::string> outputBuffer;
char readCoordinates(int line, int character)
{
if (line < outputBuffer.size() && character < outputBuffer[line].size())
return outputBuffer[line][character];
return 0;
}
int main()
{
std::string myOutput("Hello World !");
outputBuffer.push_back(myOutput);
std::cout << myOutput << std::endl;
if (char c = readCoordinates(0, 3))
std::cout << c << std::endl;
}
output:
Hello World !
l
Upvotes: 0
Reputation: 10184
You could do this from the string that is printed, but after it is printed it is just pixels on a screen, or ink on a printer. So, the answer is no, unless you have e.g. a vision system to look around and see where it was printed, recognize the letters and lines, and figure out the coordinates.
Upvotes: 0
Reputation:
You might consider defining your own custom print function, which logs in a vector
everything you've already outputted to the console. That way, you could easily grab a character, or string, from the console.
It might take up a lot of memory, however, if you're outputting thousands of lines.
Upvotes: 1