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. github打开慢,页面打不开,请求老是失败问题修复总结

    感谢老铁 QQ(1218624820) 提供的方法建议 原因来自于DNS污染, 到下面的目录进行修改文件 C:\Windows\System32\drivers\etc 在后面粘贴下面的信息 192. ...

  2. 使用POI导出Excel(二)-利用模板

    一.基本操作见: 使用POI导出Excel 二.本次功能需求 给了一个模板,里面有6个sheet页,每页里面都需要填充相应的数据.如图: 三.需求分析 1.分了6个sheet页,每页的数据都不一样,首 ...

  3. LINUX 设置交换分区的大小

    创建交换分区目录 mkdir /data1/mnt/ 卸载当前交换分区 swapoff /data1/mnt/10GB.swap 设置交换分区为 5Gdd if=/dev/zero of=/data1 ...

  4. Yii隐藏单入口

    Yii进入项目首页时默认是index.php文件路径,如何把index.php去掉,方法如下: 打开apache配置文件http.conf,找到如下的代码: #LoadModule rewrite_m ...

  5. vim自动缩进

    最近写完程序,在进行调试时发现特别困难,代码乱的一塌糊涂,特别是代码量很多时,调试起来特别囧,逻辑很难理清. 这让我想起了缩进功能,可以让代码自动对齐. gedit编辑器在菜单栏里的编辑->首选 ...

  6. 2018-2019-2 《网络对抗技术》Exp4 恶意代码分析 20165222李勖

    1.系统运行监控 (1)使用如计划任务,每隔一分钟记录自己的电脑有哪些程序在联网,连接的外部IP是哪里.运行一段时间并分析该文件,综述一下分析结果.目标就是找出所有连网的程序,连了哪里,大约干了什么( ...

  7. Android - 传统蓝牙通信聊天

    Android -传统蓝牙通信聊天 技术:java+Android4.4+jdk1.8 运行环境:Android4.4.Android7.0 概述 Android 传统蓝牙的使用,包括开关蓝牙.搜索设 ...

  8. 根据tomcat的日志判断web的发布路径以及服务路径

    [ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.apache.catalina.startup.HostConfig.unde ...

  9. hadoop启动时,报ssh: Could not resolve hostname xxx: Name or service not known

    本文转载自:http://blog.csdn.net/wodewutai17quiet/article/details/76795951 问题:hadoop启动时,报ssh: Could not re ...

  10. [转][Dapper]SQL 经验集

    condition.Append(" AND ChineseName like @name"); p.Add("@name", "%" + ...