I'm using C++ BUILDER 6 and I'm trying to understand this code.
I'm logging 4 variables into a txt file and obtaining success:
sprintf(buffer, "%i,%i,%i,%i", x, yMin, yMed, yMax);
ofstream logData;
logData.open("logData.txt", ios::app);
logData << buffer << "\n";
My txt output:
1,1686,1784,1972
2,1687,1785,1973
3,1688,1786,1974
...
I need to read this file to get variables back and plot to a graph. I'm doing like this and it's working properly:
std::ifstream infile(OpenDialog->FileName.c_str());
int num, y, yMin, yMax;
char a,b,c;
char buffer[100];
if(infile.is_open())
{
while (infile >> x >> a >> yMin >> b >> yMed >> c >> yMax)
{
sprintf(buffer, "X: %i | YMIN: %i | YMED: %i | YMAX: %i", x, yMin, yMed, YMax);
Memo1->Lines->Add(buffer);
}
}
My Memo1 output:
X:1 | YMIN: 1686 | YMED: 1784 | YMAX: 1972
X:2 | YMIN: 1687 | YMED: 1785 | YMAX: 1973
X:3 | YMIN: 1688 | YMED: 1786 | YMAX: 1974
...
My question is: How did C++ distinguish the variables? I didn't get where it assumes that x = 1
, yMin = 1686
... by the while loop reading line by line of txt file:
while (infile >> x >> a >> yMin >> b >> yMed >> c >> yMax)
When the code enters to sprinft line:
sprintf(buffer, "X: %i | YMIN: %i | YMED: %i | YMAX: %i", x, yMin, yMed, YMax);
The variables x, yMin, yMed, YMax already assumed some value from txt file.
Note: In my understood C++ expects some INT + CHAR + INT + CHAR... Where the CHAR is a ','. But '1' is a CHAR too and if I log:
1116861178411972 instead 1,1686,1784,1972
It doesn't works properly.
Can someone give me some guide? Somewhere to start to understand it?