• <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 閱讀(3781) 評論(3)  編輯 收藏 引用 所屬分類: 6.Others

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


            #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;
            }

             

            最后才發(fā)現(xiàn)是文件名有問題,修改程序,去掉生成文件名的一個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;
            }


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

            Feedback

            # re: std::ofstream failed  回復(fù)  更多評論   

            2014-07-14 00:36 by pdb.so
            多看書,多調(diào)試

            # re: std::ofstream failed  回復(fù)  更多評論   

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

            # re: std::ofstream failed  回復(fù)  更多評論   

            2014-07-22 18:38 by eryar
            是的。
            加上assert()會更好點
            @云騰
            av无码久久久久不卡免费网站| 精品国产91久久久久久久a| 97超级碰碰碰碰久久久久| 久久国产免费观看精品3| 久久www免费人成看片| 亚洲人成无码网站久久99热国产| 久久99精品久久久久久秒播| 久久精品亚洲男人的天堂 | 久久精品无码一区二区三区日韩| 色综合久久中文色婷婷| 99久久无码一区人妻| 久久精品中文字幕一区| 午夜视频久久久久一区| 99久久综合国产精品免费| 久久福利资源国产精品999| 少妇久久久久久久久久| 国产精品久久毛片完整版| 国产高潮久久免费观看| 色综合久久中文字幕综合网 | 久久久国产精华液| 久久无码AV中文出轨人妻| 漂亮人妻被中出中文字幕久久| 亚洲国产精品高清久久久| 久久无码人妻一区二区三区午夜| 97精品伊人久久大香线蕉app | 国产精品激情综合久久| 中文精品99久久国产| 久久精品中文无码资源站| 久久久WWW免费人成精品| 中文字幕无码免费久久| 久久九九全国免费| 亚洲中文字幕无码一久久区| 99久久亚洲综合精品成人| 青青草原综合久久大伊人| 精品国产福利久久久| 无码八A片人妻少妇久久| 久久er热视频在这里精品| 人人妻久久人人澡人人爽人人精品| 狠狠88综合久久久久综合网| 97香蕉久久夜色精品国产| 国产免费久久精品99久久|