【发布时间】:2014-04-06 21:50:30
【问题描述】:
我有以下主要方法:
int main(string argf)
{
ifstream exprFile(argf);
string inExpr;
if (exprFile.is_open())
{
while ( getline(exprFile,inExpr) )
{
//do stuff
}
exprFile.close();
}
else cout << "Unable to open file";
system("pause"); // to wait for user input; allows the user to see what was printed before the window closes
return 0;
}
我已使用以下命令从命令行运行此程序:
- "C:\Complete Filepath\Project2.exe" "C:\Differnt Filepath\args.txt"
- C:\Complete Filepath\Project2.exe C:\Differnt Filepath\args.txt
- "C:\完整文件路径\Project2.exe" "args.txt"
- C:\完整文件路径\Project2.exe args.txt
最后两个 args.txt 与可执行文件位于同一目录中。所有四个都给出了“无法打开文件”的结果。在对其进行任何操作之前尝试打印 argf 值根本没有产生任何结果。完全空白的打印语句。
然后我进入 Visual Studio 2010 选项,并在参数部分下添加了 args.txt 文件的所有变体,该文件也位于不同的位置,但没有任何效果。
我做错了什么?
您应该如何打开在命令行中作为参数传递的文件?
【问题讨论】:
-
这不是
main()的有效声明。有关main()的定义允许和不允许的详细讨论,请参阅What shouldmain()return in C and C++。
标签: c++ file command-line arguments