精英盒子 -> 程序设计 -> 怎样用C++实现把string类型的文本保存为.txt? [打印本页]

编程土豆 2011-09-28 21:42

怎样用C++实现把string类型的文本保存为.txt?

想用控制台写个记事本,但有些功能不知在怎么实现
1、把用户输入的文本保存在string类型的变量里,并保存为.txt文件
2、可以打开用户保存的.txt文件
求执导~~~
下面是开头:
  1. #include  <iostream>
    #include  <string>
    using  namespace  std;

    int  main()
    {
        cout  <<  "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
        cout  <<  "\t欢迎使用GnoteBook记事笔记本\t\n";
        cout  <<  "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";

        string  note;

    }



jybox 2011-09-28 22:01
表示我根本不会用控制台的记事本,比如vi、vim、gvim神马的
更别提写了

jybox 2011-09-28 22:13
保存文件嘛
  1. QFile file(FilePath);          //路径
    if(file.open(QIODevice::WriteOnly))
            {
                QTextStream stream(&file);
                stream<<note;
                file.close();
            }


编程土豆 2011-09-29 21:41
  1. #include  <iostream>
    #include  <string>
    #include  <QTextIStream>
    #include  <QFile>
    using  namespace  std;

    int  main()
    {
        cout  <<  "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
        cout  <<  "\t欢迎使用GnoteBook记事笔记本\t\n";
        cout  <<  "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";

        string  note;
        cout  <<  "请选择:\n";
        cout  <<  "1.新建记事本请输入1\t2.打开记事本请输入2\t\n";
        cout  <<  "请选择:";
        int  mode(0);
        cin  >>  mode;

        if(mode  >  2)
        {
            cout  <<  "错误!退出!!\n";
            return  -1;
        }

        if(mode  ==  0)
        {
            cout  <<  "错误!退出!!\n";
            return  -1;
        }

        if(mode  ==  1)
        {
            cout  <<  "请输入内容,输入完毕后请按ctrl+z  或者  ctrl+d:\n";
            while(getline(cin,note));
        }

        QFile file(C:);
        if(file.open(QIODevice::WriteOnly))
                {
                    QTextStream stream(&file);
                    stream<<note;
                    file.close();
                }
        cout  <<  "文件已保存在C盘~~~~\n"
    }
这个问题很多呀~~~还有,你给我的代码使用是要加入哪些头文件?我用的QT
错误:
  1. In file included from /usr/include/c++/4.5/backward/strstream:47:0,
                     from ../GoteBook/GoteBook.cpp:5:
    /usr/include/c++/4.5/backward/backward_warning.h:28:2: warning: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated.
    ../GoteBook/GoteBook.cpp: In function ‘int main()’:
    ../GoteBook/GoteBook.cpp:39:16: error: ‘C’ was not declared in this scope
    ../GoteBook/GoteBook.cpp:43:25: error: no match for ‘operator<<’ in ‘stream << note’
    /usr/include/qt4/QtCore/qtextstream.h:183:18: note: candidates are: QTextStream& QTextStream::operator<<(QBool)
    /usr/include/qt4/QtCore/qtextstream.h:184:18: note:                 QTextStream& QTextStream::operator<<(QChar)
    /usr/include/qt4/QtCore/qtextstream.h:185:18: note:                 QTextStream& QTextStream::operator<<(char)
    /usr/include/qt4/QtCore/qtextstream.h:186:18: note:                 QTextStream& QTextStream::operator<<(short int)
    /usr/include/qt4/QtCore/qtextstream.h:187:18: note:                 QTextStream& QTextStream::operator<<(short unsigned int)
    /usr/include/qt4/QtCore/qtextstream.h:188:18: note:                 QTextStream& QTextStream::operator<<(int)
    /usr/include/qt4/QtCore/qtextstream.h:189:18: note:                 QTextStream& QTextStream::operator<<(unsigned int)
    /usr/include/qt4/QtCore/qtextstream.h:190:18: note:                 QTextStream& QTextStream::operator<<(long int)
    /usr/include/qt4/QtCore/qtextstream.h:191:18: note:                 QTextStream& QTextStream::operator<<(long unsigned int)
    /usr/include/qt4/QtCore/qtextstream.h:192:18: note:                 QTextStream& QTextStream::operator<<(qlonglong)
    /usr/include/qt4/QtCore/qtextstream.h:193:18: note:                 QTextStream& QTextStream::operator<<(qulonglong)
    /usr/include/qt4/QtCore/qtextstream.h:194:18: note:                 QTextStream& QTextStream::operator<<(float)
    /usr/include/qt4/QtCore/qtextstream.h:195:18: note:                 QTextStream& QTextStream::operator<<(double)
    /usr/include/qt4/QtCore/qtextstream.h:196:18: note:                 QTextStream& QTextStream::operator<<(const QString&)
    /usr/include/qt4/QtCore/qtextstream.h:197:18: note:                 QTextStream& QTextStream::operator<<(const QByteArray&)
    /usr/include/qt4/QtCore/qtextstream.h:198:18: note:                 QTextStream& QTextStream::operator<<(const char*)
    /usr/include/qt4/QtCore/qtextstream.h:199:18: note:                 QTextStream& QTextStream::operator<<(const void*)
    /usr/include/qt4/QtCore/qchar.h:394:28: note:                 QDataStream& operator<<(QDataStream&, const QChar&)
    /usr/include/qt4/QtCore/qbytearray.h:577:28: note:                 QDataStream& operator<<(QDataStream&, const QByteArray&)
    /usr/include/qt4/QtCore/qstring.h:1083:28: note:                 QDataStream& operator<<(QDataStream&, const QString&)
    /usr/include/qt4/QtCore/qobject.h:397:22: note:                 QDebug operator<<(QDebug, const QObject*)
    /usr/include/qt4/QtCore/qiodevice.h:247:22: note:                 QDebug operator<<(QDebug, QIODevice::OpenMode)
    /usr/include/qt4/QtCore/qlocale.h:720:28: note:                 QDataStream& operator<<(QDataStream&, const QLocale&)
    /usr/include/qt4/QtCore/qtextstream.h:290:21: note:                 QTextStream& operator<<(QTextStream&, QTextStream& (*)(QTextStream&))
    /usr/include/qt4/QtCore/qtextstream.h:293:21: note:                 QTextStream& operator<<(QTextStream&, QTextStreamManipulator)
    ../GoteBook/GoteBook.cpp:47:1: error: expected ‘;’ before ‘}’ token



jybox 2011-09-29 22:31
睁大眼睛看第38行
QFile file(C:);

编程土豆 2011-09-30 12:58
jybox:[表情]  [表情]  [表情] 睁大眼睛看第38行
QFile file(C:); (2011-09-29 22:31) 

怎么了?有问题么~~~

jybox 2011-09-30 17:45
编程土豆:怎么了?有问题么~~~ (2011-09-30 12:58) 

你不觉得有问题?参数应该是个文件地址吧,而且是字符串.......

编程土豆 2011-09-30 22:32
这样还是错的:
  1. QFile file("C:\");
        if(file.open(QIODevice::WriteOnly))
                {
                    QTextStream stream(&file);
                    stream<<note;
                    file.close();
                }
        cout  <<  "文件已保存在C盘~~~~\n";

错误:
  1. In file included from /usr/include/c++/4.5/backward/strstream:47:0,
                     from ../GoteBook/GoteBook.cpp:5:
    /usr/include/c++/4.5/backward/backward_warning.h:28:2: warning: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated.
    ../GoteBook/GoteBook.cpp:39:16: warning: missing terminating " character
    ../GoteBook/GoteBook.cpp:39:5: error: missing terminating " character
    ../GoteBook/GoteBook.cpp: In function ‘int main()’:
    ../GoteBook/GoteBook.cpp:40:5: error: expected primary-expression before ‘if’
    ../GoteBook/GoteBook.cpp:39:15: warning: unused variable ‘file’
    make: *** [GoteBook.o] 错误 1



编程土豆 2011-09-30 22:33
表要喷我,
不会用Qt地说~~~~

编程土豆 2011-09-30 22:33
马上要学地说~~~

jybox 2011-09-30 22:39
你指定文件名了吗..........................试问怎么往C:/写东西.............

jybox 2011-09-30 22:40
另外从错误信息看,还有其他错误,貌似是多了或少了一个双引号

编程土豆 2011-09-30 22:46
jybox:[表情] 你指定文件名了吗..........................试问怎么往C:/写东西............. (2011-09-30 22:39) 

不懂呀~~~~求指教!!

编程土豆 2011-09-30 22:51
舅舅我!

jybox 2011-09-30 23:01
编程土豆:舅舅我![表情] (2011-09-30 22:51) 

我也没办法啊,我对你是否成功安装了QT表示怀疑
你试试那些自带示例能不能编译
另外可以参考我的hosts文件编辑器,涉及文件读写
http://code.google.com/p/jybox/source/browse/#svn%2FHostsEdit

编程土豆 2011-09-30 23:12
试着编译了一个示例,结果:
  1. RCC: Error in '../stickman/stickman.qrc': Cannot find file 'animations/chilling'
    make: *** [qrc_stickman.cpp] 错误 1
额~~~~看不懂

jybox 2011-09-30 23:17
编程土豆:试着编译了一个示例,结果:
RCC: Error in '../stickman/stickman.qrc': Cannot find file 'animations/chilling'
make: *** [qrc_stickman.cpp] 错误 1额~~~~看不懂[表情] (2011-09-30 23:12) 

好吧,你可以重新安装QT了..............

ps.不必at我,作为站长每个帖子我都会看

jybox 2011-09-30 23:18
  1. sudo apt-get install qt4-dev-tools qt4-doc qt4-qtconfig qt4-demos qt4-designer
    sudo apt-get install qtcreator

这么安装完全无压力

编程土豆 2011-09-30 23:40
怎么安装完了还是一样~~~郁闷!!

jybox 2011-10-01 00:07
编程土豆:怎么安装完了还是一样~~~郁闷!![表情] (2011-09-30 23:40) 

同情,实在不行等11.10正式版发布之后重装系统吧

编程土豆 2011-10-01 09:02
顺便问一下,你用的是最新版还是长期支持版?

编程土豆 2011-10-01 09:04
jybox:同情,实在不行等11.10正式版发布之后重装系统吧 (2011-10-01 00:07) 

顺便问一下,你用的是最新版还是长期支持版?

jybox 2011-10-01 17:40
编程土豆:顺便问一下,你用的是最新版还是长期支持版?
 (2011-10-01 09:04) 

最新版......

zhaoqike 2011-10-23 19:35
windows下:
在c盘下新建一个aa.txt的文件
QString note = "jfkdlsjdlf";
    QFile file("C:\\aa.txt");
    if(file.open(QIODevice::WriteOnly))
                {
                    QTextStream stream(&file);
                    stream<<note;
                    file.close();
                }
注意:文件名两个斜杠,要用QString,文件名双引号

zhaoqike 2011-10-23 19:37
如果是linux下的我就暂时不会了

jybox 2011-10-23 20:07
zhaoqike:windows下:
在c盘下新建一个aa.txt的文件
QString note = "jfkdlsjdlf";
    QFile file("C:\\aa.txt");
    if(file.open(QIODevice::WriteOnly))
....... (2011-10-23 19:35) 

linux区别不大

\\是因为反斜杠需要转义

也可以用/来分割路径...




Powered by phpwind v8.7 Code ©2003-2011 phpwind
Time 0.068530 second(s),query:5 Gzip enabled