본문 바로가기

C language

[C#] txt 파일에 쓰기


Console.Write("N[{0}][{1}]", Nodes[i][j].x, Nodes[i][j].y);

와 같이 {} 안에 숫자를 넣어주고 구분자(,)로 "" 다음에 순차적으로 {} 안에 들어갈 변수들을 써주면된다


 

실제예제코드#1 ( in C#)


실제예제코드#1에서


Path_print를 string 형의 배열로 선언해준것을 볼수 있다.

그래서 그 Path_print 에 string 값을 넣어준후

순차적으로 foreach 를 통하여 txt 파일로 내보내줄수 있다. 



    private void printNodes()
    {
            string[] Path_print = new string[Board.xCellsNum*2];
            string a;
            int temp = 1;
            for (int i = 0; i < Nodes.Count; i++)
            {
                for (int j = 0; j < Board.yCellsNum; j++)
                {
                    if (Nodes[i][j].obstacle == true) a = "○";
                    else a = "X";
                    Console.Write("N[{0}][{1}]", Nodes[i][j].x, Nodes[i][j].y);
                    Console.Write("~{0}%{1}@:{2} ⁄ ", Nodes[i][j].g, Nodes[i][j].h, Nodes[i][j].obstacle);
                    Path_print[(i + 1) * 2 - 2] = Path_print[(i + 1) * 2 - 2] + Convert.ToString("   "+Nodes[i][j].x + "," + Nodes[i][j].y + "(" + a +")|" );
                    ⁄⁄Path_print[(i + 1) * 3 - 2] = Path_print[(i + 1) * 3 - 2] + Convert.ToString("\t" + Nodes[i][j].obstacle + "|");
                    Path_print[(i + 1) * 2 - 1] = Path_print[(i + 1) * 2 - 1] + "--------";
                    temp++;
                }
                Console.WriteLine("");
            }
            
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\test\printNodes.txt", true))
            {
                foreach (string line in Path_print)
                {
                    file.WriteLine(line);
                }
            }
        }

 

실제예제코드#2 ( in C#)

 
실제예제코드#2에서

직접 file.WriteLine을 통하여 값을 써주고 for 문으로 돌려서
txt 파일안에 라인들을 직접 저장하는 것이 가능하다.

private void nodeListCheck(List<node> List)
        {
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\test\nodeListCheck.txt", true))
            {
                for (int i = 0; i < List.Count; i++)
                {
                    file.WriteLine("Listcheck{0} :{1} ⁄ {2} ⁄ {3} ⁄ {4} ⁄ {5} ", i, List[i].x, List[i].y, List[i].opened, List[i].g, List[i].h);
                }
        
            }
            Console.WriteLine(" i번째 ⁄ x ⁄ y ⁄ opened ⁄ g ⁄ h");
            ⁄⁄neighbor check@@@@
        }

 

실제예제코드#3


이 코드는 MicroSoft 에서 직접 가져온 예문으로

실제적으로 쓰이는 코드를 간단한 Example 로 표현해놓은 것이 인상적이다.


class WriteTextFile
{
    static void Main()
    {

        ⁄⁄ These examples assume a "C:\Users\Public\TestFolder" folder on your machine.
        ⁄⁄ You can modify the path if necessary. 

        ⁄⁄ Example #1: Write an array of strings to a file. 
        ⁄⁄ Create a string array that consists of three lines. 
        string[] lines = {"First line", "Second line", "Third line"};
        System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);


        ⁄⁄ Example #2: Write one string to a text file. 
        string text = "A class is the most powerful data type in C#. Like structures, " +
                       "a class defines the data and behavior of the data type. ";
        System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text);

        ⁄⁄ Example #3: Write only some strings in an array to a file. 
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt"))
        {
            foreach (string line in lines)
            {
                ⁄⁄ If the line doesn't contain the word 'Second', write the line to the file. 
                if (!line.Contains("Second"))
                {
                    file.WriteLine(line);
                }
            }
        }

        ⁄⁄ Example #4: Append new text to an existing file 
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt", true))
        {
            file.WriteLine("Fourth line");
        }  
    }
}
⁄* Output (to WriteLines.txt):
    First line
    Second line
    Third line

 Output (to WriteText.txt):
    A class is the most powerful data type in C#. Like structures, a class defines the data and behavior of the data type.

 Output to WriteLines2.txt after Example #3:
    First line
    Third line

 Output to WriteLines2.txt after Example #4:
    First line
    Third line
    Fourth line
 *⁄




'C language' 카테고리의 다른 글

[C#] txt 파일 읽기,삭제,쓰기  (0) 2013.06.09
[C#] 구조체를 다차원배열로  (0) 2013.04.19