###前提・実現したいこと
お世話になっています。
C言語でファイルを読み込んだ後、文字を検索後その文字を置換し新しいファイルを作るプログラムを作っています
現在3列目を検索し置換しようとしています。
###発生している問題・エラーメッセージ
ATOM 166 HO2' G 5 1.286 42.680 32.880 1.00 0.00 H ATOM 167 G34' G 5 2.363 44.078 32.398 1.00 0.00 H ```エラーメッセージ 置換されていない。
###C言語
#include <stdio.h>
#include <string.h>
const int BUFSIZE = 512;
const int PATH_MAX = 4016;
const char *permute_str(const char *source, const char *target, const char *replace, int column);
int main(void)
{
FILE *fp_in, *fp_out;
char fname_in[PATH_MAX], fname_out[PATH_MAX];
char buf[BUFSIZE], target[BUFSIZE], replace[BUFSIZE];
printf("検索されるファイル名 : ");
scanf("%s", fname_in);
printf("出力先のファイル名 : ");
scanf("%s", fname_out);
printf("置換前の文字列 : ");
scanf("%s", target);
printf("置換後の文字列 : ");
scanf("%s", replace);
if ((fp_in = fopen(fname_in, "r")) == NULL) { printf("ファイル %s が見つかりません\n", fname_in); return 0;
}
fp_out = fopen(fname_out, "w"); while (fgets(buf, BUFSIZE, fp_in) != NULL) { const char *res = permute_str(buf, target, replace, 3); fprintf(fp_out, "%s", res); } fclose(fp_in); fclose(fp_out); return 0;
const char *permute_str(const char *source, const char *target, const char *replace, int column)
{
char buf[1000];
char *str;
char *tok;
int count = 0;
strcpy(buf,source);
tok =strtok(buf," \t\r\n");
if(tok == NULL)
return 0;
while(tok !=NULL)
{
str = tok;
if(++count == column)
break;
tok = strtok(NULL," \t\r\n");
}
return source;
}
###補足情報 このプログラムは全てlinux上で作っています。 まだまだ至らないとこばかりですがご協力お願いいたします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/07/08 08:17