teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

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

2017/12/29 14:09

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -57,11 +57,9 @@
57
57
 
58
58
  if (fp != NULL) {
59
59
  for(int i = 0; fgets(tmpline, 256, fp); ++i){
60
- if (tmpline != NULL){
61
60
  printf("%d : %s", i, tmpline);
62
61
  }
63
62
  }
64
- }
65
63
 
66
64
  fclose(fp);
67
65
  return 0; }

2

試したことの追記

2017/12/29 14:09

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -43,5 +43,38 @@
43
43
  ファイルのデータが全て表示される
44
44
  ```
45
45
 
46
+ ###試したこと
47
+ 以下のようなfor文を用いたコードを実行したところ、エラーが出てしまいました。
48
+ ```c
49
+ //trial.c
50
+ #include <stdio.h>
51
+ int main(){
52
+ FILE* fp; // file pointer
53
+
54
+ char tmpline[256]; // temporary space
55
+
56
+ fp = fopen("Hello.dat", "r"); // open a file
57
+
58
+ if (fp != NULL) {
59
+ for(int i = 0; fgets(tmpline, 256, fp); ++i){
60
+ if (tmpline != NULL){
61
+ printf("%d : %s", i, tmpline);
62
+ }
63
+ }
64
+ }
65
+
66
+ fclose(fp);
67
+ return 0; }
68
+ ```
69
+
70
+ ```
71
+ % gcc -Wall trial.c -o trial
72
+ trial.c: In function 'main':
73
+ trial.c:7:5: error: 'for' loop initial declarations are only allowed in C99 mode
74
+ for(int i = 0; fgets(tmpline, 256, fp); ++i){
75
+ ^
76
+ trial.c:7:5: note: use option -std=c99 or -std=gnu99 to compile your code
77
+ ```
78
+
46
79
  ###補足情報(言語/FW/ツール等のバージョンなど)
47
80
  GNU Emacs 22.1.1

1

コードの修正

2017/12/29 14:06

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -30,8 +30,6 @@
30
30
  fp = fopen("Hello.dat", "r"); // open a file
31
31
  if (fp != NULL) {
32
32
  while (fgets(tmpline, 256, fp)){
33
- tmpline //
34
-
35
33
  if (tmpline != NULL){
36
34
  printf("%s", tmpline);
37
35
  }