从文件读取字符到数组,显示从数组中读取无效数据 C6385

int main()
{
	fstream file;
	file.open("text.txt", ios::in);
	if (!file)
	{
		cout << "File Open Error!";
		exit(false);
	}
	int i=0;
	char temp;
	while (!file.eof())//测定字符长度
	{
		file >> temp;
		i++;
	}
	file.close();
	char* str = (char*)malloc(sizeof(char) * i);//动态开辟数组空间
	file.open("text.txt", ios::in);
	if (!file)
	{
		cout << "File Open Error!";
		exit(false);
	}
	//file.seekg(0L, ios::beg);
	i = 0;
	while (!file.eof())
	{
		file>>str[i];
		i++;
	}
	cout<<"data在text中出现了:"<<Index(str, "data")<<"次";//调用检索函数,输出data出现的次数
	file.close();
	free(str);
	return 0;
}

file>>str[i] 这一行显示警告C6385,应该如何更改?

关于C6385读取的数据无效&C6386缓冲区溢出报错_u010180491的博客-CSDN博客_c语言c6385错误怎么改