C# 텍스트파일 마지막줄의 EOF 처리

mauri의 이미지

안녕하세요.
혹시 C#해보신분들께 조언좀 부탁드립니다.

지금 C#으로 텍스트 파일 처리 프로그램을 만들고 있는데요.
다음과 같이 EOF의 위치가 다른 파일2개가 있다고 가정할 경우..

(파일1)
AAA
BBB[EOF]

(파일2)
AAA
BBB
[EOF]

C#의 ReadLine()으로 처리하면 파일1, 파일2 둘다 2줄만 있는걸로 인식한다는 것입니다. ㅡ0ㅡ)!

지금 파일을 한줄 읽고 -> 변환처리 -> 다른 이름으로 출력하고 있기 때문에..
둘째줄 BBB 다음에 개행문자가 있느냐 EOF가 있느냐를 처리를 해 줘야 하는데요.

파일 스트림 이동시키거나 버퍼 읽어서 찾는건 흠좀 귀찮을듯 싶습니다.;;

이거 간단하게 해결할 수 있는 메소드나 방법 아시는 분은 조언 부탁드립니다..

HDNua의 이미지

1. 파일 스트림 이동시켜서 찾았는데, 그렇게 귀찮아보이지 않아서 그냥 답변 남깁니다. 원하시는 답변이 아닐 수도 있겠네요.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
using System.IO;
 
namespace LineCount
{
    class Program
    {
        static bool IsFileEndNewLine(FileInfo file)
        {
            FileStream fs = file.Open(FileMode.Open);
            fs.Position = fs.Length - 1;
            int b = fs.ReadByte();
            fs.Close();
            return (b == (int)'\n');
        }
 
        static void Main(string[] args)
        {
            FileInfo file = new FileInfo("file.txt");
            StreamReader sr = file.OpenText();
            string line;
            while (sr.EndOfStream == false)
            {
                line = sr.ReadLine();
                Console.WriteLine(line);
            }
            sr.Close();
            Console.WriteLine(IsFileEndNewLine(file));
        }
    }
}

저는 이렇게 생각했습니다.

mauri의 이미지

한 30분 뒤져봤는데, 역시 스트림 이동시키는 방법밖에 없는것 같네요.
더 뒤지느니 그냥 만들겠다 싶었는데, 이렇게 직접 만들어 주셔서 감사드립니다..m(__)m

버퍼에 읽어들일 생각이었는데, 님이 작성해주신 방법이 훨씬더 효율적이겠네요.
감사합니다~!!!

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.