我正在尝试使用历史股票价格分析股票数据。可以从Yahoo Finance(https://finance.yahoo.com/quote/AAPL/history?p=AAPL)作为CSV文件下载此数据。但是,当我下载文件时,它被组织为一行,并且在每一行的末尾没有用新行分隔。我已经读到这是因为不同的OS使用不同的字符来表示新行。在下载文件或在文本编辑器中时,有什么方法可以解决此问题,还是应该修改代码以适应下载文件的格式?我正在Windows 10上使用C ++进行编码,并在NotePad中查看文件。 (对于StackOverFlow还是我的新手,因此感谢您提出问题的建议。)
例:
代码应该如何
2017-10-16,157.899994,160.000000,157.649994,159.880005,159.307312,24121500
2017-10-17,159.779999,160.869995,159.229996,160.470001,159.895203,18997300
2017-10-18,160.419998,160.710007,159.600006,159.759995,159.187729,16374200
2017-10-19,156.750000,157.080002,155.020004,155.979996,155.421280,42584200
2017-10-20,156.610001,157.750000,155.960007,156.250000,155.690308,23974100
如何下载
2017-10-16,157.899994,160.000000,157.649994,159.880005,159.307312,241215002017-10-17,159.779999,160.869995,159.229996,160.470001,159.895203,189973002017-10-18,160.419998,160.710007,159.600006,159.759995,159.187729,163742000002 155.020004,155.979996,155.421280,425842002017-10-20,156.610001,157.750000,155.960007,156.250000,155.690308,23974100
我的代码要读入文件
void Stock::readData(filename){
ifstream stream;
stream.open(fileName);
if(stream.fail()){
cout << "The file " << fileName <<" failed to open." << endl;
return;
}
string line ="";
cout << line << endl;
while(!stream.eof()){
// cout << '*' << endl;
getline(stream,line);
//cout << line << endl;
string parsed[7];
int num = Spliter(line, ',', parsed, 7);
string tempDate;
float tempOpen;
float tempHigh;
float tempLow;
float tempClose;
float tempCloseAdj;
float tempVolume;
/*for(int i=0; i<num; i++){
cout << parsed[i] << endl;
}*/
tempDate = parsed[0];
tempOpen = stof(parsed[1]);
tempHigh = stof(parsed[2]);
tempLow = stof(parsed[3]);
tempClose = stof(parsed[4]);
tempCloseAdj = stof(parsed[5]);
tempVolume = stof(parsed[6]);
/*
cout <<(tempDate) << " ";
cout << (tempOpen)<< " ";
cout << (tempHigh)<< " ";
cout << (tempLow)<< " ";
cout << (tempClose)<< " ";
cout << tempCloseAdj<< " ";
cout << (tempVolume) << endl;
*/
vector<float> v;
dates.push_back(tempDate);
data.push_back(v);
data[numberOfDays].push_back(tempOpen);
data[numberOfDays].push_back(tempHigh);
data[numberOfDays].push_back(tempLow);
data[numberOfDays].push_back(tempClose);
data[numberOfDays].push_back(tempCloseAdj);
data[numberOfDays].push_back(tempVolume);
// cout << '#' << endl;
numberOfDays++;
}
stream.close();
}
最佳答案
我解决了这个问题。为了在Mac,Windoxs或Unix等平台之间转换EOL行尾,请下载文本编辑器,例如Notepad ++。在notepadd ++中打开文件,然后转到“编辑”选项卡。找到“ EOL COnversion”按钮并将其用于转换文件。