c - 没有任何内容被写入 outfile
问题描述
1. // Libraries
2. #include <stdio.h>
3.
4. // Function Declarations
5.
6. int getData (FILE* inData, int* region, int* numFire, int* numAcres);
7. int writeReport (FILE* outReport, int region, int avrg, int numFire, int numAcres);
8. void calcAvrg (int numAcres, int numFire, int* avrg);
9.
10. int main (void)
11. {
13. // Local Declarations
14. FILE* inData;
15. FILE* outReport;
15.
16. int region;
17. int numFire;
18. int sumFire;
19. int numAcres;
20. int sumAcres;
21. int avrg; // Average acres per fire
22.
23. // Statements
24. printf("Beginning fire report\n");
25. if (!(inData = fopen ("fire_data.txt", "r")))
26. {
27. printf("\aError opening fire file\n");
28. return 100;
29. } // if open input
30.
31. if (!(outReport = fopen ("fire_report.txt", "w")))
32. {
33. printf("\aError opening fire report\n");
34. return 102;
35. } // if open output
36.
37. fprintf(outReport, "| Region | Number of Fires | Number of Acres| Average Acres per Fire |");
38.
39. while (getData (inData, ®ion, &numFire, &numAcres))
40. {
41. calcAvrg (numAcres, numFire, &avrg);
42. writeReport (outReport, region, avrg, numFire, numAcres);
43.
44. sumFire += numFire;
45. sumAcres += numAcres;
46. } // while
47.
48. fprintf(outReport, "\n\n| Summary |\n");
49. fprintf(outReport, "\nTotal Number of Fires: %d", sumFire);
50. fprintf(outReport, "\nTotal Number of Acres Burned: %d", sumAcres);
51.
52. fclose (inData);
53. fclose (outReport);
54.
55. printf("Ending fire report\n");
56. return 0;
57. } // main
58.
59. // Function Definitions
60.
61. int getData (FILE* inData, int* region, int* numFire,
62. int* numAcres)
63. {
64. // Local Declarations
65. int ioResult;
66.
67. // Statements
68. ioResult = fscanf(inData, "%d%d%d", region, numFire, numAcres);
69. if (ioResult == EOF)
70. return 0;
71. else if (ioResult != 3)
72. {
73. printf("\aError reading data\n");
74. return 0;
75. } // if
76. else
77. return 0;
78. } // getData
79.
80. void calcAvrg (int numAcres, int numFire, int* avrg)
81. {
82. // Statements
83. *avrg = (numAcres / numFire);
84. return;
85. } // calcAvrg
86.
87. int writeReport (FILE* outReport, int region, int avrg, int numFire, int numAcres)
88. {
89. // Statements
90. fprintf(outReport, "%d %d %d %d\n", region, numFire, numAcres, avrg);
91. return 0;
92. } // writeReport
[1]:https : //i.stack.imgur.com/HmGRL.png 这是fire_data.txt的图片
该程序应该读取 fire_data.text 并创建火灾报告,然后将其存储在 fire_report.text 中。fire_report.txt 应包含区域编号、火灾数量、英亩数和每次火灾的平均英亩数。
当我运行程序并打开 fire_report.txt 时,除了第 37、48、49 和 50 行之外,没有任何内容被写入文件。我想知道是否可以通过 writeReport 函数获得帮助,看看是否是问题所在或别的东西。
解决方案
您编写的 getData 函数对您从中读取的文件的内容有点敏感。fscanf 因难以使用而臭名昭著。相反,为了使它更健壮地使用
fgets
从文件中读取一行,然后使用sscanf
从缓冲区 alt 中读取单个值。使用 strtok 来挑选内容。
如果成功,您可能还希望返回 1,而不是两种情况下都返回 0。
int getData(FILE* inData, int* region, int* numFire, int* numAcres)
{
char buffer[256];
if (fgets(buffer, sizeof(buffer), inData) != NULL)
{
if (sscanf(buffer, "%d%d%d", region, numFire, numAcres) == 3)
{
return 1;
}
}
return 0;
}
免责声明:假设您的数据是逐行的,您不会显示它的外观,所以我只是在猜测。如果它是一系列三元组之间没有任何分隔符,那么您将必须有一些其他机制来从文件中读取。
推荐阅读
- java - 使用 Hibernate 但具有不同列的多个连接
- reactjs - 使用 react-router-dom 在组件基础上键入 location.state 属性
- mysql - MySQL - 单元格中的数组
- javascript - 手机摄像头对比浏览器摄像头画质
- reactjs - React TypeScript:动态路径的正确类型
- intellij-idea - 在打开 PhpStorm 的情况下从命令行运行代码格式化程序
- c# - 传递给视图的错误模型项属于“x”类型,但此字典需要“y”类型的模型项
- pandas - 如何将 pandas DataFrames 的字典连接成一个单一的 DataFrame?
- android - 无法从 Bloc Flutter 中听到新的价值
- .net-core - PublishSingleFile 选项如何处理旧版本的应用程序?