C ++ - 主要参数 [英] C++ - arguments to main

查看:149
本文介绍了C ++ - 主要参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常基本的问题。我尝试编写一个程序,输出的文件的文件名拖动到exe。



我遵循本指南的主要参数:


这是我的代码:

  #includestdafx.h 
#include< iostream>
using namespace std;
int _tmain(int argc,_TCHAR * argv [])
{
cout<<num arguments:<< argc<<\\\


while(argc--)
printf(%s\\\
,* argv ++);

cout<<press any key;
getchar();
return 0;
}

但所有输出都是:



如果我运行它不拖动和放置文件到exe:



num个参数:1



G



按任意键



如果我拖放3个文件, >

num个参数:4



G



p>

G



G



p>

应用程序名称或任何文件的名称都不是以G开头



有什么问题?



谢谢!

解决方案

因为你是混合Unicode而不是。我不能解释Gs抱歉(除非你的所有文件都在驱动器G:?),但你会看到完整的文件名与

  while(argc--)
_tprintf(_T(%s\\\
),* argv ++);



事后,由sbi的回答:我找不到 _tcout 用作

  _tcout<< * argv ++; 

如果你想保留C ++ - 我想你必须定义一个, p>

  #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%确定是什么。


very basic question. i try to write a program that outputs the filenames of the files dragged onto the exe.

i followed this guide for main arguments: http://publications.gbdirect.co.uk/c_book/chapter10/arguments_to_main.html

this is my code:

#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;
}

but all it output's is:

if i run it without dragging&dropping files onto the exe:

num arguments: 1

G

press any key

if i drag&drop 3 files it will output:

num arguments: 4

G

G

G

G

press any key

neither the application name, nor the name of any of the files starts with "G"

what's the problem?

thanks!

解决方案

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++);

instead.

Afterthought, and beaten to it by sbi's answer: I can't find a _tcout to use as

_tcout << *argv++;

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

although this being C++ they probably shouldn't be defines but something else, and I'm not 100% sure what.

这篇关于C ++ - 主要参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆