简体   繁体   English

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

[英]Reading and writing using fscanf and fprintf in a while loop

EDIT: With the help of members here I have updated my code. 编辑:在这里的成员的帮助下,我已经更新了我的代码。 I am currently trying to solve why the while loop never detects the EOF. 我目前正在尝试解决为什么while循环永远无法检测到EOF。

I don't need an increment counter for [i] in my function returnStats(). 我的函数returnStats()中不需要[i]的增量计数器。 I found this interesting and I can't seem to figure out why. 我发现这很有趣,但我似乎无法弄清楚为什么。

Specifically in the while loop which scans until the End of file. 特别是在while循环中,该循环扫描到文件结束。 How does the program know to move to the next element of the array? 程序如何知道要移到数组的下一个元素?

Also, since i is no longer a counter I can't use it to keep count so I used j along with j++. 另外,由于我不再是计数器,因此无法使用它来保持计数,因此我将j与j ++一起使用。

My program details are as follows: 我的程序详细信息如下:

#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

Code Re-work 代码返工

#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

you do not need to increment the position, bacause you dont need array for this. 您不需要增加位置,因为您不需要为此的数组。 Your program uses just the first item of the array. 您的程序仅使用数组的第一项。 So it replaces old value by new one prom the file. 因此它用新的prom文件替换了旧的值。 But you have just last value in the memory. 但是,您的内存中只有最后一个值。

Simple solution if it doesn't harm is to append some string at the end of output in child/client process like “\\nend of output\\n”. 一个简单的解决方案是在子进程/客户端进程的输出末尾附加一些字符串,例如“ \\ nend output \\ n”。 I am not sure if fflush() can help or just give it a try 我不确定fflush()是否可以提供帮助或只是尝试一下

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

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