#1
by fabersid » 6年前
printf("%s",tempname);と出力されたファイル名が一致しない
どこを修正すると不具合が直るか教えて下さい。
コード:
#include <stdio.h> // fopen, fclose, fgets, printf, sscanf, sprintf, remove
#include <stdlib.h> // system
#include <string.h> // strstr, strchr
#define N 256
void find_line(const char *);
void strchg(char *, const char *, const char *);
char *filename = "readme.txt";
char tempname[16];
char buf[1024*1024];
int main(void){
char readline[N];
FILE *fp = fopen(filename, "r");
if (!fp) { fprintf(stderr, "can't open %s\n", filename); return 1; }
tmpnam(tempname);
if(system("ping translate.google.co.jp>nul")!=0){
printf("通信エラー:Google 翻訳に接続できません。\n");
return 1;
}
while (fgets(readline, N, fp)) {
char word[N + 100], cmd[N + 300], *p = word, *q = readline, c;
while ((c = *q++) != '\0' && c != '\n')
if (c == '&' || c == '"' || c == '#') p += sprintf(p, "%%%02X", c);
else *p++ = c;
*p = '\0';
sprintf(cmd, "powershell -command wget \"\"\"https:"
"//translate.google.co.jp/?oe=cp932&text=%s\"\"\""
" -outfile %s", word, tempname);
system(cmd);
find_line("<span id=result_box");
}
fclose(fp);
//remove(tempname);
return 0;
}
void find_line(const char *str)
{
FILE *fp = fopen(tempname, "r");
if (!fp) return;
while (fgets(buf, sizeof buf, fp)) {
char *p = strstr(buf, str);
if (p) {
p = strchr(strchr(p, '>') + 1, '>') + 1;
*strchr(p, '<') = '\0';
strchg(p,"&","&");
strchg(p,""","\"");
strchg(p,"'","'");
/*printf(" -> [%s]\n", p);*/printf("%s\n", p);
}
}
fclose(fp);
}
void strchg(char *buf, const char *str1, const char *str2){
//https://www.grapecity.com/tools/support/powernews/column/clang/049/page03.htmより
char tmp[1024 + 1];
char *p;
while ((p = strstr(buf, str1)) != NULL) {
/* 見つからなくなるまで繰り返す
pは旧文字列の先頭を指している */
*p = '\0'; /* 元の文字列を旧文字列の直前で区切って */
p += strlen(str1); /* ポインタを旧文字列の次の文字へ */
strcpy(tmp, p); /* 旧文字列から後を保存 */
strcat(buf, str2); /* 新文字列をその後につなぎ */
strcat(buf, tmp); /* さらに残りをつなぐ */
}
}
printf("%s",tempname);と出力されたファイル名が一致しない
どこを修正すると不具合が直るか教えて下さい。
[code=c]#include <stdio.h> // fopen, fclose, fgets, printf, sscanf, sprintf, remove
#include <stdlib.h> // system
#include <string.h> // strstr, strchr
#define N 256
void find_line(const char *);
void strchg(char *, const char *, const char *);
char *filename = "readme.txt";
char tempname[16];
char buf[1024*1024];
int main(void){
char readline[N];
FILE *fp = fopen(filename, "r");
if (!fp) { fprintf(stderr, "can't open %s\n", filename); return 1; }
tmpnam(tempname);
if(system("ping translate.google.co.jp>nul")!=0){
printf("通信エラー:Google 翻訳に接続できません。\n");
return 1;
}
while (fgets(readline, N, fp)) {
char word[N + 100], cmd[N + 300], *p = word, *q = readline, c;
while ((c = *q++) != '\0' && c != '\n')
if (c == '&' || c == '"' || c == '#') p += sprintf(p, "%%%02X", c);
else *p++ = c;
*p = '\0';
sprintf(cmd, "powershell -command wget \"\"\"https:"
"//translate.google.co.jp/?oe=cp932&text=%s\"\"\""
" -outfile %s", word, tempname);
system(cmd);
find_line("<span id=result_box");
}
fclose(fp);
//remove(tempname);
return 0;
}
void find_line(const char *str)
{
FILE *fp = fopen(tempname, "r");
if (!fp) return;
while (fgets(buf, sizeof buf, fp)) {
char *p = strstr(buf, str);
if (p) {
p = strchr(strchr(p, '>') + 1, '>') + 1;
*strchr(p, '<') = '\0';
strchg(p,"&","&");
strchg(p,""","\"");
strchg(p,"'","'");
/*printf(" -> [%s]\n", p);*/printf("%s\n", p);
}
}
fclose(fp);
}
void strchg(char *buf, const char *str1, const char *str2){
//https://www.grapecity.com/tools/support/powernews/column/clang/049/page03.htmより
char tmp[1024 + 1];
char *p;
while ((p = strstr(buf, str1)) != NULL) {
/* 見つからなくなるまで繰り返す
pは旧文字列の先頭を指している */
*p = '\0'; /* 元の文字列を旧文字列の直前で区切って */
p += strlen(str1); /* ポインタを旧文字列の次の文字へ */
strcpy(tmp, p); /* 旧文字列から後を保存 */
strcat(buf, str2); /* 新文字列をその後につなぎ */
strcat(buf, tmp); /* さらに残りをつなぐ */
}
}[/code]
[offtopic]http://dixq.net/forum/viewtopic.php?f=3&t=20011
の別トピックとして投稿された質問です。[/offtopic]