gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:51:45 25 4
gpt4 key购买 nike

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

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

特别是在扫描文件末尾的 while 循环中。程序如何知道移动到数组的下一个元素?

此外,由于 i 不再是计数器,我无法使用它来保持计数,所以我将 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

最佳答案

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

关于c - 在 while 循环中使用 fscanf 和 fprintf 进行读写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53385076/

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