C++ - main 的参数 [英] C++ - arguments to main
问题描述
非常基本的问题.我尝试编写一个程序,输出拖到 exe 上的文件的文件名.
very basic question. i try to write a program that outputs the filenames of the files dragged onto the exe.
我遵循本指南的主要论点:http://publications.gbdirect.co.uk/c_book/chapter10/arguments_to_main.html
i followed this guide for main arguments: 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;
}
但它的输出是:
如果我运行它而不将文件拖放到 exe 上:
if i run it without dragging&dropping files onto the exe:
参数个数:1
G
按任意键
如果我拖放 3 个文件,它将输出:
if i drag&drop 3 files it will output:
参数个数:4
G
G
G
G
按任意键
既不是应用程序名称,也不是任何文件的名称以G"开头
neither the application name, nor the name of any of the files starts with "G"
有什么问题吗?
谢谢!
推荐答案
你只能得到第一个字母,因为你混合了 Unicode 而不是.抱歉,我无法解释 Gs(除非您的所有文件都在驱动器 G 上:?)但您会看到带有
You're only getting the first letter because you're mixing Unicode and not. I can't explain the Gs sorry (unless all your files are on drive G:?) but you'll see the full filenames with
while(argc--)
_tprintf(_T("%s\n"), *argv++);
相反.
事后考虑,并被 sbi 的回答打败了:我找不到 _tcout
用作
Afterthought, and beaten to it by sbi's answer: I can't find a _tcout
to use as
_tcout << *argv++;
相反,如果您想保留 C++ - 我想您必须定义一个,例如
instead if you wanted to stay C++ - I guess you'd have to define one, e.g.
#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
虽然这是 C++,但它们可能不应该被定义,而是其他的东西,而且我不能 100% 确定是什么.
although this being C++ they probably shouldn't be defines but something else, and I'm not 100% sure what.
这篇关于C++ - main 的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!