"Krzaczki" w outpucie programu w konsoli windows

0

Mam program ktory poprawnie wyswietla polskie znaki z outputu programu w konsoli pod linuxem ale na windowsie juz 'wykrzacza' te polskie znaki, dlaczego moze tak byc ?
kod programu:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<curl/curl.h>
#include<stdbool.h>

struct receiveData{
  char *memory;
  size_t size;
};

static char errorBuffer[CURL_ERROR_SIZE];
char *receiveInput();
static bool init(CURL **conn, char *url, struct receiveData *rec);
static int writer(char *data, size_t size, size_t nmemb, void *userp);
void print(struct receiveData a);
int hexval(char c);
char *utf8(int u);

int main(int argc, char *argv[]){
  CURL *conn = NULL;
  CURLcode code;
  char buffer[100];

  struct receiveData recDat;

  recDat.memory = malloc(1);
  recDat.size = 0;

  if (argc != 3 || (strcmp(argv[1], "pl") == 0 && strcmp(argv[1], "en") == 0)){
    fprintf(stderr, "Usage: %s pl/en <url>\n", argv[0]);

    exit(EXIT_FAILURE);
  }

  if(strcmp(argv[1], "pl") == 0)
    sprintf(buffer, "http://translatica.pl//translate.php?direction=plen&source=%s", argv[2]);
  if(strcmp(argv[1], "en") == 0)
    sprintf(buffer, "http://translatica.pl//translate.php?direction=enpl&source=%s", argv[2]);

  curl_global_init(CURL_GLOBAL_DEFAULT);

  if (!init(&conn, buffer, &recDat)){
    fprintf(stderr, "Connection initializion failed\n");

    exit(EXIT_FAILURE);
  }

  code = curl_easy_perform(conn);
  curl_easy_cleanup(conn);

  if(code != CURLE_OK){
    fprintf(stderr, "Failed to get '%s' [%s]\n", argv[2], errorBuffer);

    exit(EXIT_FAILURE);
  }
  else
    print(recDat);

  return 0;
}

static bool init(CURL **conn, char *url, struct receiveData *rec){
  CURLcode code;

  *conn = curl_easy_init();
  if(conn == NULL){
    fprintf(stderr, "Failed to create CURL connection\n");

    exit(EXIT_FAILURE);
  }

  code = curl_easy_setopt(*conn, CURLOPT_ERRORBUFFER, errorBuffer);
  if(code != CURLE_OK){
    fprintf(stderr, "Failed to set error buffer [%d]\n", code);

    return false;
  }

  code = curl_easy_setopt(*conn, CURLOPT_URL, url);
  if(code != CURLE_OK){
    fprintf(stderr, "Failed to set URL [%s]\n", errorBuffer);

    return false;
  }

  code = curl_easy_setopt(*conn, CURLOPT_FOLLOWLOCATION, 1L);
  if(code != CURLE_OK){
    fprintf(stderr, "Failed to set redirect option [%s]\n", errorBuffer);

    return false;
  }

  code = curl_easy_setopt(*conn, CURLOPT_WRITEFUNCTION, writer);
  if(code != CURLE_OK){
    fprintf(stderr, "Failed to set writer [%s]\n", errorBuffer);

    return false;
  }

  code = curl_easy_setopt(*conn, CURLOPT_WRITEDATA, (void *)rec);
  if(code != CURLE_OK){
    fprintf(stderr, "Failed to set write data [%s]\n", errorBuffer);

    return false;
  }

  return true;
}

static int writer(char *contents, size_t size, size_t nmemb, void *userp){
  size_t realsize = size * nmemb;
  struct receiveData *rec = (struct receiveData *)userp;

  void *tmp = realloc(rec->memory, rec->size + realsize + 1);

  if(tmp == NULL){
    printf("not enough memory");
    free(rec->memory);

    return 0;
  }
  else
    rec->memory = tmp;

  memcpy(&(rec->memory[rec->size]), contents, realsize);
  rec->size += realsize;
  rec->memory[rec->size] = 0;

  return realsize;
}

void print(struct receiveData rec){
  int lenght = strlen(rec.memory), state = 0;

  for(int i = 0, j = 0, x = 0; i < lenght; ++i){
    if(rec.memory[i] == '\\' || state == 1){
      state = 1;
      ++i;
      if(rec.memory[i] == 'u'){
        state = 2;
        ++i;
      }
    }
    if(state == 2){
      ++j;
      x = x * 16 + hexval(rec.memory[i]);
    }
    if(j == 4){
      printf("%s", utf8(x));
      state = 0;
      j = 0, x = 0;
      ++i;
    }
    if(state == 0 && rec.memory[i] != '\\')
      printf("%c", rec.memory[i]);
    if(rec.memory[i] == '\\')
      --i;
  }

  printf("\n");
}

int hexval(char c){
  if(c == '0')return 0;
  if(c == '1')return 1;
  if(c == '2')return 2;
  if(c == '3')return 3;
  if(c == '4')return 4;
  if(c == '5')return 5;
  if(c == '6')return 6;
  if(c == '7')return 7;
  if(c == '8')return 8;
  if(c == '9')return 9;
  if(c == 'a')return 10;
  if(c == 'b')return 11;
  if(c == 'c')return 12;
  if(c == 'd')return 13;
  if(c == 'e')return 14;
  if(c == 'f')return 15;

  return 16;
}

char *utf8(int u){
  if(u == 0x0104)return "Ą";
  if(u == 0x0105)return "ą";
  if(u == 0x0106)return "Ć";
  if(u == 0x0107)return "ć";
  if(u == 0x0118)return "Ę";
  if(u == 0x0119)return "ę";
  if(u == 0x0141)return "Ł";
  if(u == 0x0142)return "ł";
  if(u == 0x0143)return "Ń";
  if(u == 0x0144)return "ń";
  if(u == 0x015a)return "Ś";
  if(u == 0x015b)return "ś";
  if(u == 0x00d3)return "Ó";
  if(u == 0x00f3)return "ó";
  if(u == 0x0179)return "Ź";
  if(u == 0x017a)return "ź";
  if(u == 0x017b)return "Ż";
  if(u == 0x017c)return "ż";

  return 0;
}
0

Różne kodowanie. Kompilator i konsola powinny mieć to samo kodowanie.
Posprawdzaj jakie masz ustawienia kompilatora i konsoli.

0

Uch.. było. Jedynym rozwiązaniem które działa w 100% to zapisanie pliku źródłowego w kodowaniu CP852.
Ale nie wszystkie edytory takie mają.
Może być potrzebne też wywołanie setlocale(), ale to dodatkowo, oprócz kodowania.

#include <locale.h>
#include <stdio.h>

int main()
{
  setlocale(LC_CTYPE,".852");
  printf("zażółć gęślą jaźń");
}

z.PNG

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0