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. Oracle联合查询

    select * from teacher--联合查询 --01.union (并集)select tno from teacher where tno>1080 union(select tn ...

  2. test20181025 Color

    题意 分析 自己的想法 可以莫队+平衡树. 对每个颜色维护一颗平衡树,然后移动莫队端点的时候在平衡树中查询. 区间加操作容易实现. 单点修改转化为平衡树的插入删除. 感谢Z前辈的指导. 时间复杂度\( ...

  3. WinSCP一个好用的连接linux服务器的

    用虚拟机ssh登陆远程服务器,终端命令copy本地文件到服务器简直弱爆了. 不然用win下的WinSCP,牛逼到爆了.操作跟FTP软件差不多

  4. MyEclipse部署项目到Tomcat上,但是classes文件夹下没有编译项目

    在MyEclipse中把项目部署到Tomcat上,但是Tomcat下的classes文件夹下没有编译项目解决方法:1-直接在点击菜单栏的Project--clean,对项目进行clean2-查看菜单栏 ...

  5. 1.Appium环境搭建

    1.安装node.js (1)去node官网下载,根据操作系统的不同选择不同对应的版本(https://nodejs.org/en/download/) (2)下载对应的版本后进行安装,一直下一步直至 ...

  6. vue项目修改favicon

    首先你的在你的static文件中添加favicon.icon 然后通过以下方式进行修改 1)方式一:修改index.html文件 <link rel="shortcut icon&qu ...

  7. Mac 下使用brew install 报错: Cowardly refusing to `sudo brew install'

    Mac 下使用brew install 报错: localhost:infer-osx-v0.6.0 admin$ sudo brew install opam Error: Cowardly ref ...

  8. C#使用OpcNetApi.dll和OpcNetApi.Com.dll操作OPC

    本人学习了一下.Net,恰好,51自学网,又要用这个.而网上很多VC6,VB6,VB .Net的但,很少C#的.现在研究一下,给出例子: 测试平台,是VS2008,KEPServer,OpcNetAp ...

  9. 进程基本-进程创建,僵尸进程,exec系列函数

    Linux系统中,进程的执行模式划分为用户模式和内核模式,当进程运行于用户空间时属于用户模式,如果在用户程序运行过程中出现系统调用或者发生中断事件,就要运行操作系统(即核心)程序,进程的运行模式就变为 ...

  10. osx安装启动mysql

    安装mysql 最新版 56 brew install mysql 1 启动报错 ben:~ soul$ which mysql /usr/local/bin/mysql ben:~ soul$ my ...