在while循环中使用fscanf和fprintf进行读写

时间:2018-11-20 01:43:43

标签: c scanf eof

编辑:在这里的成员帮助下,我更新了代码。我目前正在尝试解决为什么while循环从未检测到EOF。

我的函数returnStats()中不需要[i]的增量计数器。我发现这很有趣,但我似乎无法弄清楚为什么。

特别是在while循环中,该循环扫描直到文件结束。程序如何知道要移到数组的下一个元素?

此外,由于我不再是计数器,因此无法使用它来保持计数,因此我将j和j ++一起使用。

我的程序详细信息如下:

#include <stdio.h>

// function prototypes
void loadTextFile();
void returnStats();

// begin main function
int main(void){

    loadTextFile();
    returnStats();

    return 0;
} // end main function

// function which returns the number of entries, average, and maximum of the numbers read
void returnStats(){

    FILE *file = fopen("question5.txt","r");

    if(file == NULL)
    {
        printf("question3.dat cannot be opened!\n");
        fprintf(stderr, "Error opening the fil!\n");
    }

    else
        printf("\nquestion3.dat was opened successfully for reading.\n\n");

    int i=0, j=0; 
    int numbers[4];
    float average=0.0;
    int max=0;

    while(fscanf(file, "%d", &numbers[i]) != EOF) 
    {
        printf("%d was read from .txt file\n", numbers[i]);
        average += numbers[i];  

        if(numbers[i]>max)
            max = numbers[i];

        j++;    

    }   

    printf("\nThe number of entries is: %d", j);    
    printf("\nThe average of the numbers is: %.2f", average/4);
    printf("\nThe maximum of the numbers is: %d", max);

    fclose(file);

} // end returnAverage


// function to load the .txt file with array values so we may execute main on any computer. 
void loadTextFile(){

    FILE *file = fopen("question5.txt","w");

    if(file == NULL)
    {
        printf("question3.dat cannot be opened!\n");
        fprintf(stderr, "Error opening the fil!\n");
    }

    else
        printf("question3.dat was opened successfully for writing.\n");

    int numberArray[4]={56, 23, 89, 30};

    int i=0;

    for(i=0;i<4;i++)
    {
        fprintf(file, "%d\n", numberArray[i]);

    }

    printf("\nThe data was successfully written\n");

    fclose(file);
} // end function loadTextFile

代码返工

#include <stdio.h>

// function prototypes
void loadTextFile(FILE *file);
void returnStats(FILE *file);

// begin main function
int main(void){

    FILE *text = fopen("question6.txt","w");

    if(text == NULL)
    {
        printf("question6.dat cannot be opened!\n");
        fprintf(stderr, "Error opening the fil!\n");
    }

    else
        printf("\nquestion6.dat was opened successfully for Writing.\n\n");

    loadTextFile(text);
    returnStats(text);

    fclose(text);

    return 0;
} // end main function

// function which returns the number of entries, average, and maximum of the numbers read
void returnStats(FILE *file){


    int i=0;
    int number[4];

    while(fscanf(file, "%d", &number[i]) != EOF)
    //for (i=0;i<4;i++)
    {   
    //  fscanf(file, "%d", &number[i]);
        printf("\n%d : Was Read from the File\n", number[i]);
        fprintf(file, "%d\n", number[i]+10);
    }

} // end returnStats

// function to load the .txt file with array values so we may execute main on any computer. 
void loadTextFile(FILE *file){


    int numberArray[4]={56, 23, 89, 30};

    int i=0;

    for(i=0;i<4;i++)
    {
        fprintf(file, "%d\n", numberArray[i]);
        printf("\n%d : was written to the file\n", numberArray[i]);

    }

    printf("\nThe data was successfully written\n");

} // end function loadTextFile

2 个答案:

答案 0 :(得分:2)

您不需要增加位置,因为您不需要数组。您的程序仅使用数组的第一项。因此它用新的prom文件替换了旧的值。但是,您的内存中只有最后一个值。

答案 1 :(得分:0)

简单的解决方案是,在子进程/客户端进程的输出末尾附加一些字符串,例如“ \ nend \ n”。 我不确定fflush()是否可以提供帮助,或者只是尝试一下