0

I cannot understand why there is no record of the result in the G.bin file the task condition is to write part of the line before or after 0 in the second file you need to write down the part where the amount is greater.

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

int main()
{
    FILE* fa;
    errno_t err = fopen_s(&fa, "F.bin", "wb");
    if (err != 0)
    {
        printf("Error opening file F.bin\n");
        return 1;
    }

    printf("Enter numbers:\n");

    int sum_before = 0;
    int sum_after = 0;
    int num;
    int zero_encountered = 0;
    char input[256];
    fgets(input, sizeof(input), stdin);

    size_t input_length = strlen(input);
    if (input[input_length - 1] == '\n')
    {
        input[input_length - 1] = '\0'; // Видалити символ нового рядка
    }

    fwrite(input, sizeof(char), strlen(input), fa);

    fclose(fa);

    char* token;
    char* nextToken = NULL;
    token = strtok_s(input, " ", &nextToken);

    while (token != NULL)
    {
        num = atoi(token);
        if (num == 0)
        {
            zero_encountered = 1;
        }
        if (!zero_encountered)
        {
            sum_before += num;
        }
        else
        {
            sum_after += num;
        }
        token = strtok_s(NULL, " ", &nextToken);
    }

    FILE* fb;
    err = fopen_s(&fb, "G.bin", "wb");
    if (err != 0)
    {
        printf("Error opening file G.bin\n");
        return 1;
    }

    if (sum_before > sum_after)
    {
        size_t part_length = strlen(input) - strlen(nextToken) - 1; // Обчислюємо довжину першої частини
        fwrite(input, sizeof(char), part_length, fb);
    }
    else
    {
        fwrite(nextToken, sizeof(char), strlen(nextToken), fb);
    }

    fclose(fb);

    printf("Результат успішно записано у файл G.bin\n");

    return 0;
}


data from the keyboard:

12 45 78 0 312 545 899 

data that should be in the G.bin file:

312 545 899 
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • `errno_t` ===> What is this new type that I'm unaware of? Why calculate the length of `input` 3 times? And do check the return of `fwrite()` and `fgets()`. – Harith May 11 '23 at 09:26
  • 1
    Have you tried to use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through your code, line by line while monitoring variables and their values, to see what really happens? – Some programmer dude May 11 '23 at 09:28
  • 2
    Be aware that `strtok_s` modifies the content of the `input`. Read the documentation closely and look what happens to the content of `input` inside the while loop. – Jabberwocky May 11 '23 at 10:04
  • 1
    @Haris `errno_t`, `fopen_s` etc. is C11 stuff, read this: https://en.cppreference.com/w/c/io/fopen. – Jabberwocky May 11 '23 at 10:07
  • 2
    Looks more like a non-standard Microsoft specific function `strtok_s` that uses 3 arguments instead of the C11 function that uses 4 arguments. Using standard functions instead of proprietary ones would make your program portable and would help us to reproduce the problem. – Bodo May 11 '23 at 10:37
  • 1
    Why are you using `fopen_s` and `strtok_s` instead of `fopen` and `strtok`? Is it because Microsoft told you to do so? In that case, I suggest that you ignore that recommendation. You can add the line `#define _CRT_SECURE_NO_WARNINGS` in the first line of your source file to suppress those warnings/error messages from Microsoft that tell you to use the `_s` versions of the functions. – Andreas Wenzel May 11 '23 at 13:27
  • Validate that the input contains only digits and whitespace, then use `sscanf()` to read the numbers from the input string without modifying it. To this end, the return value of `sscanf()` will tell you when you've reached the end of the string, and a `%n` conversion specifier will help you track how many characters of input are consumed by each call. – John Bollinger May 11 '23 at 13:49

1 Answers1

0

Some highlights:

  1. Don't use errno_t, strtok_s.
  2. Still you have possible bugs here (I didn't fix all your bugs).
  3. You need to do another checks if the user insert another 0's. In your case is just for one and only one zero.

Here is fixed code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    FILE* fa = fopen("F.bin", "wb");
    if (fa == NULL) {
        printf("Error opening file F.bin\n");
        return 1;
    }

    printf("Enter numbers:\n");

    int sum_before = 0;
    int sum_after = 0;
    int num;
    int zero_poped = 0;
    int zero_encountered = 0;
    char input[256];
    fgets(input, sizeof(input), stdin);

    size_t input_length = strlen(input);
    if (input[input_length - 1] == '\n') {
        input[input_length - 1] = '\0'; // Remove the newline character
    }

    fwrite(input, sizeof(char), strlen(input), fa);
    fclose(fa);

    // Split the input string manually
    int i = 0;
    while (input[i] != '\0') {
        int j = i;
        while (input[j] != ' ' && input[j] != '\0') {
            j++;
        }

        char number[10];
        strncpy(number, input + i, j - i);
        number[j - i] = '\0';
        num = atoi(number);

        if (num == 0) {
            zero_encountered = 1;
            zero_poped = j + 1;
        }
        if (!zero_encountered) {
            sum_before += num;
        } else {
            sum_after += num;
        }

        i = j + 1;
    }

    FILE* fb = fopen("G.bin", "wb");
    if (fb == NULL) {
        printf("Error opening file G.bin\n");
        return 1;
    }

    if (sum_before > sum_after) {
        fwrite(input, sizeof(char), zero_poped - 1, fb);
    } else {
        fwrite(input + zero_poped, sizeof(char), strlen(input) - zero_poped, fb); 
    }
    fclose(fb);

    printf("Result successfully written to G.bin\n");

    return 0;
}

Output (before bigger):

Enter numbers:   
8 2 0 1 1
Result successfully written to G.bin
In G.bin - 8 2

Output (after bigger):

Enter numbers:   
1 2 0 1 8
Result successfully written to G.bin
In G.bin - 1 8
gera verbun
  • 285
  • 3
  • 6