我写了一个简单的程序来抓住雅虎财经的股票价格。读取数据的循环提前截断(并停止网站数据显示的位置,而不是完整下载到excell文件的正确日期)。所以我在循环中加入了一个cout命令来尝试调试,并且它正确地工作!Cout修复了C++程序中的错误,但是为什么?
那么为什么使用cout函数改变程序功能呢?有任何想法吗?以下是代码。 (我发现了两个相关的职位,但仍然无法数字出来,比如“可以清点以某种方式改变变量?”和“C++程序奇怪的错误:删除打印输出打破计划”)
#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <windows.h>
#include <wininet.h>
using namespace std;
int main()
{
HINTERNET hOpen, hURL;
LPCWSTR NameProgram = L"Webreader"; // LPCWSTR == Long Pointer to Const Wide String
LPCWSTR Website;
char file[101];
int i;
string filename;
unsigned long read;
filename = "data.txt";
ofstream myFile(filename);
if (! myFile)
{
cout < "Error opening file\n";
}
if (!(hOpen = InternetOpen(NameProgram, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0)))
{
cerr << "Error in opening internet" << endl;
return 0;
}
Website = L"http://ichart.finance.yahoo.com/table.csv?s=MSFT&a=00&b=1&c=2009&d=09&e=22&f=2010&g=d&ignore=.csv";
hURL = InternetOpenUrl(hOpen, Website, NULL, 0, 0, 0); //Need to open the URL
InternetReadFile(hURL, file, 100, &read);
file[read] = '\0';
myFile << file;
while (read == 100)
{
InternetReadFile(hURL, file, 100, &read);
file[read] = '\0';
myFile << file;
cout << file; //If I take this line out, the function terminates early.
}
myFile << file;
InternetCloseHandle(hURL);
myFile.close();
return 0;
}
你确定这正是你的代码? 'cout <“Error ...”'看起来很可疑... – 2010-10-22 02:49:50
你是对的,这是一个错误,但它在我的程序中(我不知道它为什么编译),所以我只是将它改为<<并仍然得到同样的问题,cout解决了另一个问题。 (感谢您注意!) – 2010-10-22 02:53:57
编码提示:您可以通过使用do-while而不是常规while来将初始读取合并到循环中。 – chrisaycock 2010-10-22 02:58:35