file reading in C++ -


could me figure out c++ code got somewhere want understand it. have figured out of things cannot head around. here is:

vector<double> prices;            // vector of option prices vector<int> strikes;                // vector of strikes char buffer[100];                   // buffer line read char databuffer[100];         // stores current data string read char *str = null;                   // pointer data string const char *file = "optiondata.txt"; // file option chain info ifstream fin;                        // input file stream  fin.clear(); fin.open(file); if (fin.good()) {     while (!fin.eof()){         // read in 1 line @ time         fin.getline(buffer,sizeof(buffer)/sizeof(buffer[0]));         ifstream str1(buffer);          // data         str1 >> databuffer; // read data file         while (!str1.eof()){             // read in contract maturity, strike, , price             str1 >> databuffer;       // read option maturity month             str1 >> databuffer;       // read option maturity year              str1 >> databuffer;       / read option maturity strike             // convert strike char* data integers             // , add strike vector             strikes.push_back(atoi(databuffer));              str1 >> databuffer;       // read option market price             // convert option price char* data floats             // , add strike vector             prices.push_back(atof(databuffer));         }          buffer[strlen(buffer) + 1] = '\0';     } } else {     cout << "file not good!" << "\n"; } // close file fin.close(); 

what dont following

  1. ifstream str1(buffer);
  2. fin.getline(buffer,sizeof(buffer)/sizeof(buffer[0]));
    particularly sizeof(buffer)/sizeof(buffer[0])
  3. buffer[strlen(buffer) + 1] = '\0';
  4. str1 >> databuffer;

the file being read "optiondata.txt" , sample of 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 patient me teaching myself c++. ran freezes machine.

no wonder machine got frozen , inner lop won't stop.

anyway, made few changes, didn't want change whole code make easy understand.

vector<double> prices;            // vector of option prices vector<int> strikes;                // vector of strikes string buffer;                   // buffer line read string databuffer;         // stores current data string read char *str = null;                   // pointer data string const char *file = "./optiondata.txt"; // file option chain info ifstream fin;                        // input file stream  fin.clear(); fin.open(file); if (fin.good()) {     while (!fin.eof()){         // read in 1 line @ time   getline(fin,buffer);         stringstream str1(buffer);         // data         //str1 >> databuffer; // read data file         while (str1 >> databuffer){             // read in contract maturity, strike, , price             // read option maturity month             str1 >> databuffer;       // read option maturity year             str1 >> databuffer;       // read option maturity strike             // convert strike char* data integers             // , add strike vector    strikes.push_back(atoi(databuffer.c_str()));             str1 >> databuffer;       // read option market price             // convert option price char* data floats             // , add strike vector    prices.push_back(atof(databuffer.c_str()));         }        // buffer[strlen(buffer) + 1] = '\0';     } } else {     cout << "file not good!" << "\n"; } // close file fin.close(); 

i changed databuffer , buffer string rather char[]. it's easy manage.

the major change str1. changed ifstream stringstream, since have data in memory.

regarding questions , think others answered


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -