C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  117 Posts :: 2 Stories :: 61 Comments :: 0 Trackbacks

常用链接

留言簿(8)

搜索

  •  

最新评论

阅读排行榜

评论排行榜

1、 将“字符串”转化换为“整数”

      1.1    转换函数

         字符串转换成双精度类型的函数有:atof(……)和_wtof(……)
         转换成整型的函数有:atoi(……)、_atoi64(……)、_wtoi(……)和_wtoi64(……)
         转换成长整型的函数有:atol(……)和_wtol(……)。

函数的原型如下:

double atof(
   
const char *string
);
double _wtof(
   
const wchar_t *string
);
int atoi(
   
const char *string
);
_int64 _atoi64(
   
const char *string
);
int _wtoi(
   
const wchar_t *string
);
_int64 _wtoi64(
   
const wchar_t *string
);
long atol(
   
const char *string
)
long _wtol(
   
const wchar_t *string
)

说明:上述函数参数为要转换的字符串,函数的返回结构式转换后的数据。
如果对这个函数原型不懂得话,可看下帖,因为我刚开始看也糊涂,就csdn提问了。
http://topic.csdn.net/u/20100126/14/9dff0450-0dd3-498a-a7ef-b3a66e512bb4.html

1.2    代码参考
         因为是初学者,敲书上的代码,开始无法实现。问别人,发现时自己文件建立方式错误。下面是我建立工程的步骤,以及学到的东西:
         第一步:file->new,再选择选项卡的project,选择Win32 Console Application



第二步:我这个工程的代码选择的第四个。其他的一些代码,选择第三个就足够了,以后的一些代码一般选择的第三个。


第三步:双击图中选中的那个,就可以写代码了,具体代码以及注释见下文。




//下面这个头文件是必须包括的而且是第一个
#include "stdafx.h"
//文件的名字是hello
#include "Hello.h"
//下面用的是C++标准的写法,不用.h的
#include <iostream>
using namespace std;


//_DEBUG宏,具体的作用现在还不是很清楚
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    
int nRetCode = 0;

    
// initialize MFC and print and error on failure, Leave the code here would be OK.
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    
{
        
// TODO: change error code to suit your needs
        cerr << _T("Fatal Error: MFC initialization failed"<< endl;
        nRetCode 
= 1;
    }

    
else
    
{
        
// TODO: code your application's behavior here.
        CString strHello = "Hello kangnixi";
        
//strHello.LoadString(IDS_HELLO); I will press F5 to debug.? seen>?
        cout << (LPCTSTR)strHello << endl;
        CString ting1
="1234";
        
int i_num=atoi(ting1);
        cout
<<i_num<<endl;
        
        CString ting2
="1231344.45345";
        
double d_num=atof(ting2);
        cout
<<d_num<<endl;
    }

    
return nRetCode;
}

上面是我的代码,代码运行的时候出现一些问题,关于问题解答可见下帖:
http://topic.csdn.net/u/20100126/16/6c71f2cc-1a3b-47d1-ba20-23e378bb35b6.html

如果还想获得更多关于《Visual C++代码参考与技巧大全》的内容,可点击下面网址,

http://www.cppblog.com/kangnixi/archive/2010/01/13/105591.html
posted on 2010-01-27 12:32 烟皑 阅读(2740) 评论(0)  编辑 收藏 引用 所属分类: 《Visual C++代码参考与技巧大全》学习笔记

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理