诊断
我相信您几乎可以肯定是在 Windows 系统上工作,因为您在 fopen() 模式下使用了 "wt"。 t 不被 C 标准或大多数 Unix 系统识别。也因为你 used fflush(stdin) 这在非 Windows 系统上基本上没有意义,但被定义为在 Windows 系统上执行或多或少有用的任务。
您的主要问题是您使用"wt" 打开一个文本文件进行写入,但是当您使用fwrite() 时,您正在将二进制数据写入一个文本文件。这意味着包含 10 或 13 的条目可能会造成混乱。
使用"wb"(阅读时使用"rb")。这受到 C 标准的支持,表明您正在执行二进制 I/O,而不是文本 I/O,并防止 I/O 系统对您的数据造成严重破坏。
当您只有包含 100 条记录的数组时,您也在写入 200 条记录(正如 R Sahu 在他们的 answer 中指出的那样)——这也不是幸福的秘诀。
您使用while (!feof(fp1)) 的循环是错误的;使用while (!feof(file)) is always wrong。您继续阅读相同的元素 — listadol[0] 和 ventasl[0],因为您只需将数组名称传递给 fread()。您可以在每个文件的一个 fread() 操作中读取所有记录,或者您需要正确索引数组(例如传递 &listadol[i])。
严格来说,您应该验证fopen() 调用(特别是)是否成功。可以说,您应该对 fread() 和 fwrite() 调用执行相同的操作。超级狂热者会检查fclose() 电话。如果数据文件很重要,您应该这样做,以防磁盘上没有足够的空间,或者出现其他严重错误。
处方
将更改放在一起,并使用NUM_ENTRIES 识别条目数(并将其从 100 更改为 10 以保持输出的紧凑性),我得到:
#include <stdio.h>
#include <stdlib.h>
struct estructura_salida
{
int num_art;
int exist;
};
enum { NUM_ENTRIES = 10 };
int main(void)
{
struct estructura_salida listado[NUM_ENTRIES];
struct estructura_salida ventas[NUM_ENTRIES];
struct estructura_salida listadol[NUM_ENTRIES];
struct estructura_salida ventasl[NUM_ENTRIES];
for (int i = 0; i < NUM_ENTRIES; i++)
{
listado[i].num_art = i + 1;
listado[i].exist = i + 20;
ventas[i].num_art = i + 1;
ventas[i].exist = i + 10;
}
printf("\ncargados\n");
for (int i = 0; i < NUM_ENTRIES; i++)
{
printf("%i\t%i\t%i\n", listado[i].num_art, listado[i].exist, ventas[i].exist);
}
const char name1[] = "stock.dbf";
const char name2[] = "ventas.dbf";
FILE *fp1 = fopen(name1, "wb");
FILE *fp2 = fopen(name2, "wb");
if (fp1 == NULL)
{
fprintf(stderr, "Failed to open file %s for reading\n", name1);
exit(EXIT_FAILURE);
}
if (fp2 == NULL)
{
fprintf(stderr, "Failed to open file %s for reading\n", name2);
exit(EXIT_FAILURE);
}
fwrite(listado, sizeof(struct estructura_salida), NUM_ENTRIES, fp1);
fwrite(ventas, sizeof(struct estructura_salida), NUM_ENTRIES, fp2);
fclose(fp1);
fclose(fp2);
printf("\nleyendo el archivo");
fp1 = fopen(name1, "rb");
fp2 = fopen(name2, "rb");
if (fp1 == NULL)
{
fprintf(stderr, "Failed to open file %s for reading\n", name1);
exit(EXIT_FAILURE);
}
if (fp2 == NULL)
{
fprintf(stderr, "Failed to open file %s for reading\n", name2);
exit(EXIT_FAILURE);
}
int n;
for (n = 0; fread(&listadol[n], sizeof(listadol[n]), 1, fp1) == 1; n++)
;
int m;
for (m = 0; fread(&ventasl[m], sizeof(ventasl[m]), 1, fp2) == 1; m++)
;
fclose(fp1);
fclose(fp2);
printf("\narchivo leido\n");
if (n != m)
{
fprintf(stderr, "Read different numbers of records (%d from %s, %d from %s)\n",
n, name1, m, name2);
exit(EXIT_FAILURE);
}
for (int i = 0; i < n; i++)
{
printf("%i\t%i\t%i\n", listadol[i].num_art, listadol[i].exist, ventasl[i].exist);
}
return 0;
}
最好使用一个函数来打开检查失败并报告错误并退出的文件 - 读者练习。
输出是:
cargados
1 20 10
2 21 11
3 22 12
4 23 13
5 24 14
6 25 15
7 26 16
8 27 17
9 28 18
10 29 19
leyendo el archivo
archivo leido
1 20 10
2 21 11
3 22 12
4 23 13
5 24 14
6 25 15
7 26 16
8 27 17
9 28 18
10 29 19
十六进制转储程序显示这两个数据文件包含您所期望的:
stock.dbf:
0x0000: 01 00 00 00 14 00 00 00 02 00 00 00 15 00 00 00 ................
0x0010: 03 00 00 00 16 00 00 00 04 00 00 00 17 00 00 00 ................
0x0020: 05 00 00 00 18 00 00 00 06 00 00 00 19 00 00 00 ................
0x0030: 07 00 00 00 1A 00 00 00 08 00 00 00 1B 00 00 00 ................
0x0040: 09 00 00 00 1C 00 00 00 0A 00 00 00 1D 00 00 00 ................
0x0050:
ventas.dbf:
0x0000: 01 00 00 00 0A 00 00 00 02 00 00 00 0B 00 00 00 ................
0x0010: 03 00 00 00 0C 00 00 00 04 00 00 00 0D 00 00 00 ................
0x0020: 05 00 00 00 0E 00 00 00 06 00 00 00 0F 00 00 00 ................
0x0030: 07 00 00 00 10 00 00 00 08 00 00 00 11 00 00 00 ................
0x0040: 09 00 00 00 12 00 00 00 0A 00 00 00 13 00 00 00 ................
0x0050:
除了奇数的 CR 或 LF 之外,没有任何可打印的字符,因此右侧的信息并不能提供所有信息。 (在运行 macOS 10.13.6 High Sierra 的 Mac 上测试,使用 GCC 8.2.0。)