我需要将一个文件的内容(它有 37 条记录)分散在 sqrt(37) 文件上(四舍五入)。实际上,问题是什么:我的代码没有在 7 文件中创建最后一个条目(它甚至没有创建它)。请告诉我要修复什么。提前致谢。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main(void) {
FILE* fp;
FILE* fp2;
int n, count; // n - количество записей в файле, count- количество файлов
char buffer[3];
char name[80] = "TMP";
double tmp1, tmp2, tmp3;
if ((fp = fopen("Data.txt", "r")) == NULL) {
printf("File not opened\n");
_getch();
exit(EXIT_FAILURE);
}
else
printf("File opened\n");
fscanf(fp, "%d", &n);
count = sqrt(n);
printf("%d", count);
for (int i = 0; i < count; i++) {
name[3] = 0;
_itoa(i + 1, buffer, 10);
strcat(name, buffer);
strcat(name, ".txt");
if ((fp2 = fopen(name, "w")) == NULL) {
printf("File fp2 not opened\n");
_getch();
exit(EXIT_FAILURE);
}
for (int j = 0; j < n / count; j++) {
fscanf(fp, "%lf %lf %lf", &tmp1, &tmp2, &tmp3);
fprintf(fp2, "%lf %lf %lf\n", tmp1, tmp2, tmp3);
}
fclose(fp2);
}
_getch();
return 0;
}
你在说话
n == 37吗?然后
给出 6;
执行 6 次 - 0、1、...、5。
你说的是哪个第七档?
请原谅我不清楚的问题。下次我会更小心的。
这只是我需要解决的整个问题的一部分。在其中,您需要将 n 条记录分散到 sqrt (n) 个文件中,然后在每个文件中对它们进行排序,然后将所有内容合并在一起。我问你问题的第一部分。但是,幸运的是,过了一会儿,我想到了。也许有人会需要它。