-1

I am writing a class to provide various IO operations that will be used for reading and writing to a file. I am currently trying to inherit from fstream rather then create my own file open, close, etc functions. Currently my sample code below has no functions added, I simply open the write to it and close it. However the test code is unable to successfully open the file when exicuted. I will be adding additional code I can properly inherit from fstream. I get no compiler warnings or errors with this code using g++, and it runs without crashing however I get the "Unable to open file" message, proving the open was not successful.

int main(int argc, char* argv[])
{
    FileParser myFileParser;

    myFileParser.open("c:\test.txt"); 
  if (myFileParser.is_open())
  {
    myFileParser << "This is a line.\n";
    myFileParser << "This is another line.\n";
    myFileParser.close();
  }
  else
  {
    cout << "Unable to open file\r\n";
  }

  myFileParser.close();

    return 0;
}

#pragma once
#include <fstream>
using namespace std;

class FileParser: public fstream 
{
    public:
        FileParser(void);
        virtual ~FileParser(void);

    private:
};

#include "FileParser.h"

FileParser::FileParser(void)
{
}

FileParser::~FileParser(void)
{
}

Any help with this issue would be appreciated.


The issue has now been resolved, thanks for the support.

Regards

4

1 回答 1

1

为了解决您的直接问题:

  1. 您忘记转义反斜杠:

    myFileParser.open("c:\\test.txt");

  2. 从文件路径来看,我猜您在 Windows 上,您可能会收到“权限被拒绝”,因为您没有在该位置打开文件的权限(对于 Vista 及更高版本来说是这样)。

于 2013-11-01T03:26:31.147 回答