英文:
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(÷d, "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 <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?
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论