有人可以帮助我弄清楚我将此C ++代码弄到什么地方,但想了解它的意思。我已经弄清楚了其中的一些,但是有些事情我无法理解。这里是:
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();
我没有得到的是以下
ifstream str1(buffer);
fin.getline(buffer,sizeof(buffer)/sizeof(buffer[0]));
特别是
sizeof(buffer)/sizeof(buffer[0])
buffer[strlen(buffer) + 1] = '\0';
str1 >> dataBuffer;
读取的文件是“ optionData.txt”,其示例是:
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
请耐心,我正在自学c ++。我运行了它,但是它冻结了我的机器。
最佳答案
难怪您的机器被冻结了,内部活门不会停止。
无论如何,我做了一些更改,但我不想更改整个代码以使您易于理解。
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();
我将dataBuffer和buffer更改为字符串而不是char []。很容易管理。
主要变化是str1。我将它从ifstream更改为stringstream,因为您已经在内存中存储了数据。
关于这些问题,我认为其他人对你的回答很好
关于c++ - 用C++读取文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3886325/