質問編集履歴
2
最後の出力がうまくできない
title
CHANGED
File without changes
|
body
CHANGED
@@ -15,10 +15,6 @@
|
|
15
15
|
となるようなコードを作りたいです。
|
16
16
|
|
17
17
|
|
18
|
-
ファイルを使って1つのテキストを配列として読み取るところまではわかるのですか
|
19
|
-
3つのテキストを読み取ることとそれを全て比較するやり方がわかりません。
|
20
|
-
ちなみにテキストを3つに分けて考えることに固執しているわけではなくて
|
21
|
-
1つのテキストに3つ分のデータを入力して考える方が簡単ならその方法でも大丈夫です。
|
22
18
|
|
23
19
|
コードを書くことが苦手なのでアドバイスをいただけないかと思っています。
|
24
20
|
よろしくお願いいたします。
|
@@ -26,53 +22,57 @@
|
|
26
22
|
|
27
23
|
追記
|
28
24
|
fgetc関数を使ってみたのですが
|
29
|
-
|
25
|
+
最後の3つのテキストを1つのマスでかけて表示させる時に、ちゃんと1か0の数字が表示されません。
|
30
|
-
|
26
|
+
fgetcをintに変換する方法を教えてください
|
27
|
+
(atoi関数を使ってみてもダメでした)
|
31
28
|
|
32
29
|
|
33
30
|
```c言語
|
34
|
-
|
35
31
|
#include <stdio.h>
|
36
32
|
#include <stdlib.h>
|
37
33
|
|
38
34
|
int main(void) {
|
39
35
|
FILE *fp; // FILE型構造体
|
40
|
-
|
36
|
+
static char fname1[] = "red.txt";
|
41
|
-
char fname2[]="green.txt";
|
37
|
+
static char fname2[]="green.txt";
|
42
|
-
char fname3[]="blue.txt";
|
38
|
+
static char fname3[]="blue.txt";
|
43
39
|
char str[16];
|
44
40
|
int f1, f2, f3, f4, f5;
|
45
41
|
int l[25];
|
46
42
|
int m[25];
|
47
43
|
int n[25];
|
48
|
-
int i
|
44
|
+
int i,a,b,seki;
|
49
45
|
|
50
|
-
fp = fopen(fname1, "r"); // ファイルを開く。失敗するとNULLを返す。
|
51
|
-
if(fp == NULL) {
|
52
|
-
printf("%s file not open!\n", fname1);
|
53
|
-
return -1;
|
54
|
-
}
|
55
46
|
|
56
|
-
while((l[i]=fgetc(fp)) != EOF) {
|
57
|
-
putchar(l[i]);
|
58
|
-
i++;
|
59
47
|
|
48
|
+
fp = fopen(fname1, "r"); // ファイルを開く。失敗するとNULLを返す。
|
49
|
+
if(fp == NULL) {
|
50
|
+
printf("%s file not open!\n", fname1);
|
51
|
+
return -1;
|
60
|
-
|
52
|
+
}
|
61
|
-
fclose(fp); // ファイルを閉じる
|
62
53
|
|
54
|
+
while((l[i]=fgetc(fp)) != EOF) {
|
55
|
+
i++;
|
63
56
|
|
57
|
+
}
|
58
|
+
printf("\n");
|
59
|
+
fclose(fp); // ファイルを閉じる
|
64
60
|
|
65
61
|
|
62
|
+
|
63
|
+
|
66
64
|
fp = fopen(fname2, "r"); // ファイルを開く。失敗するとNULLを返す。
|
67
65
|
if(fp == NULL) {
|
68
66
|
printf("%s file not open!\n", fname2);
|
69
67
|
return -1;
|
70
68
|
}
|
69
|
+
i=0;
|
71
70
|
while((m[i]=fgetc(fp)) != EOF) {
|
72
|
-
putchar(m[i]);
|
73
|
-
i++;
|
74
71
|
|
72
|
+
i++;
|
73
|
+
|
75
74
|
}
|
75
|
+
printf("\n");
|
76
76
|
fclose(fp); // ファイルを閉じる
|
77
77
|
|
78
78
|
|
@@ -84,16 +84,43 @@
|
|
84
84
|
printf("%s file not open!\n", fname3);
|
85
85
|
return -1;
|
86
86
|
}
|
87
|
+
i=0;
|
87
88
|
while((n[i]=fgetc(fp)) != EOF) {
|
88
|
-
|
89
|
+
|
90
|
+
|
89
91
|
i++;
|
90
92
|
|
91
93
|
}
|
94
|
+
printf("\n");
|
92
95
|
|
93
|
-
fclose(fp); // ファイルを閉じる
|
94
96
|
|
95
|
-
|
97
|
+
fclose(fp); // ファイルを閉じる
|
96
98
|
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
i=0;
|
103
|
+
|
104
|
+
for(a=0;a<5;a++){
|
105
|
+
|
106
|
+
for(b=0;b<5;b++){
|
107
|
+
|
108
|
+
seki=l[i]*m[i]*n[i];
|
109
|
+
printf("%c ",seki);
|
110
|
+
|
111
|
+
// printf("%c ",l[i]); 試しにl[i]を表示させてみてもダメ
|
112
|
+
|
113
|
+
i++;
|
114
|
+
}
|
115
|
+
printf("\n");
|
116
|
+
}
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
return 0;
|
97
121
|
}
|
98
122
|
|
123
|
+
|
124
|
+
|
125
|
+
|
99
126
|
```
|
1
コードの変更 fgetc関数 3つのテキストの比較がわからない
title
CHANGED
File without changes
|
body
CHANGED
@@ -24,31 +24,76 @@
|
|
24
24
|
よろしくお願いいたします。
|
25
25
|
|
26
26
|
|
27
|
+
追記
|
28
|
+
fgetc関数を使ってみたのですが
|
29
|
+
2個目のテキストからファイルが開けませんとエラーで出るようになってしまいました。
|
30
|
+
ポインタと構造体が正直よくわかってないのですが、アドバイスお願いします
|
31
|
+
|
32
|
+
|
27
33
|
```c言語
|
28
34
|
|
29
|
-
|
30
35
|
#include <stdio.h>
|
31
36
|
#include <stdlib.h>
|
32
37
|
|
33
38
|
int main(void) {
|
34
|
-
FILE *fp; // FILE型構造体
|
39
|
+
FILE *fp; // FILE型構造体
|
35
|
-
char
|
40
|
+
char fname1[] = "red.txt";
|
41
|
+
char fname2[]="green.txt";
|
42
|
+
char fname3[]="blue.txt";
|
36
43
|
char str[16];
|
37
|
-
|
44
|
+
int f1, f2, f3, f4, f5;
|
45
|
+
int l[25];
|
46
|
+
int m[25];
|
47
|
+
int n[25];
|
48
|
+
int i=0;
|
38
49
|
|
39
|
-
fp = fopen(
|
50
|
+
fp = fopen(fname1, "r"); // ファイルを開く。失敗するとNULLを返す。
|
40
51
|
if(fp == NULL) {
|
41
|
-
printf("%s file not open!\n",
|
52
|
+
printf("%s file not open!\n", fname1);
|
42
53
|
return -1;
|
43
54
|
}
|
44
55
|
|
56
|
+
while((l[i]=fgetc(fp)) != EOF) {
|
57
|
+
putchar(l[i]);
|
58
|
+
i++;
|
59
|
+
|
60
|
+
}
|
61
|
+
fclose(fp); // ファイルを閉じる
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
|
45
|
-
|
66
|
+
fp = fopen(fname2, "r"); // ファイルを開く。失敗するとNULLを返す。
|
67
|
+
if(fp == NULL) {
|
46
|
-
printf("%s
|
68
|
+
printf("%s file not open!\n", fname2);
|
69
|
+
return -1;
|
47
70
|
}
|
71
|
+
while((m[i]=fgetc(fp)) != EOF) {
|
72
|
+
putchar(m[i]);
|
73
|
+
i++;
|
48
74
|
|
75
|
+
}
|
49
|
-
fclose(fp); // ファイルを閉じる
|
76
|
+
fclose(fp); // ファイルを閉じる
|
50
77
|
|
51
|
-
return 0;
|
52
78
|
|
53
79
|
|
80
|
+
|
81
|
+
|
82
|
+
fp = fopen(fname3, "r"); // ファイルを開く。失敗するとNULLを返す。
|
83
|
+
if(fp == NULL) {
|
84
|
+
printf("%s file not open!\n", fname3);
|
85
|
+
return -1;
|
86
|
+
}
|
87
|
+
while((n[i]=fgetc(fp)) != EOF) {
|
88
|
+
putchar(n[i]);
|
89
|
+
i++;
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
fclose(fp); // ファイルを閉じる
|
94
|
+
|
95
|
+
return 0;
|
96
|
+
|
97
|
+
}
|
98
|
+
|
54
99
|
```
|