玩转vdbench数据-----字符串文件指针

文件操作,字符串指针的一些函数用法:

1,sprintf

#include <stdio.h>

int sprintf( char *buffer, const char *format [, argument,...] );

可以控制精度

char str[20];
double f=14.309948;
sprintf(str,"%6.2f",f);
可以将多个数值数据连接起来。
char str[20];
int a=20984,b=48090;
sprintf(str,"%f",a,b);
str[]="20984 48090"
可以将多个字符串连接成字符串
char str[20];
char s1={'A','B','C'};
char s2={'T','Y','x'};
sprintf(str,"%.3s%.3s",s1,s2);
可以动态指定,需要截取的字符数
char s1={'A','B','C'};
char s2={'T','Y','x'};
sprintf(str,"%.*s%.*s",2,s1,3,s2);
sprintf(s, "%*.*f", 10, 2, 3.1415926); 

    循环拼接的方法:
    double a[3] = {1.234,2.345,3.456};
    char *p = (char *)malloc(sizeof(char) * 100);
    char *point = p;
    for (int i = 0; i < 3 ; i++) {
        sprintf(point , "%g," , a[i]);
        for(;*point;) point++;
    }
    cout<<p<<endl;// C ----->print("%s",p);

2,memcpy()函数用法


函数原型

void *memcpy(void*dest, const void *src, size_t n);

void *memcpy(void *restrict dest, const void *restrict src, size_t n)  
{  
        int i;  
        const char *s = src;  
        char *d = dest;  
  
        assert(dest && src);  
        assert((src + n <= dest) || (dest + n <= src));  
  
        for (i = 0; i < n; i++)  
                d[i] = s[i];  
        return dest;  
}  

这里的几个关键点解释如下:

  1. src指针前面要加const,暗示src是输入参数;
  2. src和dest是非重叠内存,restrict关键字暗示编译器可以优化内存访问的编译;
  3. 断言(assert) src和dest非空,拷贝的源头和目的地非重叠,程序员要理解出错处理和断言的区别;
  4. 返回void *。

功能

由src指向地址为起始地址的连续n个字节的数据复制到以destin指向地址为起始地址的空间内。

头文件

#include<string.h>

返回值

函数返回一个指向dest的指针。

说明

  1.source和destin所指内存区域不能重叠,函数返回指向destin的指针。

  2.与strcpy相比,memcpy并不是遇到'\0'就结束,而是一定会拷贝完n个字节。

memcpy用来做内存拷贝,你可以拿它拷贝任何数据类型的对象,可以指定拷贝的数据长度;

例:

  char a[100], b[50];

  memcpy(b, a,sizeof(b)); //注意如用sizeof(a),会造成b的内存地址溢出。

  strcpy就只能拷贝字符串了,它遇到'\0'就结束拷贝;例:

  char a[100], b[50];

strcpy(a,b);

  3.如果目标数组destin本身已有数据,执行memcpy()后,将覆盖原有数据(最多覆盖n)。如果要追加数据,则每次执行memcpy后,要将目标数组地址增加到你要追加数据的地址。

  //注意,source和destin都不一定是数组,任意的可读写的空间均可。

作用:将s中的字符串复制到字符数组d中。

  //memcpy.c

  #include<stdio.h>

  #include<string.h>

  intmain()

  {

  char*s="Golden Global View";

  chard[20];

  clrscr();

  memcpy(d,s,strlen(s));

  d[strlen(s)]='\0';//因为从d[0]开始复制,总长度为strlen(s),d[strlen(s)]置为结束符

  printf("%s",d);

  getchar();

  return0;

  }

  输出结果:GoldenGlobal View

作用:将s中第14个字符开始的4个连续字符复制到d中。(从0开始)

  #include<string.h>

  intmain()

  {

  char*s="Golden Global View";

  chard[20];

  memcpy(d,s+14,4);//从第14个字符(V)开始复制,连续复制4个字符(View)

  //memcpy(d,s+14*sizeof(char),4*sizeof(char));也可

  d[4]='\0';

  printf("%s",d);

  getchar();

  return0;

  }

  输出结果: View
作用:复制后覆盖原有部分数据

  #include<stdio.h>

  #include<string.h>

  intmain(void)

  {

  charsrc[] = "******************************";

  chardest[] = "abcdefghijlkmnopqrstuvwxyz0123as6";

  printf("destinationbefore memcpy: %s\n", dest);

  memcpy(dest,src, strlen(src));

  printf("destinationafter memcpy: %s\n", dest);

  return0;

  }

  输出结果:

  destinationbefore memcpy:abcdefghijlkmnopqrstuvwxyz0123as6

  destinationafter memcpy: ******************************as6

3.lseek

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#define SIZE 1024

int main()
{
    int fd = open("big", O_RDWR|O_CREAT, 0777);
    if(fd == -1)
    {
        perror("open fd");
        return -1;
    }

    // 设置一个文件偏移指向1g处,卡住他;
    lseek (fd, 1024*1024*1024, SEEK_SET);

    char buf[SIZE] = "hello";
    write(fd,buf,1);

    close(fd);

    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章