helpme's blog

By helpme, history, 9 years ago, In English

Hello everyone,

Today I had to make a test case with 1000 characters, so I had to write a txt file. I wrote this program:

#include<bits/stdc++.h>
using namespace std;
int main ()
{
    freopen("zxq.txt" , "w" , stdout) ;
    for(int i = 0 ; i < 1000; i++)
        cout << rand() % 2 << ' ' ;
}

but, when I opened the zxq.txt file there were some other charaters like '‱' and '‰' not '0' or '1' ! But, when I changed the code as following it worked correctly:

#include<bits/stdc++.h>
using namespace std;
int main ()
{
    freopen("zxq.txt" , "w" , stdout) ;
    for(int i = 0 ; i < 1000; i++)
        cout << rand() % 2 ;
}

Can anyone please explain why it is working like this or how to resolve this problem?
Thanks in advance!

  • Vote: I like it
  • +13
  • Vote: I do not like it

»
9 years ago, # |
  Vote: I like it +13 Vote: I do not like it

The file itself is alright in both cases.
I am able to reproduce the problem only when I open the first file in Notepad.
Perhaps it (wrongly) decides the file is in UTF-16 instead of ASCII.