• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            eryar

            PipeCAD - Plant Piping Design Software.
            RvmTranslator - Translate AVEVA RVM to OBJ, glTF, etc.
            posts - 603, comments - 590, trackbacks - 0, articles - 0

            std::ofstream failed

            Posted on 2014-07-12 15:55 eryar 閱讀(3792) 評論(3)  編輯 收藏 引用 所屬分類: 6.Others

            今天在輸出數據到文件時,用到C++的std::ofstream,結果文件就是沒有輸出成功,示例程序如下所示:


            #include 
            <fstream>
            #include 
            <sstream>
            #include 
            <iostream>
            #include 
            <cassert>

            void Output(const std::string &theFileName)
            {
                std::ofstream os(theFileName.c_str());

                assert(os.good());

                os 
            << "Just for test" << std::endl;

                os.close();
            }

            int main(int argc, char* argv[])
            {
                std::stringstream ss;

                
            // remove the std::endl
                ss << "test" << 1 << ".txt" << std::endl;

                Output(ss.str());

                
            return 0;
            }

             

            最后才發現是文件名有問題,修改程序,去掉生成文件名的一個std::endl即可:

             


            #include 
            <fstream>
            #include 
            <sstream>
            #include 
            <iostream>
            #include 
            <cassert>

            void Output(const std::string &theFileName)
            {
                std::ofstream os(theFileName.c_str());

                assert(os.good());

                os 
            << "Just for test" << std::endl;

                os.close();
            }

            int main(int argc, char* argv[])
            {
                std::stringstream ss;

                
            // remove the std::endl
                ss << "test" << 1 << ".txt";

                Output(ss.str());

                
            return 0;
            }


            不經意間就會寫出這種小錯誤的代碼,而且找錯很很費時。

            Feedback

            # re: std::ofstream failed  回復  更多評論   

            2014-07-14 00:36 by pdb.so
            多看書,多調試

            # re: std::ofstream failed  回復  更多評論   

            2014-07-22 17:17 by 云騰
            文件名不允許存在特殊字符 www.cppentry.com

            # re: std::ofstream failed  回復  更多評論   

            2014-07-22 18:38 by eryar
            是的。
            加上assert()會更好點
            @云騰
            久久夜色精品国产www| 久久久久亚洲AV无码观看| 久久超乳爆乳中文字幕| 精品久久久噜噜噜久久久| 97久久久精品综合88久久| 中文字幕成人精品久久不卡| 久久激情亚洲精品无码?V| 精品国产乱码久久久久久呢| 国产精品视频久久久| 日韩电影久久久被窝网| 亚洲午夜久久久影院伊人| 一本一本久久a久久综合精品蜜桃| 欧美va久久久噜噜噜久久| 91久久精品无码一区二区毛片| 中文字幕久久亚洲一区| 91精品免费久久久久久久久| 97久久国产综合精品女不卡 | 久久精品国产色蜜蜜麻豆| 伊人久久大香线蕉av不卡| 国产成人精品久久亚洲| 无码精品久久久久久人妻中字 | 久久无码AV一区二区三区| 欧美亚洲国产精品久久蜜芽| 亚洲∧v久久久无码精品| 日本加勒比久久精品| 成人午夜精品久久久久久久小说| 久久久久亚洲AV片无码下载蜜桃| 亚洲国产精品综合久久一线| 国产成人久久激情91| 国产V亚洲V天堂无码久久久| 久久综合九色综合网站| 中文字幕精品久久久久人妻| 久久久WWW成人| 合区精品久久久中文字幕一区| 久久综合久久综合久久| 久久国产色AV免费观看| 少妇高潮惨叫久久久久久| 久久亚洲中文字幕精品有坂深雪| 亚洲精品乱码久久久久久自慰| 久久久久亚洲AV片无码下载蜜桃| 久久午夜无码鲁丝片秋霞|