简体   繁体   English

Fprintf没有将数据打印到文件

[英]Fprintf is not printing data to the file

I am trying to get the data in my arr array to print to the file "out.txt". 我正在尝试获取我的arr数组中的数据以打印到文件“ out.txt”。 The program was able to do it before but for some reason will now not print at all and even after undoing to a previous version nothing will print. 该程序以前可以执行此操作,但是由于某种原因,现在根本无法打印,即使撤消到以前的版本后也无法打印。

I have code in place that proves both that the File is successfully opened and the data is correct in the array. 我有代码来证明文件已成功打开并且数组中的数据正确。 All other functions work properly. 所有其他功能均正常运行。

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);
}

expected results with sample input. 样品输入的预期结果。 In.txt: In.txt:

5
6 3
4 6
2 5
2 7
5 2 

out.txt: out.txt:

5
2 5
2 7
4 6
5 2
6 3

Actual results: out.txt: 实际结果:out.txt:

//Blank

That code runs fine here on my Mac, at least with the mergeSort commented out. 该代码在我的Mac上可以正常运行,至少在注释了mergeSort的情况下。 You're not running on Windows, are you? 您不是在Windows上运行,是吗? If so the file may look empty because it's missing carriage returns. 如果是这样,该文件可能看起来为空,因为它缺少回车符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM