C++ 下标需要数组或指针错误
我知道我犯了什么错误,但不知道如何解决 我正在写一个winAPI,它计算一个givien文件中找到了多少个“a”字符。 我仍然收到错误“下标需要数组或指针”(请在代码中找到注释)C++ 下标需要数组或指针错误,c++,C++,我知道我犯了什么错误,但不知道如何解决 我正在写一个winAPI,它计算一个givien文件中找到了多少个“a”字符。 我仍然收到错误“下标需要数组或指针”(请在代码中找到注释) #包括“stdafx.h” #包括 int _tmain(int argc,_TCHAR*argv[] { WCHAR str=L'a'; 处理一个问题; TCHAR*fn; fn=L“d:\\test.txt”; A=CreateFile(fn,泛型读取,0,空,打开现有,文件属性正常,空); if(A==无效的\u
#包括“stdafx.h”
#包括
int _tmain(int argc,_TCHAR*argv[]
{
WCHAR str=L'a';
处理一个问题;
TCHAR*fn;
fn=L“d:\\test.txt”;
A=CreateFile(fn,泛型读取,0,空,打开现有,文件属性正常,空);
if(A==无效的\u句柄\u值)
{
_tprintf(L“无法打开文件\n”);
}
其他的
{
德沃德真的;
整数字母;
整数字符串大小;
做
{
字节x[1024];
ReadFile(A、x、1024,实际上为空);
stringsize=sizeof(真的);
对于(int i=0;iReadFile()
将数据放入第二个参数中,在您的示例中是x
在您的代码中,实际上是读取的字节数。它只是一个数字。您不能在普通数字上加下标,因此会显示错误消息
所以改变
if(really[i]==str)
到
但你会遇到另一个问题:
DWORD really;
int countletter;
int stringsize;
do {
BYTE x[1024];
bool bResult = ReadFile(A, x, 1024, &really, NULL); // must save the result
if (bResult)
break; // Error!
// stringsize = sizeof(really); // <== NO NO NO will always return 8
stringsize = really; // this is what you need
for (int i = 0; i < stringsize; i++)
{
if(x[i] == str)
countletter++;
}
} while (really == 1024);
dwordreally;
整数字母;
整数字符串大小;
做{
字节x[1024];
bool-bResult=ReadFile(A,x,1024,&really,NULL);//必须保存结果
if(bResult)
break;//错误!
//stringsize=sizeof(really);//您的变量really
既不是数组,也不是指针,因此订阅它(really[i]
)我喜欢你的解释方式,但是先生,我一直在使用DO,我用X输入缓冲来压缩,它显示正确的答案,你能解释为什么如果我继续使用,同时会考虑一个问题,再次感谢你的帮助。
if (x[i] == str)
DWORD really;
int countletter;
int stringsize;
do {
BYTE x[1024];
bool bResult = ReadFile(A, x, 1024, &really, NULL); // must save the result
if (bResult)
break; // Error!
// stringsize = sizeof(really); // <== NO NO NO will always return 8
stringsize = really; // this is what you need
for (int i = 0; i < stringsize; i++)
{
if(x[i] == str)
countletter++;
}
} while (really == 1024);