locked
How read a text file if special character Bullet is present using c#? RRS feed

  • Question

  • User50299930 posted

    Hi,

    How to read the text file if that text contains special character as Bullet?

    For special character '*' my code is working.

    But with bullet when read text file special character is droped.

    My Code is as follows

    FileStream fStream = new FileStream(path, FileMode.Open);
    string contents = null;
    Byte[] bytes = new Byte[fStream.Length];
    fStream.Read(bytes, 0, bytes.Length);

    System.Text.Encoding enc = System.Text.Encoding.ASCII;
    contents = enc.GetString(bytes);
    Console.WriteLine(contents);
    Console.ReadLine();
    fStream.Close();

    sample data is


    ST(Bullet)850(Bullet)067650007
    BEG(Bullet)00(Bullet)BK(Bullet)9196185(Bullet)(Bullet)20140904

    plz guide me if anything wrong?

    Thanks

    Ramki

    Wednesday, August 12, 2015 10:15 AM

Answers

  • User753101303 posted

    Do you really want to use ASCII? If a character is not part of the ASCII table you'll have to replace it with another character. Unless you have some strong reason I would just try to keep using the current encoding used by this file.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 13, 2015 4:09 AM
  • User-84896714 posted

    Hi krishn451,

    Thank you for your post.

    How read a text file if special character Bullet is present using c#?

    The answer is use the right Encoding to read data. Do you know which Encoding was used to write data to the text file? You could use UTF8 to read text from this file. Try below code.

    FileStream fStream = new FileStream(path, FileMode.Open);
    string contents = null;
    Byte[] bytes = new Byte[fStream.Length];
    fStream.Read(bytes, 0, bytes.Length);
    
    System.Text.Encoding enc = System.Text.Encoding.UTF8;
    contents = enc.GetString(bytes);
    Console.WriteLine(contents);
    Console.ReadLine();
    fStream.Close();

    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 13, 2015 4:43 AM

All replies

  • User753101303 posted

    Hi,

    It's best to be always explicit. Do you mean it is replaced by a "?" character which is what happens if it is not part of the ASCII table (rather than just being ignored)?

    Or do you mean it is really just ignored?

    Wednesday, August 12, 2015 1:11 PM
  • User50299930 posted

    Hi,

    Thanks for a your reply.

    Yes you are correct. replaced by '?'

    what is the solution to work my code for both astrick and BELL?

    Thanks,

    Ramki

    Thursday, August 13, 2015 2:21 AM
  • User753101303 posted

    Do you really want to use ASCII? If a character is not part of the ASCII table you'll have to replace it with another character. Unless you have some strong reason I would just try to keep using the current encoding used by this file.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 13, 2015 4:09 AM
  • User-84896714 posted

    Hi krishn451,

    Thank you for your post.

    How read a text file if special character Bullet is present using c#?

    The answer is use the right Encoding to read data. Do you know which Encoding was used to write data to the text file? You could use UTF8 to read text from this file. Try below code.

    FileStream fStream = new FileStream(path, FileMode.Open);
    string contents = null;
    Byte[] bytes = new Byte[fStream.Length];
    fStream.Read(bytes, 0, bytes.Length);
    
    System.Text.Encoding enc = System.Text.Encoding.UTF8;
    contents = enc.GetString(bytes);
    Console.WriteLine(contents);
    Console.ReadLine();
    fStream.Close();

    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 13, 2015 4:43 AM