Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ file
Hi all,
I wanted to write file in c++
i have some data in unsigned char
 
this is my code:
 
typedef unsigned char UCHAR;
 
for (int i=0; i < 4; i++)
<pre lang="xml">UCHAR ucMntFlat[4][1024];
  {
      char filename[64];
      sprintf( filename, &quot;temp%d.txt&quot;, i);
       ofstream out(filename);
      out.write((char *)ucMntFlat,sizeof(ucMntFlat));
      out.close();
 
   }</pre>
i m getting some special characters in file for ex:
>à/ô/ô/ô ©+ÿ ú ú   L/é/P/é ÿ/è˜AB >§> <ë//ÿV˜‰/Yœ§/ 	„ 	„ÿ<Òÿa ò;;;;;;K¥&FZ“.…W'é…P«¦…,(ÅNœÇ†DÃE_'­…+ÈE=…+F…8€Å+(‡
Gž&JW+(I>:I ˆI'‰J3ÃJ)ŠPv	P$'‰C’z	P0å
È
c+®U¡êÎL˜ _–©Y¬h
[«NW#êÎ\îÍ/–Å
Ç‘ÈçÑi©‘F±ä‘].hRŸˆÑK•ZÒ'/åÕZŸ/•gšäYœ'u#`¤0ÚR½™+±[˜¨u&[]Vœá“¥-Xªíj'Ömg©s6 &3@MZgtŽ›¨µÂÏÜéö>*7DQ^kx…’Ÿ¬¹ÆÓàíú!.;HUbo|‰–£°½Ê×äñþ%2?LYfs€š§´ÁÎÛèõ<)6CP]jw„‘ž«¸ÅÒßìù -:GTan{ˆ•¢¯¼ÉÖãðý
$1>KXerŒ™¦³
 
can u plz tell me how can i save this data in file..
plz help..
god bless u all.. Smile | :)
Posted 6-May-13 0:02am
MAU7871.6K
Comments
Jochen Arndt at 6-May-13 5:11am
   
What did you expect?
You are writing data from ucMntFlat which isn't initialized anywhere. So it contains garbage.
 
Also please edit your question. The code actually posted should not compile (the local variable ucMntFlat is defined 4 times inside a loop but accessed later outside the loop).

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

if your buffer contains binary data, you have two options:
  1. save it, as it stands, in a binary file (basically what you already did)
  2. save the hexadecimal representation of the binary data in a text file (in order to have a human-readable file)
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Mika Wendelius 261
1 Abhinav S 155
2 Peter Leow 140
3 Sergey Alexandrovich Kryukov 125
4 Kornfeld Eliyahu Peter 115
0 Sergey Alexandrovich Kryukov 8,593
1 OriginalGriff 6,536
2 Peter Leow 3,727
3 Zoltán Zörgő 3,456
4 Richard MacCutchan 2,417


Advertise | Privacy | Mobile
Web04 | 2.8.150123.1 | Last Updated 6 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100