如何进行C++ Builder的Visual构件库

开发 后端
其实C#的语法与C++非常相似,有些变化引起的错误却很严重,本文就对于大多数用户头疼的C++ Builder进行详细的剖析与介绍。

如果你用过具有string数据类型的编程语言,你可能很不习惯,别人也有同感,所以标准C++语言库中提供了几个字串操作函数,希望大家能够在这边文章中得到自己想要的信息。

关于每个函数的详细说明和实例,见C++ Builder联机帮助。

//set up a string to hold 29 characters   
 
char buff[30];   
 
//copy a string literal to the buffer   
 
strcpy (buff,"This is a test.");//display it   
 
cout << buff << end;   
 
//initialize a second string buffer   
 
char buff2[]="A second string.";   
 
//copy the contents of this string to the first buffer   
 
strcpy (buff,buff2);   
 
cout << buff << end1;  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

这里介绍的字串操作是C语言中的字串处理方法。大多数C++编译器提供了cstring类,可以简化字串的处理(C++ Builder的Visual构件库中有个AnsiString类,可以处理字串操作。C++ Builder联机帮助中详细介绍了AnsiString类)。

尽管C语言中的字串处理方法比较麻烦,但并不过时,C++编程人员经常在使用cstring类和AnsiString类等字串类的同时使用C语言中的字串处理方法。这里不想对表中的每个函数进行举例说明,只想举两个最常用的函数。strcpy()函数将一个字串复制到另一字串中,源字串可以是变量或直接字串。例如下列代码:

//set up a string to hold 29 characters   
 
char buff[30];   
 
//copy a string literal to the buffer   
 
strcpy (buff,"This is a test.");//display it   
 
cout << buff << end;   
 
//initialize a second string buffer   
 
char buff2[]="A second string.";   
 
//copy the contents of this string to the first buffer   
 
strcpy (buff,buff2);   
 
cout << buff << end1;  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

这里建立了放10个字符的字符数组,最初指定需要9个字节的字符串(记住终止null)。后来可能忘记了数组长度,将需要16个字节的字串复制到了缓冲区,对数组重载了六个字节。这个小小错误就擦去了某个内存位置上的六个字节。

所以将数据复制到字符数组中时要特别小心。另一个常用的字串函数是sprintf()。这个函数可以混合文本和数字建立格式化字串。下面例子将两个数相加,然后用sprintf()建立字串以报告结果:

//set up a string to hold 29 characters   
 
char buff[30];   
 
//copy a string literal to the buffer   
 
strcpy (buff,"This is a test.");//display it   
 
cout << buff << end;   
 
//initialize a second string buffer   
 
char buff2[]="A second string.";   
 
//copy the contents of this string to the first buffer   
 
strcpy (buff,buff2);   
 
cout << buff << end1;  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

本例中%d告诉sprintf()函数此处有个整型值,格式字串末尾插入变量x,告诉sprintf()在字串的这个位置放上变量x的值。sprintf()是个特别的函数,可以取多个变元。你必须提供目标缓冲区和格式字串,但格式字串后面的变元数是个变量。下面的sprintf()例子用了另外三个变元:

//set up a string to hold 29 characters   
 
char buff[30];   
 
//copy a string literal to the buffer   
 
strcpy (buff,"This is a test.");//display it   
 
cout << buff << end;   
 
//initialize a second string buffer   
 
char buff2[]="A second string.";   
 
//copy the contents of this string to the first buffer   
 
strcpy (buff,buff2);   
 
cout << buff << end1;  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

许多编程人员因为忘了这个简单的事实而夜不能寐,苦苦折腾。这是个常见的错误,别说我没有告诉你。C++ Builder有个兄弟叫wsprintf(),是Windows版的sprintf().Windows程序中可能同时用这两个函数。

wsprintf()与sprintf()的作用相似,***的差别是不能在格式字串中放上浮点数。C++ Builder程序中两个函数均可使用,但用sprintf()更好,因为它完全支持浮点数(还可以少输入一个字符)。关于sprintf()的进一步介绍,见C++ Builder联机帮助。

【编辑推荐】

  1. 简介学习C++总结之谈
  2. C++库函数进行学习探索总结笔记
  3. C++类库设计的基本构思与方法
  4. C++语言真的还有市场价值?
  5. C++类库设计的基本构思与方法
责任编辑:chenqingxiang 来源: NET130
相关推荐

2010-01-20 10:49:29

Visual C++界

2010-01-22 16:21:50

C++ Builder

2010-01-12 09:59:08

Visual C++

2010-01-20 09:32:30

C++类

2010-01-27 15:29:45

C++异常处理

2010-01-20 13:29:40

C++环境

2010-01-26 14:10:22

Visual C++

2010-02-02 13:04:03

C++头文件

2014-09-19 10:46:36

LuaCC++

2010-01-15 19:05:42

学习C++

2010-02-05 17:16:05

C++构造函数

2010-01-27 10:45:21

C++单例模式

2010-01-11 11:02:59

Visual C++

2009-12-10 17:19:05

Visual Stud

2010-01-21 16:33:44

C++Builder

2010-01-15 17:31:18

C++Test

2010-01-08 17:13:46

Visual C++环

2010-01-12 14:22:26

Visual C++

2010-01-12 15:13:37

Visual C++环

2009-08-27 15:33:58

C# Cookie编程
点赞
收藏

51CTO技术栈公众号