一些C++函数

1.memcpy()

所属包:#include <string.h>
定义:void *memcpy(void *dest, const void *src, size_t n)
描述:用于将src指向的内存内容复制n个字节存储到dest指向的内存地址上。src与dest指向的内存区域不允许重叠。与strcpy()不同的是,memcpy()会完整的复制n个字节,不会因为遇到字符串结束'\0'而结束。
参数:dest-拷贝的目标地址
src-拷贝的源地址
n-拷贝的长度
返回:指向dest的指针

2. lseek

所属包:
#include<sys/types.h>
#include<unistd.h>
定义: off_t lseek(int fileDescription,off_t offset ,int fromWhere)
描述:文件读写指针指向一个已打开的文件当前的读写位置,该函数用于控制文件读写指针的位置。
参数:fileDescription-进行操作的文件描述符
offset-文件指针的偏移量
fromWhere-操作的起始位置。参数 fromWhere 为下列其中一种:
SEEK_SET 文件开头,则参数 offset 即为新的读写位置。
SEEK_CUR 当前读写位置,则以目前的读写位置往后增加 offset 个位移量。
SEEK_END 文件末尾,则将读写位置指向文件尾后再增加 offset 个位移量。
返回:操作后的文件读写指针相对于文件开头的位置.如果参数fileDescription指定的是 pipe(管道)、FIFO 或者 socket,lseek 返回 -1 并且置 errno 为 ESPIPE.

int File :: Close (off_t curLength) {

//myFilDes is the file description, int
// write out the current length in pages
lseek (myFilDes, 0, SEEK_SET);
write (myFilDes, &curLength, sizeof (off_t));

// close the file
close (myFilDes);

// and return the size
return curLength;

}



3.原型:extern char *strtok(char *s, char *delim);

所属包:#include <string.h>
功能:分解字符串为一组标记串。s为要分解的字符串,delim为分隔符字符串。
说明:首次调用时,s必须指向要分解的字符串,随后调用要把s设成NULL。
strtok在s中查找包含在delim中的字符并用NULL( '\0 ')来替换,直到找遍整个字符串。返回指向下一个标记串。当没有标记串时则返回空字符NULL。
//   strtok.c 
#include <syslib.h>
#include <string.h>
#include <stdio.h>
main()
{
char *attribute= "part.ps_partkey";
char *separator = " .";
char *tableName;

tableName=strtok(attribute, separator);
while(tableName)
{
printf( "%s\n ",tableName);
strtok(NULL, separator);
}

getchar();
return 0;
}


4.sprintf 非常好用的字符串处理函数

函数功能:把格式化的数据写入某个字符串
头文件:#include <stdio.h>
函数原型:int sprintf( char *buffer, const char *format [, argument] … );
返回值:字符串长度(strlen)
描述:用法类似于printf,是个变参函数。可以用于连接不同的内容,比如将两个字符串连接在一起,sprintf(outString, "%s %s %s!",“I”,"Love", "you");//产生字符串“I Love you!”; 也可以用于将其他类型数据转换成字符串,如sprintf(outString, "%d", 123); //产生"123"; 甚至可以根据你需要的格式输出,sprintf(outString, "%10.3f", 3.1415626); //产生:" 3.142",指定宽度,不足的左边补空格。

void Record :: PrintRecord(char * bits){
char fileName[32];
sprintf(fileName, "Join_%d.temp", staticNumFiles);
FILE *writefile = fopen (fileName, "w");
char str[128];
double *myDouble = (double *) &(bits[pointer]);
sprintf(str, "%e", *myDouble);
fputs(str, outFile);
fclose(writefile);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值