4
#include<stdio.h>
#include<stdlib.h>
void init(char*filename)
{
    FILE* f = fopen(filename,"w");
    fprintf(f,"%d ",8);
    fprintf(f,"%d ",6);
    int i, j;
    for(i = 0; i < 8; i ++)
    {
        for(j = 0; j < 8; j ++)
        {
            fprintf(f,"%d ", rand()%6);
        }
    }    
    fclose(f);
}
int main()
{
    init("input.txt");
}

when I open file I see this:

*‸‶‵‵‴‴‵‴‰‰‴′‵‵‱″‱‵‱′″‰″‰′″‴‴″′′‵‵‰‵‰″‴‵‱‱‰‵″′″″′″‱‵‴‵′‴″″‱‵″‱‴‴‵′‰*

Why don't I see the values of rand()%6 that were printed to this file??

I use Dev-C++ 5.6.3 to run the program and save it with format .cpp, Open with Notepad

Bodo
  • 9,287
  • 1
  • 13
  • 29
Stack
  • 111
  • 6
  • I could not replicate your issue. See https://repl.it/repls/FlawedRosybrownEngineering. We might need some more system details to find out why that is happening – pastaleg Mar 24 '20 at 10:34
  • @MarioTheSpoon but i use w, Values binary stored by wb ??? – Stack Mar 24 '20 at 10:38
  • yep, i use dev-c to run this program – Stack Mar 24 '20 at 10:40
  • The mode `"w"` or `"wb"` will only make a difference in the line endings on systems that use CR+LF. The `fprintf` format `"%d"` will print the text representation of an `int` number. I cannot reproduce the problem either, I get numbers separated by spaces as expected. – Bodo Mar 24 '20 at 10:41
  • @pastaleg when I `fprintf(f, "%c", ' ')` this error can be sloved. – Stack Mar 24 '20 at 10:42
  • 2
    The per mille and per thousand signs are U+2030 and U+2031 respectively. The ASCII codes for space and the digits 0 and 1 are 0x20, 0x30 and 0x31. It appears that you are viewing a plain ASCII (or UTF-8, for that matter) file as UTF-16 file. – M Oehm Mar 24 '20 at 10:43
  • works fine for me: https://godbolt.org/z/uYLfx- – 0___________ Mar 24 '20 at 10:43
  • @Stack Please add all information to your question instead of writing comments. What program do you use to open the file? – Bodo Mar 24 '20 at 10:44
  • I know this program can work with many compilers, But dev-c will make the error. it works fine if I use `rand()` and error with `rand()%6` – Stack Mar 24 '20 at 10:48
  • @Bodo yep, What is more infor you need? I use Dev-C++ 5.6.3, save the program with format .cpp – Stack Mar 24 '20 at 10:53
  • @MOehm That should be an answer. I’ve added my own answer to this effect now but if you want to write your own I’ll delete mine and upvote yours instead. – Konrad Rudolph Mar 24 '20 at 10:56
  • @Stack instead of opening your file use `cat thefilename` (Linux, Mac) or `type thefilename` (Windows) – Jabberwocky Mar 24 '20 at 10:57
  • 1
    @Stack Please [edit] your question to add requested information instead of answering in comments. What program do you use to open the file `"input.txt"`? – Bodo Mar 24 '20 at 10:58
  • @KonradRudolph: That's okay. Your link is spot on. – M Oehm Mar 24 '20 at 11:03
  • @Bodo i use notpad, and i think notepad misunderstood this file is UTF-16 encoded – Stack Mar 24 '20 at 11:12
  • 1
    @Stack You don't have to explain what others already wrote. The main point is that all information should be in the question. This site is used as a knowledge base for people having similar problems, so it is important that they can find your question and the corresponding answer. Without your confirmation that you use Notepad the answer was based on guessing. – Bodo Mar 24 '20 at 12:00

1 Answers1

6

The problem isn’t your code. The problem is that Notepad.exe1 thinks that your file is UTF-16 encoded, when in reality it’s ASCII encoded. The file is fine.


1 or whatever editor you’re opening the file in

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214