Hello,
I am new to programming and I have a strange issue.
I read lines from a textfile and throw theim separetly in an String Array.
Everything is working fine except that when I WriteLine(Array[0]) it writes in the console:
?My sentence bla bla bla....
All others element of the array write just fine...
Another odd thing is that when I use the imediate window to ask about that element, it writes it just fine.
Here is the code... If someone can figure out my mistake, it would be awesome.
Thank you!
(I didn't find a way to show the code properly so I hope it s readable)
namespace Radio
{
class Program
{
static void Main(string[] args)
{
StreamReader myReader = new StreamReader(@"playlist.txt");
String line = "";
int compteur = 0;
Console.WriteLine("Ordre normal :");
while(line != null)
{
line = myReader.ReadLine();
if(line!=null)
{
Console.WriteLine(line);
compteur++;
}
}
Console.WriteLine("Nombre de chansons dans la playlist:" + compteur.ToString());
String[] ordreNorm = new string[compteur];
myReader.BaseStream.Position = 0;
for (int i = 0; i < compteur; i++)
{
ordreNorm[i] = myReader.ReadLine();
}
foreach (string song in ordreNorm)
{
Console.WriteLine(song);
}
Console.WriteLine(ordreNorm[0]);
Console.ReadLine();
}
}
}
7 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted May 3, 2014, 1:33 PM
http://en.wikipedia.org/wiki/Byte_order_mark
http://msdn.microsoft.com/en-us/library/vstudio/9y86s1a9
Michael JacquetPosted May 2, 2014, 7:52 PM
It looks like there is an issue in my text... it's printing:
EF BB BF 6D 75 73 69 71 75
I am now totally lost ^^ I would like to do a print screen.. i m not crazy... That text is starting by "m" from musique 1... I checked and double checked...
Thank you anyway Vulpes!
[EDIT] I found the solution because of your previous answer and checked my txt file.. it was encoded UTF-8 and not ANSI. I changed it to ANSI and It worked...
Thank you very much!
VulpesPosted May 2, 2014, 7:16 PM
On my machine, using an ANSI file created in notepad, the output is:
6D 75 73 69 71 75 65 20 31
which are, of course, the hex values of the bytes corresponding to musique 1.
Michael JacquetPosted May 2, 2014, 6:42 PM
VulpesPosted May 2, 2014, 6:12 PM
One thing you could try is deleting the first line of the file and then typing it back in again just in case a non-printing character has somehow got in there.
Michael JacquetPosted May 2, 2014, 6:05 PM
musique 1
musique 2
musique 3
musique 4
musique 5
musique 6
And nothing more! It starts by a m and that "m" is printed (after the ?) And if I change myReader.BaseStream.Position = 0; By myReader.BaseStream.Position = 1;
I get:
??musique 1
musique 2
musique 3....
that s really a weird issue. An idea? Thank you again!
VulpesPosted May 2, 2014, 5:47 PM
My guess is that the first line in the file begins with an accented vowel or some other extended ASCII character greater than 127. The default Console font is unable to show such characters and therefore displays them as an '?' instead.
The immediate window, OTOH, can show them and so they display normally.