質問編集履歴

2

コードの修正をしました。

2021/12/08 06:55

投稿

sawayakaunagi
sawayakaunagi

スコア17

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- ファイルを読み込み、文字数と行数をカウントするプログラムを作成したのですが、実行に数字が表示されませんが問題でしょうか?
1
+ ファイルを読み込み、文字数と行数をカウントするプログラムを作成したのですが、実行すると、out.txt表示されるのですが、文字数が0となっていしたなぜうなるのでしょうか?
2
2
 
3
3
  ```c
4
4
 
@@ -36,7 +36,7 @@
36
36
 
37
37
  }
38
38
 
39
- if( fgets(str, 2048, fp) != NULL )
39
+ while( fgets(str, 2048, fp) != NULL )
40
40
 
41
41
  {
42
42
 
@@ -44,11 +44,11 @@
44
44
 
45
45
  }
46
46
 
47
- if( fgetc(fp) != EOF )
47
+ while(( c = fgetc(fp)) != EOF )
48
48
 
49
49
  {
50
50
 
51
- word ++;
51
+ word ++;
52
52
 
53
53
  }
54
54
 
@@ -56,13 +56,11 @@
56
56
 
57
57
 
58
58
 
59
- FILE *fp2;
59
+ fp = fopen("out.txt", "w");
60
60
 
61
61
 
62
62
 
63
- fp2 = fopen("out.txt", "w");
64
-
65
- if( fp2 == NULL)
63
+ if( fp == NULL)
66
64
 
67
65
  {
68
66
 
@@ -72,11 +70,11 @@
72
70
 
73
71
  }
74
72
 
75
- printf("Number of lines ="); fprintf( fp, "%d", line);
73
+ fprintf( fp, "%d\n", line);
76
74
 
77
- printf("Number of words ="); fprintf( fp, "%d", word);
75
+ fprintf( fp, "%d", word);
78
76
 
79
- fclose( fp );
77
+ fclose( fp );
80
78
 
81
79
  return 0;
82
80
 

1

list.txtの内容を追加しました。

2021/12/08 06:55

投稿

sawayakaunagi
sawayakaunagi

スコア17

test CHANGED
File without changes
test CHANGED
@@ -83,3 +83,67 @@
83
83
  }
84
84
 
85
85
  ```
86
+
87
+ ```c
88
+
89
+ list.txtの中身です。
90
+
91
+
92
+
93
+ List of countries and dependencies by population density
94
+
95
+ From Wikipedia, the free encyclopedia
96
+
97
+ This is a list of countries and dependent territories ranked by area and by population.
98
+
99
+ Area includes land and water.
100
+
101
+
102
+
103
+ Number of countries in the list: 7
104
+
105
+ Countries: Australia China India Japan Russia UK US
106
+
107
+
108
+
109
+ List of population
110
+
111
+ Countries Population
112
+
113
+ China 1400055440
114
+
115
+ India 1354969000
116
+
117
+ US 330263247
118
+
119
+ Russia 146877088
120
+
121
+ Japan 126577691
122
+
123
+ UK 66435600
124
+
125
+ Australia 25525452
126
+
127
+
128
+
129
+ List of area
130
+
131
+ Countries Area
132
+
133
+ Russia 17125242
134
+
135
+ US 9833517
136
+
137
+ China 9640821
138
+
139
+ Australia 7692024
140
+
141
+ India 3287240
142
+
143
+ Japan 377944
144
+
145
+ UK 242910
146
+
147
+
148
+
149
+ ```