Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 函数通过C+;中的fstream写入文件+;_C++_Fstream - Fatal编程技术网

C++ 函数通过C+;中的fstream写入文件+;

C++ 函数通过C+;中的fstream写入文件+;,c++,fstream,C++,Fstream,我创建一个具有指定名称的文件: #include <fstream> SYSTEMTIME systime; GetSystemTime (&systime); CString filename; filename = specifyNameOfFile(timestamp, suffix); // call a method std::ofstream fstream(filename, std::ios_base::app | std::ips_ba

我创建一个具有指定名称的文件:

#include <fstream>

SYSTEMTIME systime;
GetSystemTime (&systime);

CString filename;
filename =     specifyNameOfFile(timestamp, suffix); // call a method

std::ofstream fstream(filename,     std::ios_base::app | std::ips_base::out);
VC\include\fstream(803)处出现
错误C2248
:无法访问在类“std::basic\u ios”中声明的私有成员

有人能提出一个解决方案来说明我不懂怎么做吗?

我不能添加评论(声誉…),公平地说,我不太明白您想要什么,以及您的问题是什么,所以只需对您共享的代码发表一些评论(希望这也能有所帮助):

(一)

(无论如何,编译器都会处理此问题,但仍然)

2) 这里有一个输入错误: 指定文件 我想这应该是 指定名称文件

3) 在您的职能部门签名中:

void WriteToFile(unsigned int count, WORD hour, WORD minute, unsigned char result);
void MyClass::WriteToFile(unsigned char result)
{
result = 1;
}
“结果”不是通过引用获取的。我猜如果编写成功的话,您希望它能为调用方提供一些信息(为什么不使用bool WriteToFile?)。这样,无论您在函数中设置了什么“result”,它都只会影响您的函数,调用者将得到它给出的结果。 即: 假设这是您的函数:

void WriteToFile(unsigned int count, WORD hour, WORD minute, unsigned char result);
void MyClass::WriteToFile(unsigned char result)
{
result = 1;
}
调用者这样称呼它:

unsigned char writeResult = 0;
WriteToFile(writeResult)

if (writeResult == 1) ...
void WriteToFile(unsigned int count, WORD hour, WORD minute, unsigned char &result);
writeResult将保持为0

如果要更改,请将其作为引用传递,如下所示:

unsigned char writeResult = 0;
WriteToFile(writeResult)

if (writeResult == 1) ...
void WriteToFile(unsigned int count, WORD hour, WORD minute, unsigned char &result);

另外,对每个不想更改的参数使用“const”。

您说函数声明为

void WriteToFile(std::ofstream fstream, unsigned int count, WORD hour, WORD minute, unsigned char result);
这有一个问题,因为您试图通过值传递流,这意味着它会被复制。并且不能复制流对象

请改为通过引用传递:


另一方面,为了使其与其他流更加兼容,我建议您改用基类
std::ostream

void WriteToFile(std::ofstream& fstream, unsigned int count, WORD hour, WORD minute, unsigned char result);
//                            ^
//                            Note ampersand here
void WriteToFile(std::ostream& ostream, unsigned int count, WORD hour, WORD minute, unsigned char result);

现在,您可以传递任何类型的输出流(文件、字符串、
std::cout
),它将工作。

您是如何尝试“将fstream添加到函数的输入”的?你能给我们看看吗?你有什么问题吗?“如果你还没有这样做,请。”某个程序员修改了我的问题以解决你提到的缺点。谢谢你指出它们。希望现在这些都整理好了。谢谢你的评论。我会考虑的。请注意,我更新了我的问题,希望您能更好地理解我的问题并提出解决方案。请查看我下面的@Some programmer dude的答案。我只想对更新后的问题说同样的话。:)