Could someone help me figure out what this C++ code does I got it somewhere but want to understand it. I have figured out some of it but some things I cannot get my head around. Here it is:
vector<double> prices; // vector of option prices vector<int> strikes; // vector of strikes char buffer[100]; // buffer for line read char dataBuffer[100]; // stores current data string read char *str = NULL; // pointer to data string const char *file = "optionData.txt"; // file with option chain info ifstream fin; // input file stream fin.clear(); fin.open(file); if (fin.good()) { while (!fin.eof()){ // read in one line at a time fin.getline(buffer,sizeof(buffer)/sizeof(buffer[0])); ifstream str1(buffer); // Get data str1 >> dataBuffer; // read data from file while (!str1.eof()){ // read in contract maturity, strike, and price str1 >> dataBuffer; // read option maturity month str1 >> dataBuffer; // read option maturity year str1 >> dataBuffer; / read option maturity strike // convert strike char* data to integers // and add to strike vector strikes.push_back(atoi(dataBuffer)); str1 >> dataBuffer; // read option market price // convert option price char* data to floats // and add to strike vector prices.push_back(atof(dataBuffer)); } buffer[strlen(buffer) + 1] = '\0'; } } else { cout << "File not good!" << "\n"; } // close file fin.close(); What i dont get is the following
ifstream str1(buffer);fin.getline(buffer,sizeof(buffer)/sizeof(buffer[0]));
particularlysizeof(buffer)/sizeof(buffer[0])buffer[strlen(buffer) + 1] = '\0';str1 >> dataBuffer;
The file being read is "optionData.txt" and a sample of it is:
Jan 03 35.00 40.50 Jan 03 95.00 0.30 Jan 03 40.00 25.30 Jan 03 100.00 0.20 Jan 03 45.00 29.50 Jan 03 105.00 0.05 Jan 03 50.00 16.80 Jan 03 110.00 0.10 Jan 03 55.00 12.60 Jan 03 115.00 0.15 Jan 03 60.00 9.30 Jan 03 120.00 0.15 Jan 03 65.00 6.40 Jan 03 125.00 0.10 Jan 03 70.00 4.10 Jan 03 130.00 0.10 Jan 03 75.00 2.60 Jan 03 140.00 0.10 Jan 03 80.00 1.50 Jan 03 150.00 0.05 Jan 03 85.00 0.90 Jan 03 155.00 0.00 Jan 03 90.00 0.50 Jan 03 160.00 0.05 Please be patient with me I am teaching myself c++. I ran it but it freezes my machine.
if (str1 >> dataBuffer)...strlenis used, the buffer must already beNUL-terminated.