1

When data is stored on a text file using fwrite, like this, where t is a struct:

fwrite(&t, sizeof(t), 1, fptr);

The output in the text file ends up as:

楷獬湯찀쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌敷敷

(Above is a sample, not the full output)

Why does this happen?

Edit: t is a struct - where user is prompted for two strings, username and password. Here's a basic template - it was a friend who had the problem and I don't have the actual code snippet with me to share, but I assume it was something equivalent to this. I know it's not very helpful, sorry about that. Any help is appreciated.

struct details {
char username[32], password[32];
}

int main() {
struct details t;
scanf("%s",t.username);
scanf("%s",t.password);
}

File is opened as a+ mode.

Edit2: Ran friend's code, initialized struct with new file, inputting username and password as '0'. Here's the hexdump.

file name: admins.txt
mime type: 

0000-0010:  30 00 cc cc-cc cc cc cc-cc cc cc cc-cc cc cc cc  0....... ........
0000-0020:  cc cc cc cc-cc cc cc cc-cc cc cc cc-cc cc cc cc  ........ ........
0000-0030:  cc cc cc cc-cc cc cc cc-cc cc cc cc-cc cc cc cc  ........ ........
0000-0040:  cc cc cc cc-cc cc cc cc-cc cc cc cc-cc cc cc cc  ........ ........
0000-0050:  cc cc cc cc-cc cc cc cc-cc cc cc cc-cc cc cc cc  ........ ........
0000-0060:  30 00 cc cc-cc cc cc cc-cc cc cc cc-cc cc cc cc  0....... ........
0000-0070:  cc cc cc cc-cc cc cc cc-cc cc cc cc-cc cc cc cc  ........ ........
0000-0080:  cc cc cc cc-cc cc cc cc-cc cc cc cc-cc cc cc cc  ........ ........
0000-0090:  cc cc cc cc-cc cc cc cc-cc cc cc cc-cc cc cc cc  ........ ........
0000-00a0:  cc cc cc cc-cc cc cc cc-cc cc cc cc-cc cc cc cc  ........ ........
Terrornado
  • 793
  • 3
  • 9
  • 21
  • 1
    You are not writing a text file. You are writing a binary file. BTW what is `t`? – Jabberwocky May 14 '18 at 09:10
  • 1
    You probably have written some "non-text" symbols. If you try to interpret with a text editor such "non-text" symbols it will just display garbage. Try to open with a hex editor and you will see the data written in the file is correct, it's just text editors can't understand it, because they can understand only text. – малин чекуров May 14 '18 at 09:13
  • 2
    This can not be answered unless you provide some more information on the data being written, the type of file, etc – WedaPashi May 14 '18 at 09:26
  • 1
    open the file with vim and then use (ESC) :%!xxd (ENTER) to display hex values. Also as suggested provide a bit more info... – Antonin GAVREL May 14 '18 at 09:27
  • I posted the question while I was at uni, just got home. Anyhow will clarify now. – Terrornado May 14 '18 at 10:54

2 Answers2

1

Read documentation of fwrite. It is for binary IO. But you have a textual format.

You could consider parsing your textual file. Learn about fgets, sscanf, etc...

Consider also using existing textual formats like JSON, YAML, etc... You'll find many libraries for them (e.g. jansson in C).

Since you are interested in (or are observing) Chinese characters, read Utf8 Everywhere.

Also, compile your code with all warnings and debug info (e.g. gcc -Wall -Wextra -g with GCC) and learn how to use the gdb debugger to understand the behaviour of your program. You might use watchpoints to understand when a location is written.

Read much more about undefined behavior and be scared of it (junk file content is a possible effect of UB, but it could be much worse).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

Is user entered input a Unicode string? Initialize struct to all zeros. Paste hexdump of the file.

Vinay P
  • 617
  • 3
  • 13
  • This is the result: `0쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌0쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌` Hexdump is too long, in main now – Terrornado May 14 '18 at 12:30
  • you can send initial few bytes hexdump of the file using `hexdump -c ` – Vinay P May 14 '18 at 12:35
  • sorry, on windows, I assume that would be for unix? – Terrornado May 14 '18 at 12:52