用C ++编写二进制搜索树到文件

时间:2015-05-08 11:19:28

标签: c++

我创建了一个二进制搜索树,它输入一个文本文件,然后按字母顺序排列,我可以直接打印到控制台,但是当我尝试将它写入文本文件时得到是胡言乱语,无论如何,我可以使用fprintf(fp,“%s”,rootPtr-> word);?或者我应该将每个rootPtr->字放入一个数组然后将该数组写入文件?

打印出来的代码:

if (rootPtr == NULL)
        return;
    InOrder(rootPtr->left,fp);
    if(rootPtr->word != "m")
        fprintf(fp, "%s",rootPtr->word );
        //cout << rootPtr->word << endl;
    InOrder(rootPtr->right,fp);

}

乱码:

  

d0ÈõAجA_0({0¨^Aø@ A€A0þAÐBØA¸z0   Û0À> 0D·0ÉAØ'A°I0ƒ0Èõ0ÈAÈAØ)一个[AO; A @ P0 ìAðãAðÓAøOA˜‰0è®0­A0B°s0°Ê0°r00™A u00ýAøßA Bàˆ0X BxrAÅAÈì0€¼0€½0¨ÊAÐÖAÈA8A0ÝA€ Bˆ9A Ü08WA€ø0hË0@JA8UAx€é0û0èBøÍ0 {YA {1}}0EAë0Y 0 U 0°g0Èû0AGA° Y0 XA¨èAÄAp˜0øCAhAA°AHØ0ðWA€æ0ˆ A0 0A @€åA¨ZAèjA   ,A8þ0P•AðÙA~™0QAÈí0øÞ0(ZA°(A(}0P¤A   ;AAÐAAA&amp;A¨¾Akkº0À…AðSAAøÜ00qAÀBAKA'AÀöAXð0ð¿0Èμ0Èï0ø³AðÌAÈö0@ b0€0 @Í0ÐPAZA†0ðAð4A   ×0ACAhÚ0€¶0ðA¸” 0 $ A ñAˆßAB(}A8AH®Aø'Aˆm0Àœ0x“A¨AXˆAxBhµA¸“0©Aµ0Èë0€å08SA¨ã0êA ¤0ˆBè¡0ðÐAÃA8¼AX³0йAÀ«00©0X§0ÐA8 {EA {1}}¹0@Ì08ÎAÈü0~”斧÷A dAhf0gA LAP> AHA0‰阿= A   乙@ÕAÈñ0ðîAAÐ&A@h08½Aó0@1Ax­0€æA°ÕA€ÜAoA€²0ÐAˆÑ0˜‹00Ÿ0AøBXeAhe0UA ðAðAšAðä0è‰AÀ¬0€ý0ð¾0KAàœAŸA~tA8¾0è£0Èù0¨AXôAèeAð]A~sAØØ0à0ÈóAÐJAxž0ð!A(-0€Aõ0ô0Èþ08ê0FA(XAP {AèùA@IAht0PŽ08óA   0E¥0HAX¨0Ð\ A AÐÀ0ÈiAƒA˜0h/AÀ˜AP0·0E0 @a0èBX|0'0   +AΦ旋转&GT; AE©0X0A} 0X€00ƒ0pªAïAÐïAпA@j0OAà–0AØ=Aèª0o0˜~0@Ç08ÌAøÔ0FA8MAðºAÐBAðáAp¡A {á0Hx0OA{1}}7AÐFA¨5A4A_Aè†AA€A Bø.AˆAøb0hÞ0 0¨ê0°Õ0°:AxA Â0à—AH±A0†A€cAp~AÐ"AØd0€pA’0˜š0ˆÄAè¯00„0ÀšAà˜0Ðo0@Á0hÒ0@¹A€bA(|AXmAÈA8AÈ°0Aøl0Ðà0°¶A8aA?APvA%A @ E0 @:AÈ|0   Ö0'Aè÷A00 6A ’0°Å0Ø«Aˆ·AðÒA(£A¨ÍA8 A¨ëA8!AðÑA8RA¸}A@³AA8VAï0 Ó0xøAðÎA€÷0 AA£AðAXAðæ0hc00ŒAxB@i0èBøp0ŽAl0Ø0ŒAøÈ0ðÜAÐÞAAðAØu0‡甲@ BP-AÈgAøÄ0Xñ0àzA¢0°àApyA¨   AÐÔAøÑ0p| 0#-0iA0¥0H @A8ãAبAh-A(-A ÔAP˜A0B8çAháA˜wA(žAˆ08AàvAðÏAÐ1A8AðAˆÉAp•0Ø×0Œ0h´A€0x’AH*AÐÝA8jAh)AX°0à AH¦AðäAPœA¨A¨NAÀƒAð¶0ÀüA¸§AˆÂA€A8ù0@ÇAÙ08NAØw0ˆÆA8ö0دAüA8îAÐÁAA0ùAX   一个€BÐÐ0@.AÈAè0AÈÛAÈBZA   ¯Ax,0XõAHv0ç0P›0¨3AèqA\A‚A@ÖAÈÚAðØA˜uA…A“A°BèþA¨½A`¿A

1 个答案:

答案 0 :(得分:0)

我通过简单地添加.c_str()添加我的单词结尾来修复它:

fprintf(fp, "%s\n",rootPtr->word.c_str());