如何使用C ++在.Rtf文件中编写字符串? [英] How Can I Write A String In A .Rtf File Using C++?

查看:117
本文介绍了如何使用C ++在.Rtf文件中编写字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写代码,我需要在.RTF文件中编写一个unicode字符串。最好的方法是什么?

解决方案

你可以使用一个库( Google [ ^ ]是您的朋友)或直接根据其规格处理 RTF 格式(同样, Google [ ^ ]是你的朋友。)


如果文件中已经存在,则应使用以下代码本地目录。

  #include   <   fstream  >  
#include < iostream >

int main()
{
fstream infile( test.rtf); // 创建fstream,从磁盘读取名为test.rtf的文件
if (!infile.good())
{
cout<< 文件不存在<< endl;
return 0 ;
}
infile<< 要写入文件的字符串; // 写入文件结尾
infile.close(); // 关闭文件
return 0 ; // 就是这样(这是非常基本的,不会检查它是否写得正确)
}
< / iostream>< / fstream>





如果以下代码应该使用文件尚不存在

  #include   <   ofstream  >  
#include < iostream >

int main()
{
ofstream newfile( test.rtf); // 在ram中创建名为test.rtf的新文件
if (!newfile.good()) // 如果文件不能已创建
{
cout<< 无法创建文件 << ENDL;
return 0 ;
}
newfile<< 要写入文件的字符串 ; // 将字符串添加到文件
newfile.close(); // 关闭文件并将其写入磁盘
return 0 ; // 再次,这不会检查许多可能的错误
}
< ; /&的iostream GT;< / ofstream的>


Im writing a code, and i need to write a unicode string in a .RTF file. What is the best way to do it?

解决方案

You could either use a library (Google[^] is your friend) or directly handle the RTF format based on its specifications (again, Google[^] is your friend).


The following code should be used if the file already exists within the local directory.

#include <fstream>
#include <iostream>

int main()
{
    fstream infile("test.rtf"); //create fstream which reads file called "test.rtf" from disk
    if (!infile.good())
    {
        cout<<"file doesn't exist"<<endl;
        return 0;
    }
    infile<<"string you want to write to the file"; //write to end of file
    infile.close(); //close file
    return 0; //and that's it (this is very basic and does not check to see if it was written correctly)
}
</iostream></fstream>



The following code should be used if the file doesn't yet exist

#include <ofstream>
#include <iostream>

int main()
{
    ofstream newfile("test.rtf"); //create new file in ram called "test.rtf"
    if (!newfile.good()) //if the file could not be created
    {
        cout<<"the file failed to be created"<<endl;
        return 0;
    }
    newfile<<"string you want to write to file"; //adds string to file
    newfile.close(); //closes file and writes it to disk
    return 0; //again, this does not check for many possible errors
}
</iostream></ofstream>


这篇关于如何使用C ++在.Rtf文件中编写字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆