非常基本的问题.我尝试编写一个程序,输出拖动到exe文件的文件的文件名.
我遵循本指南的主要论点:http: //publications.gbdirect.co.uk/c_book/chapter10/arguments_to_main.html
这是我的代码:
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"num arguments: "<<argc<<"\n";
while(argc--)
printf("%s\n", *argv++);
cout<<"press any key";
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但它的输出是:
如果我在没有将文件拖放到exe上的情况下运行它:
num参数:1
G
按任意键
如果我拖放3个文件,它将输出:
num参数:4
G
G
G
G
按任意键
应用程序名称和任何文件的名称都不以"G"开头
有什么问题?
谢谢!
你只是得到了第一个字母,因为你正在混合使用Unicode而不是.我无法解释Gs抱歉(除非你的所有文件都在驱动器G:?)但你会看到完整的文件名
while(argc--)
_tprintf(_T("%s\n"), *argv++);
Run Code Online (Sandbox Code Playgroud)
代替.
经过深思熟虑,并被sbi的回答殴打:我找不到一个_tcout用作
_tcout << *argv++;
Run Code Online (Sandbox Code Playgroud)
相反,如果你想保持C++ - 我想你必须定义一个,例如
#ifdef _UNICODE
#define _tcin wcin
#define _tcout wcout
#define _tcerr wcerr
#define _tclog wclog
#else
#define _tcin cin
#define _tcout cout
#define _tcerr cerr
#define _tclog clog
#endif
Run Code Online (Sandbox Code Playgroud)
虽然这是C++,但它们可能不应该被定义,而是其他东西,我不是100%肯定是什么.