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

質問編集履歴

2

誤字

2016/07/08 08:14

投稿

soromon
soromon

スコア15

title CHANGED
File without changes
body CHANGED
@@ -75,4 +75,5 @@
75
75
 
76
76
 
77
77
  ###補足情報
78
- このプログラムは全てlinux上で作っています。
78
+ このプログラムは全てlinux上で作っています。
79
+ まだまだ至らないとこばかりですがご協力お願いいたします。

1

誤字

2016/07/08 08:14

投稿

soromon
soromon

スコア15

title CHANGED
File without changes
body CHANGED
@@ -9,51 +9,55 @@
9
9
  ATOM 166 HO2' G 5 1.286 42.680 32.880 1.00 0.00 H
10
10
  ATOM 167 G34' G 5 2.363 44.078 32.398 1.00 0.00 H
11
11
  ```エラーメッセージ
12
- その1.ファイルができるが全nullで返ってしまう
12
+ 置換されいない
13
- その2.test7.c: In function ‘permute_str’:
14
- test7.c:49: warning: return makes pointer from integer without a cast
15
13
  ```
16
14
 
17
15
  ###C言語
16
+ #include <stdio.h>
17
+ #include <string.h>
18
18
 
19
- #include<stdio.h>
20
- #include<string.h>
19
+ const int BUFSIZE = 512;
20
+ const int PATH_MAX = 4016;
21
- char *permute_str(const char *a, const char *b, const char *c, int column);
21
+ const char *permute_str(const char *source, const char *target, const char *replace, int column);
22
+
22
- int main (void)
23
+ int main(void)
23
24
  {
24
- FILE *fp_i,*fp_o;
25
+ FILE *fp_in, *fp_out;
25
- char *res;
26
- char a[100000],fname[256],fname2[256],sstr[100],cstr[100];
26
+ char fname_in[PATH_MAX], fname_out[PATH_MAX];
27
+ char buf[BUFSIZE], target[BUFSIZE], replace[BUFSIZE];
27
- printf("検索されるファイル名 : ");
28
+ printf("検索されるファイル名 : ");
28
- scanf("%s",fname);
29
+ scanf("%s", fname_in);
29
- printf("出力先のファイル名 : ");
30
+ printf("出力先のファイル名 : ");
30
- scanf("%s",fname2);
31
+ scanf("%s", fname_out);
31
- printf("置換前の文字列 : ");
32
+ printf("置換前の文字列 : ");
32
- scanf("%s",sstr);
33
+ scanf("%s", target);
33
- printf("置換後の文字列 : ");
34
+ printf("置換後の文字列 : ");
34
- scanf("%s",cstr);
35
+ scanf("%s", replace);
35
36
 
36
- if( (fp_i=fopen(fname,"r"))==NULL){
37
+ if ((fp_in = fopen(fname_in, "r")) == NULL) {
37
- printf("ファイル %s が見つかりません\n",fname);return;
38
+ printf("ファイル %s が見つかりません\n", fname_in);
39
+ return 0;
38
40
  }
39
- else{
41
+
40
- fp_o=fopen(fname2,"w");
42
+ fp_out = fopen(fname_out, "w");
43
+
44
+ while (fgets(buf, BUFSIZE, fp_in) != NULL) {
45
+ const char *res = permute_str(buf, target, replace, 3);
46
+ fprintf(fp_out, "%s", res);
41
- }
47
+ }
42
- while(fgets(a,1000,fp_i)!=NULL){
43
- res = permute_str(a, sstr, cstr,3);
44
- fprintf(fp_o,"%s\n",res);
45
- }
46
- fclose(fp_i);
48
+ fclose(fp_in);
47
- fclose(fp_o);
49
+ fclose(fp_out);
50
+
48
- return 0;
51
+ return 0;
49
- }
52
+
53
+
50
- char *permute_str(const char *a, const char *b, const char *c, int column)
54
+ const char *permute_str(const char *source, const char *target, const char *replace, int column)
51
55
  {
52
- char buf[1000];
56
+ char buf[1000];
53
57
  char *str;
54
58
  char *tok;
55
59
  int count = 0;
56
- strcpy(buf,a);
60
+ strcpy(buf,source);
57
61
  tok =strtok(buf," \t\r\n");
58
62
  if(tok == NULL)
59
63
  return 0;
@@ -61,17 +65,14 @@
61
65
  {
62
66
  str = tok;
63
67
  if(++count == column)
64
- break;
68
+ break;
65
69
  tok = strtok(NULL," \t\r\n");
66
70
  }
67
- return 0;//その1
71
+ return source;
68
- return(strcmp(str, b) == 0) ? 1 : 0;//その2
72
+ }
69
73
  ```
70
74
 
71
- ###試したこと
72
- その1を試すとコンパイルはされますが例としてG34'を検索し適当な文字を打つと全てnullで返されたファイルができてしまいます。
73
- その2では上記のようなエラーが出てしまうのでstrcmpを用いて文字列を比較していないことが原因だと思われます。
74
- 戻り値が詳しくわからないのでどうかご協力お願いいたします。
75
75
 
76
+
76
77
  ###補足情報
77
78
  このプログラムは全てlinux上で作っています。