質問編集履歴

3

試したことのコードの修正

2017/12/29 14:09

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -116,15 +116,11 @@
116
116
 
117
117
  for(int i = 0; fgets(tmpline, 256, fp); ++i){
118
118
 
119
- if (tmpline != NULL){
120
-
121
119
  printf("%d : %s", i, tmpline);
122
120
 
123
121
  }
124
122
 
125
123
  }
126
-
127
- }
128
124
 
129
125
 
130
126
 

2

試したことの追記

2017/12/29 14:09

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -88,6 +88,72 @@
88
88
 
89
89
 
90
90
 
91
+ ###試したこと
92
+
93
+ 以下のようなfor文を用いたコードを実行したところ、エラーが出てしまいました。
94
+
95
+ ```c
96
+
97
+ //trial.c
98
+
99
+ #include <stdio.h>
100
+
101
+ int main(){
102
+
103
+ FILE* fp; // file pointer
104
+
105
+
106
+
107
+ char tmpline[256]; // temporary space
108
+
109
+
110
+
111
+ fp = fopen("Hello.dat", "r"); // open a file
112
+
113
+
114
+
115
+ if (fp != NULL) {
116
+
117
+ for(int i = 0; fgets(tmpline, 256, fp); ++i){
118
+
119
+ if (tmpline != NULL){
120
+
121
+ printf("%d : %s", i, tmpline);
122
+
123
+ }
124
+
125
+ }
126
+
127
+ }
128
+
129
+
130
+
131
+ fclose(fp);
132
+
133
+ return 0; }
134
+
135
+ ```
136
+
137
+
138
+
139
+ ```
140
+
141
+ % gcc -Wall trial.c -o trial
142
+
143
+ trial.c: In function 'main':
144
+
145
+ trial.c:7:5: error: 'for' loop initial declarations are only allowed in C99 mode
146
+
147
+ for(int i = 0; fgets(tmpline, 256, fp); ++i){
148
+
149
+ ^
150
+
151
+ trial.c:7:5: note: use option -std=c99 or -std=gnu99 to compile your code
152
+
153
+ ```
154
+
155
+
156
+
91
157
  ###補足情報(言語/FW/ツール等のバージョンなど)
92
158
 
93
159
  GNU Emacs 22.1.1

1

コードの修正

2017/12/29 14:06

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -62,10 +62,6 @@
62
62
 
63
63
  while (fgets(tmpline, 256, fp)){
64
64
 
65
- tmpline //
66
-
67
-
68
-
69
65
  if (tmpline != NULL){
70
66
 
71
67
  printf("%s", tmpline);