質問編集履歴

2

誤字

2016/07/08 08:14

投稿

soromon
soromon

スコア15

test CHANGED
File without changes
test CHANGED
@@ -153,3 +153,5 @@
153
153
  ###補足情報
154
154
 
155
155
  このプログラムは全てlinux上で作っています。
156
+
157
+ まだまだ至らないとこばかりですがご協力お願いいたします。

1

誤字

2016/07/08 08:14

投稿

soromon
soromon

スコア15

test CHANGED
File without changes
test CHANGED
@@ -20,11 +20,7 @@
20
20
 
21
21
  ```エラーメッセージ
22
22
 
23
- その1.ファイルができるが全nullで返ってしまう
23
+ 置換されいない
24
-
25
- その2.test7.c: In function ‘permute_str’:
26
-
27
- test7.c:49: warning: return makes pointer from integer without a cast
28
24
 
29
25
  ```
30
26
 
@@ -32,75 +28,87 @@
32
28
 
33
29
  ###C言語
34
30
 
31
+ #include <stdio.h>
32
+
33
+ #include <string.h>
35
34
 
36
35
 
37
- #include<stdio.h>
38
36
 
39
- #include<string.h>
37
+ const int BUFSIZE = 512;
40
38
 
41
- char *permute_str(const char *a, const char *b, const char *c, int column);
39
+ const int PATH_MAX = 4016;
42
40
 
41
+ const char *permute_str(const char *source, const char *target, const char *replace, int column);
42
+
43
+
44
+
43
- int main (void)
45
+ int main(void)
44
46
 
45
47
  {
46
48
 
47
- FILE *fp_i,*fp_o;
49
+ FILE *fp_in, *fp_out;
48
50
 
49
- char *res;
51
+ char fname_in[PATH_MAX], fname_out[PATH_MAX];
50
52
 
51
- char a[100000],fname[256],fname2[256],sstr[100],cstr[100];
53
+ char buf[BUFSIZE], target[BUFSIZE], replace[BUFSIZE];
52
54
 
53
- printf("検索されるファイル名 : ");
55
+ printf("検索されるファイル名 : ");
54
56
 
55
- scanf("%s",fname);
57
+ scanf("%s", fname_in);
56
58
 
57
- printf("出力先のファイル名 : ");
59
+ printf("出力先のファイル名 : ");
58
60
 
59
- scanf("%s",fname2);
61
+ scanf("%s", fname_out);
60
62
 
61
- printf("置換前の文字列 : ");
63
+ printf("置換前の文字列 : ");
62
64
 
63
- scanf("%s",sstr);
65
+ scanf("%s", target);
64
66
 
65
- printf("置換後の文字列 : ");
67
+ printf("置換後の文字列 : ");
66
68
 
67
- scanf("%s",cstr);
69
+ scanf("%s", replace);
68
70
 
69
71
 
70
72
 
71
- if( (fp_i=fopen(fname,"r"))==NULL){
73
+ if ((fp_in = fopen(fname_in, "r")) == NULL) {
72
74
 
73
- printf("ファイル %s が見つかりません\n",fname);return;
75
+ printf("ファイル %s が見つかりません\n", fname_in);
76
+
77
+ return 0;
74
78
 
75
79
  }
76
80
 
77
- else{
78
81
 
79
- fp_o=fopen(fname2,"w");
80
82
 
81
- }
83
+ fp_out = fopen(fname_out, "w");
82
84
 
83
- while(fgets(a,1000,fp_i)!=NULL){
84
85
 
85
- res = permute_str(a, sstr, cstr,3);
86
86
 
87
- fprintf(fp_o,"%s\n",res);
87
+ while (fgets(buf, BUFSIZE, fp_in) != NULL) {
88
88
 
89
- }
89
+ const char *res = permute_str(buf, target, replace, 3);
90
90
 
91
- fclose(fp_i);
91
+ fprintf(fp_out, "%s", res);
92
92
 
93
- fclose(fp_o);
93
+ }
94
94
 
95
- return 0;
95
+ fclose(fp_in);
96
96
 
97
- }
97
+ fclose(fp_out);
98
98
 
99
+
100
+
101
+ return 0;
102
+
103
+
104
+
105
+
106
+
99
- char *permute_str(const char *a, const char *b, const char *c, int column)
107
+ const char *permute_str(const char *source, const char *target, const char *replace, int column)
100
108
 
101
109
  {
102
110
 
103
- char buf[1000];
111
+ char buf[1000];
104
112
 
105
113
  char *str;
106
114
 
@@ -108,7 +116,7 @@
108
116
 
109
117
  int count = 0;
110
118
 
111
- strcpy(buf,a);
119
+ strcpy(buf,source);
112
120
 
113
121
  tok =strtok(buf," \t\r\n");
114
122
 
@@ -124,27 +132,21 @@
124
132
 
125
133
  if(++count == column)
126
134
 
127
- break;
135
+ break;
128
136
 
129
137
  tok = strtok(NULL," \t\r\n");
130
138
 
131
139
  }
132
140
 
133
- return 0;//その1
141
+ return source;
134
142
 
135
- return(strcmp(str, b) == 0) ? 1 : 0;//その2
143
+ }
136
144
 
137
145
  ```
138
146
 
139
147
 
140
148
 
141
- ###試したこと
142
149
 
143
- その1を試すとコンパイルはされますが例としてG34'を検索し適当な文字を打つと全てnullで返されたファイルができてしまいます。
144
-
145
- その2では上記のようなエラーが出てしまうのでstrcmpを用いて文字列を比較していないことが原因だと思われます。
146
-
147
- 戻り値が詳しくわからないのでどうかご協力お願いいたします。
148
150
 
149
151
 
150
152