开发者

file reading in C++

开发者 https://www.devze.com 2023-01-19 07:51 出处:网络
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:

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.


  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?


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


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

精彩评论

暂无评论...
验证码 换一张
取 消