Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7080199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:45:56+00:00 2026-05-28T06:45:56+00:00

I am trying to read portuguese characters from files, and keep getting into problems.

  • 0

I am trying to read portuguese characters from files, and keep getting into problems.

I have the following C# code (for testing purposes):

var streamReader = new StreamReader("file.txt");

while (streamReader.Peek() >= 0)
{
  var buffer = new char[1];
  streamReader.Read(buffer, 0, buffer.Length);
  Console.Write(buffer[0]);
}

It reads each character in the file and then outputs it to the console.
The file contains the following: “cãsa”.
The output in the console is: “c?sa”.

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:45:57+00:00Added an answer on May 28, 2026 at 6:45 am

    You need to read the file using the correct encoding – by default the file will be read as UTF-8, if that’s not the right encoding, you will get such issues.

    In this example, I am using an constructor overload that takes an encoding, in this case UnicodeEncoding, which is UTF-16:

    using(var streamReader = new StreamReader("file.txt", Encoding.UnicodeEncoding))
    {
        while (streamReader.Peek() >= 0)
        {
          var buffer = new char[1];
          streamReader.Read(buffer, 0, buffer.Length);
          Console.Write(buffer[0]);
        }
    }
    

    In this example, I am using codepage 860, corresponding to Portuguese:

    using(var streamReader = new StreamReader("file.txt", Encoding.GetEncoding(860)))
    {
        while (streamReader.Peek() >= 0)
        {
          var buffer = new char[1];
          streamReader.Read(buffer, 0, buffer.Length);
          Console.Write(buffer[0]);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to read in some information from the google maps api into my application
am trying to read web page from java code and its fine for general
Trying to read into an object the following XML file (can be changed) into
Im trying to read an alarm structure from a Beckhoff - PLC into a
Im trying to read in image file from a server , with the code
I'm trying to read weather information from the Google Weather API. My Code looks
am trying to read few lines from a txt file using JS,and i have
Im trying to read some data from a binary file into a buffer allocated
I am trying to read in from a few files using something like this
I'm trying to read a TIFF image from file using BufferedImage. The following is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.