1
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

int main()
{
    FILE* number;
    char strings[3][100];
    errno_t err = fopen_s(&number, "number.bin", "wb");
    if (err != 0)
    {
        printf("error open number.bin\n");
        return 1;
    }

    FILE* divided;
    err = fopen_s(&divided, "divided.bin", "wb+");
    if (err != 0)
    {
        printf("error open divided.bin\n");
        return 1;
    }

    printf("Enter 3 lines:\n");

    for (int i = 0; i < 3; i++)
    {
        fgets(strings[i], sizeof(strings[i]), stdin);
        fwrite(strings[i], sizeof(char), strlen(strings[i]), number);
        char* token;
        char* nextToken = NULL;
        token = strtok_s(strings[i], " ", &nextToken);
        int count = 0;
        int numbers[100] = { 0 }; // Ініціалізація масиву чисел нулями
        while (token != NULL)
        {
            if (atoi(token) % 2 == 0)
            {
                numbers[count] = atoi(token);
                count++;
            }
            token = strtok_s(NULL, " ", &nextToken);
        }
        for (int j = count - 1; j >= 0; j--)
        {
            fprintf(divided, "%d ", numbers[j]);
        }
        fprintf(divided, "\n");
    }

    fseek(divided, 0, SEEK_SET);

    printf("Numbers that are multiples of 2 in reverse order:\n");
    char str[100];
    while (fscanf_s(divided, "%99s", str, (unsigned)_countof(str)) == 1)
    {
        if (strcmp(str, "0") != 0)
        {
            printf("%s ", str);
        }
    }

    fclose(number);
    fclose(divided);

    printf("\nSuccessfully written to file divided\n");

    return 0;
}

drive from the keyboard

12 4 9 5
200 4568 89 8
6 33333 489 725 20 

I will rewrite in the resulting file all numbers that are divisible by 2 in reverse order get:

4 12 
8 4568 200 
0 20 6 

where did the 0 in the last line come from?

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • 1
    Don't forget that `fgets()` retains the newline, which you have not filtered out with `strtok`. Passing a string which is just a newline to `atoi` will return `0`. I suggest using a delimiter string of `" \n"`. Also, `atoi()` is a very blunt instrument: `strtol()` is better. – Weather Vane May 07 '23 at 18:16
  • The output files `number.bin` and `divided.bin` contain newline characters. But, I (with MS VC) cannot replicate your output – not only is there no `0`, but the output is all on one line, unlike your example output. Perhaps you have a version problem. – Weather Vane May 07 '23 at 18:31
  • 1
    `"where did the 0 in the last line come from?"` -- You should be easily able to answer this question yourself, by running your program line by line in a [debugger](https://stackoverflow.com/q/25385173/12149471) while monitoring the output and the values of all variables. – Andreas Wenzel May 08 '23 at 03:14
  • If the first token is an **odd** number what happens with `for (int j = count - 1; j >= 0; j--)`? – David C. Rankin May 08 '23 at 05:59

1 Answers1

1

Your "drive from the keyboard" text in the question appears to be an exact copy and paste of your actual input. Good for you! There is a space after the last 20, which is being read as a zero. If you get rid of the space, hitting return right after the last digit, then your 0 20 6 in the last line of your output becomes 20 6.

You should consider using strtol() instead of atoi(), to make sure you're really getting a number.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158