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. Jenkins使用Git Parameter插件打包

    一. 下载Git Parameter插件: 二. 项目配置: Shell脚本: #!/bin/bash -l echo $deploy_envcase $deploy_env in deploy) e ...

  2. Java第五次作业--面向对象高级特性(抽象类和接口)

    一.学习要点 认真看书并查阅相关资料,掌握以下内容: 掌握抽象类的设计 掌握接口的设计 理解简单工厂设计模式 理解抽象类和接口的区别 掌握包装类的应用 掌握对象的比较方法和比较器的使用 学习使用日期操 ...

  3. PyQt4 ShowHMDB show sqlite3 with QTableWidget summary

    PyQt4 ShowHMDB show sqlite3 with QTableWidget summary Source Code: https://github.com/zengjfgit/Pyth ...

  4. Python 简单网页爬虫学习

    #coding=utf-8 # 参考文章: # 1. python实现简单爬虫功能 # http://www.cnblogs.com/fnng/p/3576154.html # 2. Python 2 ...

  5. mongodb 修改openfiles方法

    # vi /etc/security/limits.conf 在file中加上以下内容:(注意红色的为添加的) #ftp hard nproc 0#@student - maxlogins 4* so ...

  6. oracle Union 中 ORA-12704:字符集不匹配问题的解决 .

    在使用Union all连接时,若A集合中某列为nvarchar2或nvarchar类型,而B集合中无此列,用‘ ’ 来代替是会报字符集不匹配,解决方法有两种,见下面的示例 例: select '中国 ...

  7. zedgraph控件怎么取得鼠标位置的坐标值(转帖)

    我想取得zedgraph控件上任意鼠标位置的坐标值,IsShowCursorValues可以显示鼠标位置的值但是不能提取赋值给其他的变量.用PointValueEvent这个事件又只能得到已经画出的点 ...

  8. C++等语言中整型int等的取值范围计算方式

    举short为例说明 如果以最高位为符号位,二进制原码最大为0111111111111111=2的15次方减1=32767.最小为1111111111111111=-2的15次方减1=-32767此时 ...

  9. 国产FPGA市场分析 该如何破局

    2018年上半年对于中国半导体行业而言是多事之秋,发生了几件让国人深入思考的大事.我作为IC产业的逃兵,最近也在思考很多的问题,包括资本市场.集成电路行业和研究所的一些不成熟的想法. 2008年进入华 ...

  10. C# 通过Exchange server 发送邮件

    微软的Exchange邮件服务不同与一般的邮件server,他不能简单使用SmtpClient等组件实现邮件收发的功能. 那么怎么通过Exchange服务发送邮件呢? 微软的Exchange服务都有w ...