何かがおかしいtmpfile

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら

トピックに返信する


答えを正確にご入力ください。答えられるかどうかでスパムボットか否かを判定します。

BBCode: ON
[img]: ON
[flash]: OFF
[url]: ON
スマイリー: OFF

トピックのレビュー
   

展開ビュー トピックのレビュー: 何かがおかしいtmpfile

Re: 何かがおかしいtmpfile

#5

by fabersid » 6年前

かずま さんありがとうございます。

コード:

        sprintf(cmd, "powershell -command wget \"\"\"https:"
            "//translate.google.co.jp/?oe=cp932&text=%s\"\"\""
            " -outfile %s", word, tempname);
から

コード:

        sprintf(cmd, "powershell -command wget \"\"\"https:"
            "//translate.google.co.jp/?oe=cp932&text=%s\"\"\""
            " -outfile '%s'", word, tempname);
という風に '' で囲いました。
オフトピック
powershell エスケープ ドル」の
ように調べると
「"" の場合、中に $a などの変数が出てくると中身を展開します。 '' の方は $ もそのまま文字として扱います。」
と書かれた記事があったので解決しました。

Re: 何かがおかしいtmpfile

#4

by かずま » 6年前

拡張子を .$$$ でないもの、例えば .TMP に変える手もあります。

コード:

    tmpnam(tempname);
	strcpy(tempname + strlen(tempname) - 3, "TMP");

Re: 何かがおかしいtmpfile

#3

by かずま » 6年前

powershell では、$$ は一つ前のコマンドラインの最後のトークンです。

コード:

C:\tmp\hoge>powershell
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\tmp\hoge> echo 111 222 333
111
222
333
PS C:\tmp\hoge> echo $$
333
PS C:\tmp\hoge> exit

C:\tmp\hoge>
ひとつ前のコマンドラインがなければ、$$ は空になります。
$$$ はひとつの $ になります。

bcc の tmpnam() が .$$$ という拡張子のファイル名を
作るのなら、次のようにして回避してはいかがでしょうか?

コード:

    tmpnam(tempname);
    tempname[strlen(tempname) - 2] = '\0'; // ".$$$" -> ".$"
あるいは、tmpnam() を使うのをやめて、現在時刻や
乱数などから、ファイル名を作ってみてはいかがでしょうか?

関連性のある別トピックのお知らせ

#2

by fabersid » 6年前

tempfileに関する別のトピックと立てました。
何かがおかしいtmpfile

何かがおかしいtmpfile

#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);   /* さらに残りをつなぐ */
  }
}
オフトピック
http://dixq.net/forum/viewtopic.php?f=3&t=20011
の別トピックとして投稿された質問です。

ページトップ