【发布时间】:2010-10-09 03:01:01
【问题描述】:
首先,我要确定我的文件夹目录中确实有文本文件。我正在使用 Visual Studio,这是我的源代码正在编译的地方。
下面的代码应该说明它为什么不工作。在视觉工作室。
int main( const int argc, const char **argv )
{
char usrMenuOption;
const char *cFileName = argv[ 1 ];
checkName( cFileName ); // supplying the checkName function with contents of argv[1]
usrMenuOption = getUsrOption(); // calling another function
fgetc(stdin);
return 0;
}
ifstream *openInputFile( const char *cFileName )
{
// this function might be the pronblem.
ifstream *inFile;
inFile = new ifstream;
inFile->open( cFileName, ios::in );
return inFile;
}
bool checkName( const char *cFileName )
{
// it works fine if i use a regular ifstream obj and not the one from the function
ifstream *inFile;
inFile = openInputFile( cFileName );
inFile->open( cFileName, ios::in );
if ( inFile->good() )
{
return true;
}
else
{
cout << '"' << cFileName << '"' << ": File does not exist! " << endl;
return false;
}
}
如果我为 ifstream 使用非指针对象,它确实有效。 但是我需要使用我制作的功能以这种方式打开所有输入文件。 我有点困惑,因为我在 dev-cpp 中编译时没有这个问题
【问题讨论】:
-
那么它到底有什么问题呢?如果它没有编译,编译器在哪里抱怨以及抱怨什么?如果它不运行,它在做什么意外?
-
你的意思不是资源泄露? :)
-
是的,除此之外,如果你不带参数启动它会崩溃......
标签: c++ visual-studio file