0

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

  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 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.

5
  • 2
    If you're teaching yourself C++, be sure to get yourself a good beginner's C++ book. The lines of C++ code that you don't get are pretty basic. Commented Oct 7, 2010 at 22:29
  • maybe i should rephrase and say I dont see the need for that I have seen simpler examples than this in the books Commented Oct 7, 2010 at 22:34
  • 1
    The do as the simple examples do and stop calling eof() all the time. ;-) Rather check the boolean value of your stream shift operators. E.g. if (str1 >> dataBuffer) ... Commented Oct 7, 2010 at 22:41
  • 2
    I've been writing C++ for many years and I still don't get this line: buffer[strlen(buffer) + 1] = '\0' Commented Oct 7, 2010 at 22:44
  • 1
    @SoapBox: IMO it makes no sense, since, if strlen is used, the buffer must already be NUL-terminated. Commented Oct 7, 2010 at 22:48

3 Answers 3

2
  1. Declares an input stream, opening the file specified in buffer.
  2. It's a standard method to get the number of elements of an array declared on the stack; it takes the whole size of the array (in chars) and divide it by the size of each element; in this particular case it is useless since sizeof(char)==1 always, but it can be useful in future it the project is turned to wchar_ts.
  3. It's not clear to me. It seems that the author wanted to NUL-terminate the string, but strlen needs an already terminated string to work, so that thing seems pointless.
  4. Reads the next field in the specified var, using the correct overload of operator>>.

Since the questions you're asking are pretty basic, I suggest you to grab a basic C++ book and read it; it's quite easy to get bad habits in C++, especially if you learned it "badly" and without always understanding what's going on.

By the way, on this line:

 str1 >> dataBuffer; / read option maturity strike 

there's a slash missing, the compiler will complain.

Moreover, iterating on a stream checking just EOF is not a nice thing, you should also check for other stream errors, otherwise you may get caught in an infinite loop (which I think it's what's happening here).


Edit: uh, now I understand what's going on; the author of the code wanted to use a stringstream, but instead used an ifstream, which is not good at all for what he wants to do (especially since he tries to open a file named as the whole row just read from file). By the way, I don't think using a stringstream is useful here, why not just read from the original file?

Sign up to request clarification or add additional context in comments.

1 Comment

... and check outside which error flags are set: if eof is on, fine, if instead bad or fail are on (without eof) then you have to report an error.
1

No wonder your machine got frozen , the inner lop won't stop.

anyway, I made a few changes, I didn't want to change the whole code to make it easy for you to understand.

vector<double> prices; // vector of option prices vector<int> strikes; // vector of strikes string buffer; // buffer for line read string dataBuffer; // 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 getline(fin,buffer); stringstream str1(buffer); // Get data //str1 >> dataBuffer; // read data from file while (str1 >> dataBuffer){ // read in contract maturity, strike, and price // 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.c_str())); str1 >> dataBuffer; // read option market price // convert option price char* data to floats // and add to 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 and buffer into string rather than char[]. it's easy to manage.

The major change was str1. I changed it from ifstream into stringstream, since you already have the data in memory.

regarding the questions , I think the others answered you very well

2 Comments

should !str1.eof() not check for end of line and not end of file?
I have tried your solution on the file i posted, but the last values are not in the data file.
1

It's been a long time since I did C++, but here it goes:

  1. Declares an I/O stream with the allocated buffer.
  2. The division takes the size (in bytes) of the whole array and divides it by the size of an element - gives the number of elements.
  3. Null terminate the string in buffer[]. Add a 0 byte at the end of the character string. This is the C standard string notation. However, strlen() expects a null terminated string already in buffer, so I don't quite see what this line contributes.
  4. Reading from the I/O stream and directing the input to the allocated buffer. The >> operator is overloaded for the ifstream class.

5 Comments

why does it freeze my machine?
@Vaolter: try stepping through it with a debugger and see where exactly if fails.
the 1 is either wrong or at least phrased in an unclear manner; buffer contains the name of the file to be opened.
@Matteo: You are right. I misinterpreted the usage of buffer.
me too :) now that I saw @bander's answer, it's clear that the author of the code there wanted to use a stringstream, so in the current code buffer is used as the name of the file to open, but it was intended to be the data to manipulate. Big mess, indeed. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.