Почему добавляются лишние символы в конце выводимых строк из файла?

введите сюда описание изображения

#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>

#include <stdio.h>

#include <math.h>

#include <clocale>


struct ITrade {
    char name[15];
    double percentage1;
    double percentage2;
    int year1 = 1940;
    int year2 = 1958;
};


int main()
{
    setlocale(LC_ALL, "rus");
    system("chcp 1251");
    char buffer[255];
    char name_file[15];
    char percentage_file1[6];
    char percentage_file2[6];
    struct ITrade arr[7];
    int z = 0;
    int count = 0;
    int k = 0;
    int iterator = 0;
    FILE* file;

    char str[200];

    file = fopen("data.txt", "w+");

    while (z < 8) {
        fputs(gets_s(str), file);
        fputs("\n", file);
        z++;
    }


    z = 0;
    fclose(file);
    if ((file = fopen("data.txt", "r")) == NULL) {
        perror("Error occured while opening file");
        exit(0);
    }

    while ((fgets(buffer, 255, file)) != NULL && z < 7) {
        for (int i = 1; count != 3; i++) {

            if (buffer[i] == ' ' && buffer[i + 1] == ' ') {
                continue;
            }

            if (buffer[i] != ' ' && count == 0 && buffer[i] != '|') {
                while (buffer[i] != '|' && k < 15) {
                    name_file[k] = buffer[i];
                    k++;
                    i++;
                }
                k = 0;
            }

            if (buffer[i] != ' ' && count == 1 && buffer[i] != '|') {
                while (buffer[i] != '|' && k < 6) {
                    percentage_file1[k] = buffer[i];
                    k++;
                    i++;
                }
                k = 0;
            }

            if (buffer[i] != ' ' && count == 2 && buffer[i] != '|') {
                while (buffer[i] != '|' && k < 6) {
                    percentage_file2[k] = buffer[i];
                    k++;
                    i++;
                }
                k = 0;

            }
            count++;


        }

        for (int i = 0; i < 15; i++) {
            arr[z].name[i] = name_file[i];
        }
        arr[z].percentage1 = atof(percentage_file1);
        arr[z].percentage2 = atof(percentage_file2);
        z++;
        count = 0;
        printf("%s", buffer);
    }
    fclose(file);

    FILE *fileOutPut = fopen("dataOP.txt", "w+");

    printf("\n");
    for (int i = 0; i < 7; i++) {
        if (arr[i].percentage1 <= arr[i].percentage2) {
            double a = arr[i].percentage2 - arr[i].percentage1;
            fprintf(fileOutPut, "%lf %s\n", a, arr[i].name);
        }
    }

    fclose(fileOutPut);

    if ((fileOutPut = fopen("dataOP.txt", "r")) == NULL) {
        perror("Error occured while opening file");
        exit(0);
    }

    while ((fgets(buffer, 255, file)) != NULL) {        
        printf("%s\n", buffer);
    }


}

Ответы (0 шт):