关于函数参数中有指针无法带出地址
2016-12-27 20:16
253 查看
本人菜鸟一只,写了一个读入全部文件的函数,接口一直有问题,代码如下:
int ReadAllFile(wstring sfilepath, WCHAR*sfile,int mod)
{
wifstream file(sfilepath.c_str(), std::wifstream::binary);
WCHAR*szencr;
if (file)
{
// Calculate the file's size, and allocate a buffer of that size.
file.seekg(0, file.end);
const int file_size = file.tellg();
if (file_size > (1024 * 10000))
{
file.close();
//delete szencr;
return -1;
}
// Read the entire file into the buffer.
file.seekg(0, file.beg);
szencr = new WCHAR[file_size + 1];
memset(szencr, 0, file_size + 1);
//szencr = file.rdbuf();
file.read(szencr, file_size);
//file.getline(szencr, file_size);
file.close();
sfile = szencr;//希望传出地址
return 0;
}
else
{
return -1;
}
}
但是这样出来的sfile 一直为null,一直以为sfile 就是个地址,我传的地址进去,就应该能带出地址,实际上是不行的,应该将参数改成如下:
int CSkfControl::ReadAllFile(wstring sfilepath, WCHAR*&sfile,int mod)//引用传递,不是引用的话,值传递,一直不知道指针也是可以引用传递的。。。。。太菜了
{
wifstream file(sfilepath.c_str(), std::wifstream::binary);
WCHAR*szencr;
if (file)
{
// Calculate the file's size, and allocate a buffer of that size.
file.seekg(0, file.end);
const int file_size = file.tellg();
if (file_size > (1024 * 10000))
{
file.close();
//delete szencr;
return -1;
}
// Read the entire file into the buffer.
file.seekg(0, file.beg);
szencr = new WCHAR[file_size + 1];
memset(szencr, 0, file_size + 1);
//szencr = file.rdbuf();
file.read(szencr, file_size);
//file.getline(szencr, file_size);
file.close();
sfile = szencr;
return 0;
}
else
{
return -1;
}
}
int ReadAllFile(wstring sfilepath, WCHAR*sfile,int mod)
{
wifstream file(sfilepath.c_str(), std::wifstream::binary);
WCHAR*szencr;
if (file)
{
// Calculate the file's size, and allocate a buffer of that size.
file.seekg(0, file.end);
const int file_size = file.tellg();
if (file_size > (1024 * 10000))
{
file.close();
//delete szencr;
return -1;
}
// Read the entire file into the buffer.
file.seekg(0, file.beg);
szencr = new WCHAR[file_size + 1];
memset(szencr, 0, file_size + 1);
//szencr = file.rdbuf();
file.read(szencr, file_size);
//file.getline(szencr, file_size);
file.close();
sfile = szencr;//希望传出地址
return 0;
}
else
{
return -1;
}
}
但是这样出来的sfile 一直为null,一直以为sfile 就是个地址,我传的地址进去,就应该能带出地址,实际上是不行的,应该将参数改成如下:
int CSkfControl::ReadAllFile(wstring sfilepath, WCHAR*&sfile,int mod)//引用传递,不是引用的话,值传递,一直不知道指针也是可以引用传递的。。。。。太菜了
{
wifstream file(sfilepath.c_str(), std::wifstream::binary);
WCHAR*szencr;
if (file)
{
// Calculate the file's size, and allocate a buffer of that size.
file.seekg(0, file.end);
const int file_size = file.tellg();
if (file_size > (1024 * 10000))
{
file.close();
//delete szencr;
return -1;
}
// Read the entire file into the buffer.
file.seekg(0, file.beg);
szencr = new WCHAR[file_size + 1];
memset(szencr, 0, file_size + 1);
//szencr = file.rdbuf();
file.read(szencr, file_size);
//file.getline(szencr, file_size);
file.close();
sfile = szencr;
return 0;
}
else
{
return -1;
}
}
相关文章推荐
- 关于函数的指针参数
- 关于qsort第四个参数,函数指针作参数的一点思考
- [C++]关于指针作为函数参数传递
- 关于将函数的地址作为参数传递给另一个函数调用
- C函数关于qsort最后一个函数指针参数指向的函数的返回值和升序排序的关系
- 2010.8.16 关于函数参数是指针、还是引用的区别
- 关于指针当做函数参数
- 结构体指针作为函数参数传递却无法传值的有关问题
- 函数参数为指针,在函数内没有必要改变指针地址,改了也不起作用,因为即便是指针,也是值传递指针!
- 关于结构指针类型数组做函数参数的一个问题#include<stdio.h>typede
- 彻底搞定C指针-——第五篇:函数参数的传递(值传递、地址传递、引用传递)
- 关于函数的指针参数
- 关于C中函数参数,指针-常量-引用解释的4个函数例子
- 文件操作函数fseek设置文件指针的位置,第三个参数是起始地址,有0,1,2三个值,分别指首地址,当前地址,文件尾地址EOF;第二个参数是偏移地址正为右,负为左
- 关于指针作为函数参数传递的理解,对比普通变量作为函数参数的需注意点,其实就是行参和实参的问题。
- c++函数中的 指针参数跟地址参数区别
- 关于二级指针的使用测试小例子,仅供测试--参数传递的时候用指针最好,因为压栈的时候指针,只是压入一个地址的值,最多4个字节(32位机上),提供的3个测试例子,基于VC6.0的环境。
- C/C++中关于地址、指针和引用变量的学习笔记(四) : 函数
- 再开一篇关于C++程序设计基核心之一:引用,指针,符号优先级,函数参数传递
- Q13.3 关于虚表指针、虚函数表、虚函数表中虚函数地址