gpt4 book ai didi

c - Fprintf 没有将数据打印到文件

转载 作者:太空宇宙 更新时间:2023-11-04 04:12:41 26 4
gpt4 key购买 nike

我正在尝试获取 arr 数组中的数据以打印到文件“out.txt”。该程序之前能够执行此操作,但由于某种原因现在根本无法打印,甚至在撤消到以前的版本后也不会打印任何内容。

我有代码证明文件已成功打开并且数组中的数据是正确的。所有其他功能都正常工作。

int main () {

FILE *inFile;
FILE *outFile;
int n = 0;

inFile = fopen("in.txt", "r");

if (inFile == NULL) {
printf("File failed to open");
}
outFile = fopen("out.txt", "w");

if (outFile == NULL) {
printf("outFile failed to open");
}

fscanf(inFile, "%d", &n);

struct Point* arr;
arr = (struct Point*) malloc (n * sizeof(struct Point));
int i = 0;


while (i<n){
fscanf(inFile, "%d %d", &arr[i].x, &arr[i].y);
i++;
}

mergeSort(arr, 0, n-1);

fprintf(outFile, "%d\n", n);

for (int i = 0; i<n; i++){
printf("%d %d\n", arr[i].x, arr[i].y);
}

for (int i = 0; i<n; i++){
fprintf(outFile, "%d %d\n", arr[i].x, arr[i].y);
}

printf("\nSorted and output written to File\n");

int searchx, searchy;

printf("Please enter a point to search for: ");
scanf("%d %d", &searchx, &searchy);

int result = 0;

result = binarySearch(arr, searchx, searchy,0, n);

if (result == -1) {
printf("Point not found");
return 0;
}
else {
printf("Point found in element %d", result);
}
fclose(inFile);
fclose(outFile);
}

样本输入的预期结果。在.txt中:

5
6 3
4 6
2 5
2 7
5 2

输出.txt:

5
2 5
2 7
4 6
5 2
6 3

实际结果:输出.txt:

//Blank

最佳答案

该代码在我的 Mac 上运行良好,至少在 mergeSort 被注释掉的情况下。你不是在 Windows 上运行,是吗?如果是这样,文件可能看起来是空的,因为它缺少回车符。

关于c - Fprintf 没有将数据打印到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55502742/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com