0

I have to write a program that is using polish characters. The problem is with function in a function, both have the same one of variables(like a(int smth) b(int smth), this smth is the same one that had been used in both)

I have open_dyk function in main. There's text in the file, something with pl characters like grzegżółka in the first line, but it doesn't matter how many times I launch the program, it gives me huge A and random doubled character like yy 22, etc.

struct dyk{
    wchar_t line[200];
    struct dyk *next;
};
    typedef struct dyk dyk;

    dyk* add_to_bottom(wchar_t *buf, dyk *head)
    {
        dyk *current_node = head;
        dyk *new_node;

        while(current_node != NULL && current_node->next != NULL){
            current_node = current_node->next;
        }

        new_node = (dyk*)malloc(sizeof(dyk));
        wcscpy(buf, new_node->line);
        new_node->next = NULL;

        if (current_node != NULL) {
            current_node->next = new_node;
        } else {
            head = new_node;
        }

        return head;
    }

    void print_all(dyk *head) 
    {
        dyk *current_node = head;

        while ( current_node != NULL) {
            wprintf(L"%s ", current_node->line);
            current_node = current_node->next;
        }
    }

    void open_dyk(char name[100], dyk *head) 
    {
        wchar_t buforek[100];
        FILE *dyktando;
        dyktando = fopen(name, "r+");

        if(dyktando == NULL){
            wprintf(L"Błąd otwarcia pliku!\n");
        }else{
            while(!EOF){
                add_to_bottom(buforek, head);
            }
            print_all(head);
        }
        fclose(dyktando);
    }

main function consist:

system("chcp 852");
setlocale(LC_ALL, ".852");

dyk *show;
show = (dyk*)malloc(sizeof(dyk));
open_dyk("farm.txt", show);

As I said a bit earlier, expected result should looks like this:

grzegżółka?
ź ż

Actual results look like this:

A11

(or other variations like AYY, Abb, etc.)

Amatheon
  • 29
  • 5
  • 6
    Please turn that into an indented [mcve]. – Yunnosch Jan 21 '19 at 14:22
  • 6
    Whilst you're updating the indentation in your question you should probably double check you've pasted the code correctly as `while(!EOF)` doesn't look complete and there is no sign of any code that populates `buforek` – Chris Turner Jan 21 '19 at 14:28
  • [Valgrind](http://valgrind.org) is your friend. – alk Jan 21 '19 at 14:31
  • 4
    Another symptom of carelessly copied code: asymmetric `{}`. Fixing the indentation will help you fix that. But really, do not copy from your code, make a dedicated MCVE for StackOverflow. People here are really fond of that and it also tends to solve problems so that asking isn't needed anymore. – Yunnosch Jan 21 '19 at 14:31
  • The code you've presented doesn't do anything with the return value of `add_to_bottom()`. I can't be sure without an MCVE, but I'm inclined to suspect that that's a problem. – John Bollinger Jan 21 '19 at 14:38
  • @ChrisTurner Why `while(!EOF)` look like not complete, `buforek` have been used in `add_to_bottom()` @Yunnosch it's corrected, sry for that asymmetry at start – Amatheon Jan 21 '19 at 14:57
  • 1
    Hmm, unless a big typo, your code never reads anything in `open_dyk`. That is enough to later display uninitialized characters . – Serge Ballesta Jan 21 '19 at 15:03
  • Because typically you'd have something like `while((c=fgetc(dyktando))!=EOF)` if you're using `EOF`. And `add_to_bottom` isn't using `buforek` - it's copying the string from the newly created node, which hasn't been initialised, so will contain garbage - call should be `wcscpy(new_node->line, buf)`...but first you need to read some text into `buforek` – Chris Turner Jan 21 '19 at 15:21

0 Answers0