[英]Fprintf is not printing data to the file
我正在嘗試獲取我的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);
}
樣品輸入的預期結果。 In.txt:
5
6 3
4 6
2 5
2 7
5 2
out.txt:
5
2 5
2 7
4 6
5 2
6 3
實際結果:out.txt:
//Blank
該代碼在我的Mac上可以正常運行,至少在注釋了mergeSort的情況下。 您不是在Windows上運行,是嗎? 如果是這樣,該文件可能看起來為空,因為它缺少回車符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.