Wchar_t and fwrite help!

Hello people of cplusplus.com!

I'm making this thread to ask you guys for some help.

What I'm trying to accomplish is making a console application where the user will input some text which will be assigned to a wchar_t*(?) and later written in binary to a file that I later will load up in another program.

I tried this code:
wchar_t* custominput = L"";
wcin >> custominput;

(this crashes the program once the user hit enter on the input)

What I've made so far:



The code makes a file called .flt
and it makes a file with this:

1
2
3
4
5
Hex:
FE FF FF FF 01 01 00 00 00 01 00 03 00 00 00 00 44 58 55 46 4D 54 00 04 00 00 00 41 00 75 00 64 00 69 00 02 00 00 00 21 00 00 00 56 00 65 00 68 00 69 00 63 00 6C 00 65 00 54 00 79 00 70 00 65 00 47 00 72 00 69 00 64 00 44 00 42 00 54 00 61 00 62 00 6C 00 65 00 56 00 69 00 65 00 77 00 6D 00 61 00 6E 00 4E 00 61 00 6D 00 65 00 12 56 00 6F 00 6C 00 76 00 6F 00 56 00 6F 00 6C 00 76 00 6F 00

Unicode:
þÿÿÿ............DXUFMT.....A.u.d.i.....!...V.e.h.i.c.l.e.T.y.p.e.G.r.i.d.D.B.T.a.b.l.e.V.i.e.w.m.a.n.N.a.m.e..V.o.l.v.o.V.o.l.v.o.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <fstream>



using namespace std;




// Int, char, double och functions



int antBilar;
char bilTyp[100];
int bilKW;
int x;
void filter();
void binaryWrite();
wchar_t * str = L"Volvo";

// Int, char, double och functions


void filter()
{

cout << "Antal bilar: ";
cin >> antBilar;
int i;


while(x < antBilar)
{

cout << "Bilnamn #" << x+1 << endl;
x++;



}


}


void binaryWrite()
{

    ofstream file("C:\\Users\\Bosse\\Desktop\\test.flt", ios::out | ios::binary | ios::app);
if (file.good())
{

    char buf[5] = {254, 255, 255, 255, 1};
    char buf2[7] = {1, 0, 3, 0, 0, 0, 0};
    char bilNamn[94] = {68, 88, 85, 70, 77, 84, 0, 4, 0, 0, 0, 65, 0, 117, 0,
     100, 0, 105, 0, 2, 0, 0, 0, 33, 0, 0, 0, 86, 0, 101, 0, 104, 0, 105, 0, 99, 0,
      108, 0, 101, 0, 84, 0, 121, 0, 112, 0, 101, 0, 71, 0, 114, 0, 105, 0, 100, 0,
      68, 0, 66, 0, 84, 0, 97, 0, 98, 0, 108, 0, 101, 0, 86, 0, 105, 0, 101, 0, 119,
        0, 109, 0, 97, 0, 110, 0, 78, 0, 97, 0, 109, 0, 101, 0, 18};



    file.write(buf, sizeof(buf));
    file.write((char*)&antBilar, sizeof(antBilar));
    file.write(buf2, sizeof(buf2));
    file.write(bilNamn, sizeof(bilNamn));
    file.write((char*)str, wcslen(str) * sizeof(wchar_t));
    file.write((char*)str, wcslen(str) * sizeof(wchar_t));

       file.close();
}
}

int main()
{



filter();
binaryWrite();
}
Last edited on

I tried this code:
1
2
wchar_t* custominput = L"";
wcin >> custominput;

Your variable doesn't have room for any characters because of the way you initialized the variable. Try something like:
1
2
wchar_t custominput[100];
wcin >> custominput;


OOOH!
There we go! Thank you jlb! :D

Alright so this put's my desired input into the file, would you happen to know how I count the chars or "size" of the custominput as I need to define this in the binary of the file aswell?

Tried playing around with wcslen and sizeof but failed.
Kinda new to this binary and wchar thing x)

EDIT:

Also i will need to be able to write something else like:

text (space) more text
But using the above method will only write the first text before the space
Last edited on
I'm not very familiar with Unicode but you shouldn't be using strlen() you want to copy the entire array, not just the C-string. If the size of your array is 100 bytes you want to copy the entire 100 bytes.
hmm..
This is what I use to write the wchar to the file:

file.write((char*)custominput, wcslen(custominput) * sizeof(wchar_t));

I'm not quite sure what you're trying to say :S
I am saying you shouldn't be using wcslen() but you should be using the actual size of the array. Remember strlen() only reports the size up to the termination character. The actual array will usually be larger.

Jim
Oh alright!
I will try it out and see if I can figure this out!
Thanks alot Jim!
Topic archived. No new replies allowed.