玩轉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;
}



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