大家好,我正尝试从C++中的文件中读取数据。我是新来的C++,但从文件测试程序实践一个简单的读取。我的程序编译,但是当我从一个文件读取时,它会在屏幕上显示一堆垃圾。以下是我的代码:需要帮助从文件读取。 C++
#include <iostream>
#include <fstream>
int main()
{
ifstream infile;
char File[20];
cout<<"Please enter file name: "<<endl;
cin>>File;
string sym;
double o_price, h_price, l_pprice, c_pprice, c_price;
int o_shares;
infile.open(File);
if(!infile.is_open())
{
cout<<"The file cannot be open"<<endl;
}
while(!infile.eof())
{
infile>>sym>>o_price>>c_price>>h_price>>l_pprice>>c_pprice>>o_shares;
cout<<sym<<o_price<<c_price<<h_price<<l_pprice<<c_pprice<<o_shares;
}
infile.close();
return 0;
}
请我需要帮助,因为我想了解这个概念。 对于缺少的语法感到抱歉。 输入文件包含: ABC 120.09 123.11 134.45 124.34 36.67 10000
下面是在屏幕上显示的输出:! P =环R {[CONTENT_TYPES] .xml000000e?。
你错过了一半的代码,在这里。什么是“sym”,“o_price”等?你也没有显示文件中的内容。 –
此代码不会按给定的方式编译。您需要发布完整且完整的源代码。 –
对不起,我确实声明这些变量只是忘记将它们添加到代码中。但我编译,它仍然在屏幕上显示垃圾。 – Emy