• <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 閱讀(3797) 評論(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()會更好點
            @云騰
            精品久久久久久久久久久久久久久 | 久久91综合国产91久久精品| 亚洲香蕉网久久综合影视| 欧美激情一区二区久久久| 久久久久无码精品国产| 久久国产乱子精品免费女| 久久综合五月丁香久久激情| 97精品国产97久久久久久免费| 2020久久精品国产免费| 久久国产精品偷99| 狠狠综合久久AV一区二区三区| 99久久精品国内| 久久丫忘忧草产品| 国产福利电影一区二区三区久久老子无码午夜伦不 | 久久综合丝袜日本网| 久久精品免费全国观看国产| 99久久成人国产精品免费| 久久久久亚洲国产| 办公室久久精品| 九九久久99综合一区二区| 久久中文字幕人妻丝袜| 久久国产精品免费一区二区三区| 久久午夜无码鲁丝片| 久久人人爽人人人人爽AV | 久久综合色之久久综合| 久久综合九色综合欧美狠狠| 日韩久久久久久中文人妻| 久久人人爽人人爽人人片AV麻烦 | 久久人人爽人人爽人人片av麻烦 | AV无码久久久久不卡蜜桃| 伊人久久大香线蕉综合热线| 精品国产婷婷久久久| 国产精品内射久久久久欢欢| 国产精品毛片久久久久久久| 久久久久99精品成人片欧美| 亚洲国产另类久久久精品| 久久婷婷色综合一区二区| 无码人妻久久一区二区三区蜜桃 | 无码国内精品久久综合88 | 久久精品国产亚洲αv忘忧草 | 久久精品国产亚洲AV不卡|