0

if/else 句で Textwriter オブジェクトをインスタンス化すると、句の外では表示されません。ここで何ができますか?新しいファイルに追加または書き込みたい。

コードは次のとおりです。

     private static void eventfull_doing()
    {
        string path, line;
        int countLines;
        Console.Write("Enter the name(with extension) of the file in the \"data\" folder, or create a new name(with extension): ");
        path = Console.ReadLine();
        TextReader inFile = new StreamReader(@"C:\data\" + path);
        Console.Write("How many lines of text do you want to add to the file?: ");
        countLines = Convert.ToInt32(Console.ReadLine());
        if (inFile.Peek() == -1 || inFile == null)
        {
            TextWriter outFile = File.AppendText(@"C:\data\" + path);
        }
        else
        {
            TextWriter outFile = new StreamWriter(@"C:\data\" + path);
        }
        for (int i = 0; i < countLines; i++)
        {
            Console.Write("Enter line: ");
            line = Console.ReadLine();
            outFile.
    }

最初のスニペットで、if 条件が意図したものではないことに注意してください。

    string path, line;
        int countLines;
        Console.Write("Enter the name(with extension) of the file in the \"data\" folder, or create a new name(with extension): ");
        path = Console.ReadLine();
        string file = Path.Combine(@"c:\data", path);
        Console.Write("How many lines of text do you want to add to the file?: ");
        countLines = Convert.ToInt32(Console.ReadLine());
        TextWriter outFile = null;
        if (File.Exists(file))
        {
            using (outFile = File.AppendText(file))
            {
                for (int i = 0; i < countLines; i++)
                {
                    Console.Write("Enter line: ");
                    line = Console.ReadLine();
                    outFile.WriteLine(line);
                }
            }
        }
        else
        {
            using (outFile = new StreamWriter(file))
            {
                for (int i = 0; i < countLines; i++)
                {
                    Console.Write("Enter line: ");
                    line = Console.ReadLine();
                    outFile.WriteLine(line);
                }
            }
        }

    }
4

3 に答える 3

5

ステートメントの前に宣言できますがif、割り当てはありません。その後、両方のブランチで値を割り当てるようにしてください。

TextWriter outFile;
if (inFile.Peek() == -1 || inFile == null)
{
    outFile = File.AppendText(@"C:\data\" + path);
}
else
{
    outFile = new StreamWriter(@"C:\data\" + path);
}

ただし、ファイルが存在する場合でもファイルを開いたままにしておくと、問題が発生する可能性があります。また、ファイルがFile.AppendText空であることがわかった場合に、なぜ呼び出しているのかは明確ではありません。実際に作成または追加しようとしているだけですか?その場合は、そのまま使用AppendTextしてください。問題ありません。

また、ステートメントを使用しusingてライターを自動的に閉じる必要があります...そして、いくつかの場所で使用する必要がある場合は@"C:\data\" + path、その共通の式をローカル変数に抽出します。

string file = Path.Combine(@"c:\data", path);
// Now use file everywhere

またはコンストラクターの使用に固執する場合は、条件式の使用を検討してください。File.AppendTextStreamWriter

TextWriter outFile = inFile.Peek() == -1 || inFile == null
    ? File.AppendText(file) : new StreamWriter(file);
于 2013-11-09T08:31:28.280 に答える
3

それは設計によるものです。次のようなものを書く必要があります。

TextWriter outFile = null; 
if (inFile.Peek() == -1 || inFile == null)
{
    outFile = File.AppendText(@"C:\data\" + path);
}
else
{
    outFile = new StreamWriter(@"C:\data\" + path);
}

outfile への代入はnull、このコンテキストではオプションですが、いつか C++ でコーディングを開始すると、それが良い習慣であることがわかります。

于 2013-11-09T08:32:08.653 に答える
1

外側で宣言し、内側でインスタンス化してください , これは通常の動作です . コード ブロック内でのみ使用できる変数をコード ブロック内で宣言する場合 , CodeBlock の外側で変数を宣言し、インスタンス化するか、内部で何かを割り当ててくださいif elseコードブロック

違う

if ( condition ) 
{
 string s = "J" ;  
}
MessageBox.Show(s) 

正しい

string s ; 
if ( condition ) 
{ 
  s = "jo";
} 
MessageBox.Show(s) ;
于 2013-11-09T08:36:12.377 に答える