Reading txt with Streamreader

Asked

Viewed 226 times

0

When reading a TXT file, of accounting releases exporting by a third party system, being my problem in the lines that have the Nº.

VLR REF SOMETHING NO 1834 MORE TEXT

Using the class StreamReader as follows

using (var reader = new StreamReader(@"D:\Projetos\Syns\Documentação\Contabilidade\exemplo.txt"))
{
    string actual = reader.ReadToEnd();
    Assert.AreEqual("VLR REF ALGUMA COISA Nº 1834 MAIS TEXTO", actual);
}

What I have as a return in current string is

VLR REF SOMETHING N?? 1834 MORE TEXT

and need to return the value equal to the text file.

What kind of enconding I have to use in this situation?

  • 3

    try new StreamReader(@"D:\Projetos\Syns\Documentação\Contabilidade\exemplo.txt", Encoding.Default)

  • 1

    Using that way solved me.

1 answer

1


The most common is UTF8, but you can also try with ANSI

using (var reader = new StreamReader(@"D:\Projetos\Syns\Documentação\Contabilidade\exemplo.txt", System.Text.Encoding.UTF8))
{
     string actual = reader.ReadToEnd();
     Assert.AreEqual("VLR REF ALGUMA COISA Nº 1834 MAIS TEXTO", actual);
}

EDIT: As seen in the comments, by @Rovannlinhalis Enconding.Default was the solution to the problem.

  • With UTF8 did not roll, as it gets with ANSI?

  • System.Text.Encoding.ANSI, paste in your question an excerpt from the text file.

  • But I put an excerpt.

  • Is this all the contents of the file or is it composed of multiple lines? you have checked the encoding on which the file was written?

  • It is an accounting release file that is exported by an external system, not know in which enconding it is exported. I have more yes lines in this file I only extract the part that affects the reading to simplify the question, because my problem is when there is no.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.