How to get first 3 characters of an unsigned char (C++)

Viewed 48

How to get first 3 characters of an unsigned char (C++)

My program is failing because my if statement condition has errors. I don't know the code used to get the first 3 characters of an unsigned char (C++).

I have tried (unsigned)substr(c 0, 2) == "Swi" and it's throwing errors.


#include "stdafx.h"

#include <string>
#include <stdbool.h>
#include <windows.h>

#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")

#define DELAY 1000

#define _WIN32_WINNT 0x0501 // this is for XP


void set_clipboard_content(char *text) {
    HGLOBAL x = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, 64);
    char *y = (char*)GlobalLock(x);

    strcpy(y, text);
    GlobalUnlock(x);
    OpenClipboard(NULL);
    EmptyClipboard();
    SetClipboardData(CF_TEXT, x);
    CloseClipboard();
}

char *get_clipboard_content(void) {
    OpenClipboard(NULL);
    HANDLE data = GetClipboardData(CF_TEXT);
    GlobalLock(data);
    char *c = (char*)data;
    GlobalUnlock(data);
    //CloseClipboard();

    return c;
}



int main()
{

    char country1[] = "Switzerland is in the EU";
    char country2[] = "No country copied to clipboard";

    while (1) {
        char *c = get_clipboard_content();
        CloseClipboard();
        //check length
        if (((unsigned)strlen(c) == 11) && ((unsigned)substr(c 0, 2) == "Swi")) {
            set_clipboard_content(country1);
        } else {
            set_clipboard_content(country2);
        }

        Sleep(500);
    }
    return 0;
}

1 Answers
char country_first_chars[3];
for (int i = 0; i < 3; i++)
{
    country_first_chars[i] = c[i];
}

This should be what you are looking for, however I'm not sure if I understand the question correctly.

Related