将标题空格替换为 '_' , 并自动复制到剪切板上

代码:

#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <conio.h>

//清空剪切板
int ClearClipboradBuffer()
{
 HGLOBAL hClipboard = NULL;

 if( OpenClipboard( NULL ) )
 { 
  hClipboard = GetClipboardData( CF_TEXT );
  GlobalFree( hClipboard );
  EmptyClipboard();
  CloseClipboard();
  return 0;
 }
 else
 {
  return -1;
 }
}

//将字符串放到剪切板
int AppendBufferToClipboard( char *cBuffer, int nSize )
{
 HGLOBAL hClipboard = NULL;
 char *pClipBuf = NULL;

 if( OpenClipboard( NULL ) )
 { 
  hClipboard = GlobalAlloc( GMEM_DDESHARE, nSize+1 );
  pClipBuf = (char*)GlobalLock(hClipboard);
  memset( pClipBuf, 0, nSize+1 );
  memcpy( pClipBuf, cBuffer, nSize );
  GlobalUnlock( hClipboard );
  SetClipboardData( CF_TEXT, hClipboard );
  CloseClipboard();
 }
 else
 {
  return -1;
 }

 return 0;
}


int main(){
    char str[100];
    char preTitle[20] = {0};
    char buf[512] = {0};

    printf("1.【*hlwyfeng*分享】\n");
    printf("2.自定义输入\n");
    printf("3.不添加\n");
    printf("***************************\n");
    printf("请选择: ");
    int choose;
    scanf("%d", &choose);
    switch(choose){
    case 1:
        strcpy(preTitle,"【*hlwyfeng*分享】");
        system("cls");
        break;
    case 2:
        printf("请输入标题前缀: ");
        scanf("%s", preTitle);
        getchar();
        system("cls");
        break;
    case 3:
    default:
        system("cls");
    }

    while(1){
        printf("输入要处理的字符串: \n");

        //scanf("%s", str);//遇到空格会停
        gets(str);
        int k = strlen(str);

        //*****测试
        ClearClipboradBuffer();
        //*****测试

        for(int i = 0; i < k; i++){
            if (str[i] == ' ')
                str[i] = '_';
        }
        system("cls");
        int p = sprintf(buf,"%s%s",preTitle, str);
        printf("%s\n", str);

        //*****测试
        AppendBufferToClipboard(buf, p);
        //*****测试
    }

    return 0;
}

//*****测试
//*****测试


 

posted @ 2014-11-03 09:35  TabWeng  阅读(462)  评论(0编辑  收藏  举报