Write to File with C++

#include <iostream.h>
#include <fstream.h> int main()
{
const char *FILENAME = "myfile.txt"; ofstream fout(FILENAME); cout << "Enter your text: ";
char str[100];
cin >> str;
fout << "here is your text\n";
fout << str << endl; fout.close(); ifstream fin(FILENAME);
char ch;
while (fin.get(ch)) {
cout << ch;
}
fin.close(); return 0;
}
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector> using namespace std; template <class T>
void print(T & c){
for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
std::cout << *i << endl;
}
} int main()
{
vector <int> output_data(10); generate(output_data.begin(),output_data.end(),rand);
transform(output_data.begin(),output_data.end(),
output_data.begin(),bind2nd(modulus<int>(),10));
print(output_data); ofstream out("data.txt"); if(!out){
cout << "Couldn't open output file\n";
return 0;
} copy(output_data.begin(),output_data.end(),ostream_iterator<int>(out,"\n"));
out.close(); ifstream in("data.txt");
if(!in){
cout << "Couldn't open input file\n";
return 0;
} vector<int> input_data((istream_iterator<int>(in)),istream_iterator<int>());
in.close(); print(input_data); return 0;
}

Write to File with Qt

Write Binary to File with Qt


void WriteBinaryToFile(QString binaryStr, QString filePath)
{
QFile file;
QByteArray ba; QStringList ltStrs = binaryStr.split(' ');
foreach(QString str,ltStrs) {
ba.append((char)(str.toInt(0,16) & 0xff));
} file.setFileName(filePath);
if(!file.open(QIODevice::WriteOnly)){
return;
} file.write(ba);
file.close();
}

Write plain Text to File with Qt

void WritePlainTextToFile(QString plainText, QString filePath)
{
QFile file;
QTextStream out; file.setFileName(qsFilePath);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
return;
} out.setDevice(&file);
out << plainText;
file.close();
}

Write File with Unicode bom

///< some include

int main(int argc, char \*argv[])
{
QCoreApplication a(argc, argv);
QString str = QString("这是中文,QString");
QFile file;
QTextStream out; file.setFileName("a.txt");
if(!file.open(QIODevice::WriteOnly|QIODevice::Text)){
qDebug() << file.errorString();
return 0;
} out.setDevice(&file);
out.setCodec("UTF-16"); ///< unicode
out.setGenerateByteOrderMark(true); ///< with bom
out << str;
file.close();
qDebug() << "OK!"; return a.exec();
}

How to check(see) it in vim

 vim see the file hex: %!xxd
see the text : %!xxd -r

Another way to write file

ofstream myfile;
myfile.open("a.txt");
myfile << "\xEF\xBB\xBF"; // UTF-8 BOM
myfile << "\xE2\x98\xBB"; // U+263B
myfile.close();
ofstream myfile;
myfile.open("a.txt");
myfile << "\xFF\xFE"; // UTF-16 BOM
myfile << "\x3B\x26"; // U+263B
myfile.close();

Write File的更多相关文章

  1. 记一个mvn奇怪错误: Archive for required library: 'D:/mvn/repos/junit/junit/3.8.1/junit-3.8.1.jar' in project 'xxx' cannot be read or is not a valid ZIP file

    我的maven 项目有一个红色感叹号, 而且Problems 存在 errors : Description Resource Path Location Type Archive for requi ...

  2. HTML中上传与读取图片或文件(input file)----在路上(25)

    input file相关知识简例 在此介绍的input file相关知识为: 上传照片及文件,其中包括单次上传.批量上传.删除照片.增加照片.读取图片.对上传的图片或文件的判断,比如限制图片的张数.限 ...

  3. logstash file输入,无输出原因与解决办法

    1.现象 很多同学在用logstash input 为file的时候,经常会出现如下问题:配置文件无误,logstash有时一直停留在等待输入的界面 2.解释 logstash作为日志分析的管道,在实 ...

  4. input[tyle="file"]样式修改及上传文件名显示

    默认的上传样式我们总觉得不太好看,根据需求总想改成和上下结构统一的风格…… 实现方法和思路: 1.在input元素外加a超链接标签 2.给a标签设置按钮样式 3.设置input[type='file' ...

  5. .NET平台开源项目速览(16)C#写PDF文件类库PDF File Writer介绍

    1年前,我在文章:这些.NET开源项目你知道吗?.NET平台开源文档与报表处理组件集合(三)中(第9个项目),给大家推荐了一个开源免费的PDF读写组件 PDFSharp,PDFSharp我2年前就看过 ...

  6. [笔记]HAproxy reload config file with uninterrupt session

    HAProxy is a high performance load balancer. It is very light-weight, and free, making it a great op ...

  7. VSCode调试go语言出现:exec: "gcc": executable file not found in %PATH%

    1.问题描述 由于安装VS15 Preview 5,搞的系统由重新安装一次:在用vscdoe编译go语言时,出现以下问题: # odbcexec: "gcc": executabl ...

  8. input type='file'上传控件假样式

    采用bootstrap框架样式 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> &l ...

  9. FILE文件流的中fopen、fread、fseek、fclose的使用

    FILE文件流用于对文件的快速操作,主要的操作函数有fopen.fseek.fread.fclose,在对文件结构比较清楚时使用这几个函数会比较快捷的得到文件中具体位置的数据,提取对我们有用的信息,满 ...

  10. ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id

    出现场景:当点击"分类"再返回"首页"时,发生error退出   BUG描述:Caused by: java.lang.IllegalArgumentExcep ...

随机推荐

  1. 离散数学:用C语言来判断集合存在的二元关系

    用C语言来判断是否满足自反,反自反,非自反,对称,反对称,非对称和传递性 也不知道写的对不对.没有大量验证,但是随便找的一些关系测试的没毛病,如果错了,欢迎各位大佬留言 #include<bit ...

  2. windows常用DLL及作用

    Kernel.dll:内存,硬盘灯硬件管理的相关函数. gdi32.dll:图形显示相关函数(LoadImage,GetPixel,StretchBlt,Ellipse). user32.dll:wi ...

  3. php 自定义函数大全

    1. call_user_func和call_user_func_array 以上两个函数以不同的参数形式调用函数.见如下示例: <?php class demo{ public static ...

  4. 大数据框架hadoop的序列化机制

    Java内建序列化机制 在Windows系统上序列化的Java对象,可以在UNIX系统上被重建出来,不需要担心不同机器上的数据表示方法,也不需要担心字节排列次序. 在Java中,使一个类的实例可被序列 ...

  5. mysqlinsert触发器的创建

    CREATE DEFINER=`wpsuper`@`%` TRIGGER `o2oinsert` BEFORE INSERT ON `t_s_o2o`FOR EACH ROW begin set Ne ...

  6. vim配置之tagbar

    vimConfig/plugin/tagbar-setting.vim let g:tagbar_width=4 map <F12> :TagbarToggle<CR> map ...

  7. jsp---猜数字游戏,深有感触

    猜数字游戏注意两点.1.随机数和猜的数字不能放在同一个页面,不然随机数不停出现,猜的数字不可能相等的. 2.数据类型的相互转换.包装类Integer和int的用法,前者是类,后者是基本数据类型 cai ...

  8. [经验]PLSQL乱码解决

    本文摘自:http://jingyan.baidu.com/article/36d6ed1f2861f41bcf488327.html @echo off set path=D:\Program Fi ...

  9. [MVC 4] ActionResult 使用示例

    在控制器 HomeController.cs 中使用以下代码 public ActionResult Contact() { ViewBag.Message = "Your contact ...

  10. django-引用静态文件

    1.需要配置settings # 静态文件目录 STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static') 2.页面加载静态文件 {% load sta ...