为什么在写入二进制文件时,最后一行可能会出现0。

huangapple go评论53阅读模式
英文:

why when writing a binary file 0 may appear in the last line

问题

Here's the translated code portion:

#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 }; // Initialize the array of numbers with zeros
        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;
}

As for the "0" in the last line, it appears because the numbers array is initialized with zeros, and when there are no more even numbers to add to the array, it still contains those zeros. In the loop that writes the numbers to the divided file, it doesn't skip the zeros, which is why you see a "0" in the last line.

英文:
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;ctype.h&gt;
#include &lt;errno.h&gt;
int main()
{
FILE* number;
char strings[3][100];
errno_t err = fopen_s(&amp;number, &quot;number.bin&quot;, &quot;wb&quot;);
if (err != 0)
{
printf(&quot;error open number.bin\n&quot;);
return 1;
}
FILE* divided;
err = fopen_s(&amp;divided, &quot;divided.bin&quot;, &quot;wb+&quot;);
if (err != 0)
{
printf(&quot;error open divided.bin\n&quot;);
return 1;
}
printf(&quot;Enter 3 lines:\n&quot;);
for (int i = 0; i &lt; 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], &quot; &quot;, &amp;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, &quot; &quot;, &amp;nextToken);
}
for (int j = count - 1; j &gt;= 0; j--)
{
fprintf(divided, &quot;%d &quot;, numbers[j]);
}
fprintf(divided, &quot;\n&quot;);
}
fseek(divided, 0, SEEK_SET);
printf(&quot;Numbers that are multiples of 2 in reverse order:\n&quot;);
char str[100];
while (fscanf_s(divided, &quot;%99s&quot;, str, (unsigned)_countof(str)) == 1)
{
if (strcmp(str, &quot;0&quot;) != 0)
{
printf(&quot;%s &quot;, str);
}
}
fclose(number);
fclose(divided);
printf(&quot;\nSuccessfully written to file divided\n&quot;);
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?

答案1

得分: 1

你的问题中的“从键盘输入”文本似乎是您实际输入的精确复制。干得好!在最后的20后面有一个空格,这被视为零。如果您去掉空格,直接在最后一个数字后按回车键,那么您输出的最后一行中的0 20 6就变成了20 6

您应该考虑使用strtol()而不是atoi(),以确保您确实得到一个数字。

英文:

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.

huangapple
  • 本文由 发表于 2023年5月8日 01:18:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定