【问题标题】:C++ method works fine in a simple C++ project but not in a Smart Device ProjectC++ 方法在简单的 C++ 项目中工作正常,但在智能设备项目中不适用
【发布时间】:2012-03-22 12:30:24
【问题描述】:

我有一个简单的函数,它写入一个 .txt 文件。此方法在简单的 C++ 项目中完美运行。但同样的函数,当在 C++ SmartDeviceProject 中创建时,不会向文件写入任何内容。

有什么问题?

代码示例如下。

STDMETHODIMP CHelloproxy::Hai(void)
{
    CTime t = CTime::GetCurrentTime();

    fstream file("D:\\k.txt", ios::app);

    file << t.GetHour() << ":" << t.GetMinute() << ":" << t.GetSecond() << "-"
         << "Add() operation started..." << endl;

    //file<<t.Format() << "-" << "Add() operation started..." << endl;

    HRESULT hr = proxy->Hai();

    file <<t.GetHour() << ":" << t.GetMinute() << ":" << t.GetSecond() 
         << "-" << "Add() operation finished..." << endl;

    file.close();

    return hr;
}

【问题讨论】:

  • 将代码放在您的帖子中,而不是在评论中。

标签: c++ visual-studio visual-c++ atl smart-device


【解决方案1】:

请检查该行的有效性 fstream 文件("D:\k.txt", ios::app); 意思是“可能 d:\ 在智能手机中不存在”。

以下是 fstream 对象创建的详细信息

http://www.cplusplus.com/reference/iostream/fstream/fstream/

" fstream ( );显式 fstream ( const char * 文件名, ios_base

:openmode 模式 = ios_base::in | ios_base::out );

构造对象并可选择打开文件 构造 fstream 类的对象。这意味着初始化关联的 filebuf 对象,并以 filebuf 对象作为参数调用其基类的构造函数。

此外,当使用第二个构造函数版本时,流与物理文件相关联,就好像调用了具有相同参数的成员函数 open。

如果构造函数没有成功打开文件,尽管没有文件关联到流缓冲区并且设置了流的failbit,但仍然创建对象(可以通过继承成员失败检查)。 "

【讨论】:

  • 我的智能设备中没有任何驱动器。在这种情况下,默认位置是什么。
  • 尝试使用 getcwd char path[256] = {'\0'} 打印当前工作目录; getcwd(路径,255); printf("%s\n", 路径);
猜你喜欢
  • 1970-01-01
  • 2021-10-13
  • 2015-06-16
  • 2011-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多