0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SourceExpanderで出力ファイル名を変更する

Posted at

C#競技プログラミングでお世話になるSourceExpanderですが、規定では入力ファイルと同じフォルダのCombine.csxというファイルに出力されます。
このファイル名を変更する方法についてです。おすすめは3です。

1.オプションを使う

SourceExpander.Expander.Expand()にはoutputFilePathというオプションがあります。
この方法だと出力先はExeファイルのフォルダ(bin\Debug\net7.0\submit.txt など)になります。

SourceExpander.Expander.Expand(outputFilePath: "submit.txt");

var S = Console.ReadLine();
Console.WriteLine($"Hello, {S}!");

2.フルパス指定する

outputFilePathをフルパスで指定すれば所望のフォルダにファイルを出力することができます。

SourceExpander.Expander.Expand(outputFilePath: @"C:\Users\udop\Atcoder\ABC000\A\submit.txt");

var S = Console.ReadLine();
Console.WriteLine($"Hello, {S}!");

ただ、言わずもがなプロジェクトをコピペした場合などに、手で修正する必要があり面倒です。
また、提出するソースコードにローカル環境のパスが残るので、問題はさほどありませんが、ちょっと微妙です。

3.ラッパーをかます

ファイルパス未指定時に、ソースコードのあるフォルダにファイルが出力されるのは、[CallerFilePath]属性によってエントリポイントのフルパスを取得できているからなので、これを使って一段ラッパーをかましてあげるといい感じの動作になります。

void MyExpand(string outputFileName = "Combine.csx", [CallerFilePath] string? cfp = null) 
{
    if(cfp is null) return;
    var d = Path.GetDirectoryName(cfp);
    if(d is null) return;
    SourceExpander.Expander.Expand(outputFilePath: Path.Combine(d, outputFileName));
}
MyExpand("submit.txt");

var S = Console.ReadLine();
Console.WriteLine($"Hello, {S}!");

手打ちするには多いですが、コピペ運用なら問題はないかなと思います。

まとめ

ちょっと面倒ですが、問題なくできますよーという話でした。相対パスで出力できて悪いことはないと思うので、プルリク出してみてもいいのかな。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?